En Passent, add movePiece()

This commit is contained in:
pixl_xip 2024-01-22 20:12:56 -07:00
parent 4776877a54
commit 2aa2ef0233
3 changed files with 62 additions and 33 deletions

View file

@ -84,9 +84,9 @@ const highlightedCells = [];
const highlightCells = (cells) => {
for (let i = 0; i < cells.length; i++) {
document.querySelectorAll(
`.cr${cells[i][0]}.cc${cells[i][1]}`
`.cr${cells[i][1][0]}.cc${cells[i][1][1]}`
)[0].classList.add(`highlighted`);
highlightedCells.push(cells[i]);
highlightedCells.push(cells[i][1]);
}
}
@ -114,8 +114,8 @@ const cellClick = (event) => {
[
...common.getMainMovesFromArrayOfObjects(
common.possibleMoves(a, b)
),
[a, b]
),
[null, [a, b]]
]
);
highlighting.a = a;

View file

@ -32,6 +32,7 @@ export const hasMoved = {
rightRook: false,
},
}
export const canEnPassent = false;
export const currentKingPosition = {
white: [7, 4],
black: [0, 4],
@ -49,13 +50,17 @@ export const deepCopy = (array) => {
return array.map((item) => Array.isArray(item) ? deepCopy(item) : item);
}
export const moved = (from, move, sourceBoard) => {
export const moved = (move, sourceBoard) => {
const newBoard = deepCopy(sourceBoard);
const allMoves = [[from, move.main], ...(move.sideEffects)];
const allMoves = [move.main, ...(move.sideEffects)];
for (const moveNumber in allMoves) {
newBoard[allMoves[moveNumber][1][0]][allMoves[moveNumber][1][1]] = sourceBoard[allMoves[moveNumber][0][0]][allMoves[moveNumber][0][1]];
if (newBoard[allMoves[moveNumber][0][0]][allMoves[moveNumber][0][1]] == sourceBoard[allMoves[moveNumber][0][0]][allMoves[moveNumber][0][1]]) newBoard[allMoves[moveNumber][0][0]][allMoves[moveNumber][0][1]] = NP;
const [[aa, ab], [ba, bb]] = allMoves[moveNumber];
if (aa == `air`) newBoard[ba][bb] = NP;
else {
newBoard[ba][bb] = sourceBoard[aa][ab];
if (newBoard[aa][ab] == sourceBoard[aa][ab]) newBoard[aa][ab] = NP;
}
}
return newBoard;
}
@ -108,14 +113,18 @@ export const pawnMoves = (a, b, list, compare, hypotheticalBoard, player) => {
hypotheticalBoard[a + 1][b] == NP &&
hypotheticalBoard[a + 2][b] == NP
)) {
list.push({ main: (player ? [a - 1, b] : [a + 1, b]), sideEffects: [] });
list.push({ main: (player ? [a - 2, b] : [a + 2, b]), sideEffects: [] });
} else if (player ? (hypotheticalBoard[a - 1][b] == NP) : (hypotheticalBoard[a + 1][b] == NP))
list.push({ main: (player ? [a - 1, b] : [a + 1, b]), sideEffects: [] });
if (compare.includes(player ? hypotheticalBoard[a - 1][b - 1] : hypotheticalBoard[a + 1][b - 1]))
list.push({ main: (player ? [a - 1, b - 1] : [a + 1, b - 1]), sideEffects: [] });
if (compare.includes(player ? hypotheticalBoard[a - 1][b + 1] : hypotheticalBoard[a + 1][b + 1]))
list.push({ main: (player ? [a - 1, b + 1] : [a + 1, b + 1]), sideEffects: [] });
list.push({ main: [[a, b], (player ? [a - 1, b] : [a + 1, b])], sideEffects: [] });
list.push({ main: [[a, b], (player ? [a - 2, b] : [a + 2, b])], sideEffects: [] });
} else if (hypotheticalBoard[player ? a - 1 : a + 1][b] == NP)
list.push({ main: [[a, b], [player ? a - 1 : a + 1, b]], sideEffects: [] });
if (compare.includes(hypotheticalBoard[player ? a - 1 : a + 1][b - 1]))
list.push({ main: [[a, b], [player ? a - 1 : a + 1, b - 1]], sideEffects: [] });
if (compare.includes(hypotheticalBoard[player ? a - 1 : a + 1][b + 1]))
list.push({ main: [[a, b], [player ? a - 1 : a + 1, b + 1]], sideEffects: [] });
if (canEnPassent == b + 1 && a == (player ? 3 : 4))
list.push({ main: [[a, b], [player ? a - 1 : a + 1, b + 1]], sideEffects: [[[null, null], [a, b + 1]]]});
else if (canEnPassent == b - 1 && a == (player ? 3 : 4))
list.push({ main: [[a, b], [player ? a - 1 : a + 1, b - 1]], sideEffects: [[[null, null], [a, b - 1]]]});
}
export const castleMoves = (a, b, list, hypotheticalBoard, player, distance, requiredSpace) => {
@ -147,12 +156,11 @@ export const castleMoves = (a, b, list, hypotheticalBoard, player, distance, req
(b + (i - 1))
] != NP || (
checkForCheck(playerIsWhite, moved(
[a, b],
{
main: [
main: [[a, b], [
a,
b + i
],
]],
sideEffects: []
},
hypotheticalBoard
@ -168,8 +176,11 @@ export const castleMoves = (a, b, list, hypotheticalBoard, player, distance, req
console.log([requiredSpace, distance]);
list.push({
main: [
a,
b + distance
[a, b],
[
a,
b + distance
]
],
sideEffects: [
[
@ -202,9 +213,9 @@ export const knMoves = (a, b, list, compare, hypotheticalBoard, pos) => {
b + pos[i][1] < 0) continue;
if (compare.includes(hypotheticalBoard[a + pos[i][0]][b + pos[i][1]])) {
list.push({
main: [
main: [[a, b], [
a + pos[i][0],
b + pos[i][1]],
b + pos[i][1]]],
sideEffects: []
});
}
@ -292,12 +303,15 @@ export const rbqMoves = (a, b, list, compare, hypotheticalBoard, pos, includeBla
// console.log(hypotheticalBoard[a + (j * pos[i][0])][b + (j * pos[i][1])]);
list.push({
main: [
a + (
j * pos[i][0]
),
b + (
j * pos[i][1]
)
[a, b],
[
a + (
j * pos[i][0]
),
b + (
j * pos[i][1]
)
]
],
sideEffects: []
});
@ -408,20 +422,20 @@ export const possibleMoves = (a, b) => {
// example of moves:
// [
// {
// main: [2, 1],
// main: [[2, 0], [2, 1]],
// sideEffects: [
// [[5, 6], [5, 4]],
// [[2, 5], [3, 5]]
// ]
// },
// {
// main: [4, 5],
// main: [[4, 5], [5, 7]],
// sideEffects: []
// }
// ]
//
// an entry must ALWAYS have main; sideEffects is optional.
// main is just TO
// main is FROM, then TO
// sideEffects may have multiple side effects, and the first is FROM, the second is TO
switch (targetPiece) {
@ -466,7 +480,7 @@ export const possibleMoves = (a, b) => {
(move) =>
checkForCheck(
whites.includes(targetPiece),
moved([a, b], move, board)
moved(move, board)
)
);
return moves;
@ -480,3 +494,17 @@ export const getMainMovesFromArrayOfObjects = (array) => {
);
return newArray;
}
export const movePiece = (move, player) => {
const playerIsWhite = (
player == `white` ||
player == true
);
const pieces = (playerIsWhite ? hasMoved.white : hasMoved.black);
const [[fromA, fromB], [toA, toB]] = move.main;
if (move.main[0] == (player ? [7, 4] : [0, 4])) pieces.king = true;
if (move.main[0] == (player ? [7, 0] : [0, 0])) pieces.leftRook = true;
if (move.main[0] == (player ? [7, 7] : [0, 7])) pieces.rightRook = true;
if ([WP, BP].includes(board[fromA][fromB]) && Math.abs(fromA - toA) == 2) canEnPassent = toB;
board = moved(move, board);
}

View file

@ -110,6 +110,7 @@ const handleIncomingMove = (request, id) => {
return;
}
}
const startGame = (gameID) => {