Finish porting to new moves format in moved(), change syntax _a lot_, and refactored checkForCheck()

This commit is contained in:
pixl_xip 2024-01-18 20:51:01 -07:00
parent 76a6080cff
commit dbfe49adf2

View file

@ -5,7 +5,8 @@ if (ERUDA) eruda.init();
const log = DEBUG ? DIV ? (a) => { document.getElementById(`log`).innerText += `\n` + a; } : console.log : () => {};
const table = document.getElementById(`table`);
const [WP, WR, WN, WB, WQ, WK, BP, BR, BN, BB, BQ, BK, NP, WKP, BKP] = [``, ``, ``, ``, ``, ``, `♟︎`, ``, ``, ``, ``, ``, ` `, [7, 4], [0, 4]];
const [WP, WR, WN, WB, WQ, WK, BP, BR, BN, BB, BQ, BK, NP, WKP, BKP] =
[``, ``, ``, ``, ``, ``, `♟︎`, ``, ``, ``, ``, ``, ` `, [7, 4], [0, 4]];
const board = [
[BR, BN, BB, BQ, BK, BB, BN, BR],
@ -60,13 +61,6 @@ const deepCopy = (array) => {
return array.map((item) => Array.isArray(item) ? deepCopy(item) : item);
}
const movedLegacy = (piece, target, sourceBoard) => {
const newBoard = deepCopy(sourceBoard);
newBoard[target[0]][target[1]] = newBoard[piece[0]][piece[1]];
newBoard[piece[0]][piece[1]] = NP;
return newBoard;
}
const moved = (from, move, sourceBoard) => {
const newBoard = deepCopy(sourceBoard);
const allMoves = [[from, move.main], ...(move.sideEffects)];
@ -147,27 +141,95 @@ const castleMoves = (a, b, list, hypotheticalBoard, player, distance, requiredSp
distance == 0 || // pass if distance is 0, because why would it be?
requiredSpace == 0 // pass if required space is 0, because why would it be?
) return;
// log([`playerIsWhite: `, playerIsWhite, `rightCastle: `, rightCastle, `movedPieces: `, movedPieces, `a: `, a, `b: `, b, `list: `, list, `hypotheticalBoard: `, hypotheticalBoard, `player: `, player, `distance: `, distance, `requiredSpace: `, requiredSpace]);
if (playerIsWhite && rightCastle) { // if player is white and for right castling
for (let i = 1; i < requiredSpace + 1; i++) if (hypotheticalBoard[a][b + i] != NP || (checkForCheck(`white`, movedLegacy([a, b], [a, b + i], hypotheticalBoard)) && i < distance + 1)) return; // loop over the distance, making sure that there is no piece. if there is one, return, because no castling here! or, if it would be checked in that position, also no-go! (if it is in distance, of course)
list.push({ main: [a, b + distance], sideEffects: [[[a, b + (requiredSpace + 1)], [a, b + (distance - 1)]]] });
return;
}
if (playerIsWhite && !rightCastle) { // if player is white and for left castling
for (let i = -1; i > requiredSpace - 1; i--) if (hypotheticalBoard[a][b + i] != NP || (checkForCheck(`white`, movedLegacy([a, b], [a, b + i], hypotheticalBoard)) && i > distance - 1)) return; // loop over the distance, making sure that there is no piece. if there is one, return, because no castling here! or, if it would be checked in that position, also no-go! (if it is in distance, of course)
list.push({ main: [a, b + distance], sideEffects: [[[a, b + (requiredSpace - 1)], [a, b + (distance + 1)]]] });
return;
}
if (!playerIsWhite && rightCastle) { // if player is black and for right castling
for (let i = 1; i < requiredSpace + 1; i++) if (hypotheticalBoard[a][b + i] != NP || (checkForCheck(`black`, movedLegacy([a, b], [a, b + i], hypotheticalBoard)) && i < distance + 1)) return; // loop over the distance, making sure that there is no piece. if there is one, return, because no castling here! or, if it would be checked in that position, also no-go! (if it is within distance, of course)
list.push({ main: [a, b + distance], sideEffects: [[[a, b + (requiredSpace + 1)], [a, b + (distance - 1)]]] });
return;
}
if (!playerIsWhite && !rightCastle) { // if player is black and for right castling
for (let i = -1; i > requiredSpace - 1; i--) if (hypotheticalBoard[a][b + i] != NP || (checkForCheck(`black`, movedLegacy([a, b], [a, b - i], hypotheticalBoard)) && i > distance - 1)) return; // loop over the distance, making sure that there is no piece. if there is one, return, because no castling here! or, if it would be checked in that position, also no-go! (if it is in distance, of course)
list.push({ main: [a, b + distance], sideEffects: [[[a, b + (requiredSpace - 1)], [a, b + (distance + 1)]]] });
return;
}
log(deepCopy([`playerIsWhite: `, playerIsWhite, `rightCastle: `, rightCastle, `movedPieces: `, movedPieces, `a: `, a, `b: `, b, `list: `, list, `hypotheticalBoard: `, hypotheticalBoard, `player: `, player, `distance: `, distance, `requiredSpace: `, requiredSpace]));
log([`hi`]);
for (
let i = 0;
rightCastle ?
(i < requiredSpace) :
(i > requiredSpace);
rightCastle ?
(i++) :
(i--)
) {
// log start
console.warn([hypotheticalBoard[a][
rightCastle ?
(b + (i + 1)) :
(b + (i - 1))
],
checkForCheck(playerIsWhite, moved(
[a, b],
{
main: [
a,
b + i
],
sideEffects: []
},
hypotheticalBoard
)),
rightCastle ?
(i < distance + 1) :
(i > distance - 1)
]);
// log end
if (
hypotheticalBoard[a][
rightCastle ?
(b + i + 1) :
(b + (i - 1))
] != NP || (
checkForCheck(playerIsWhite, moved(
[a, b],
{
main: [
a,
b + i
],
sideEffects: []
},
hypotheticalBoard
)) &&
(
rightCastle ?
(i < distance + 1) :
(i > distance - 1)
)
)
) return;
}
log([requiredSpace, distance]);
list.push({
main: [
a,
b + distance
],
sideEffects: [
[
[
a,
b + (
rightCastle ?
(requiredSpace + 1) :
(requiredSpace - 1)
)
], [
a,
b + (
rightCastle ?
(distance - 1) :
(distance + 1)
)
]
]
]
});
log(`---`);
log(deepCopy(list));
log([a, b + distance]);
log(`---`)
}
const knMoves = (a, b, list, compare, hypotheticalBoard, pos) => {
@ -178,28 +240,106 @@ const knMoves = (a, b, list, compare, hypotheticalBoard, pos) => {
b + pos[i][1] >= hypotheticalBoard[0].length ||
b + pos[i][1] < 0) continue;
if (compare.includes(hypotheticalBoard[a + pos[i][0]][b + pos[i][1]])) {
list.push({ main: [a + pos[i][0], b + pos[i][1]], sideEffects: [] });
list.push({
main: [
a + pos[i][0],
b + pos[i][1]],
sideEffects: []
});
}
}
if (![WK, BK].includes(hypotheticalBoard[a][b])) return; // if the piece isnt a king, youre gonzo
const player = hypotheticalBoard[a][b] == WK ? `white` : `black`;
castleMoves(a, b, list, hypotheticalBoard, player, 2, 2);
castleMoves(a, b, list, hypotheticalBoard, player, -2, -3);
if (
![WK, BK]
.includes(
hypotheticalBoard[a][b]
)) return; // if the piece isnt a king, youre gonzo
const player =
hypotheticalBoard[a][b] == WK ?
`white` :
`black`;
castleMoves(
a,
b,
list,
hypotheticalBoard,
player,
2,
2
);
castleMoves(
a,
b,
list,
hypotheticalBoard,
player,
-2,
-3
);
}
const rbqMoves = (a, b, list, compare, hypotheticalBoard, pos, includeBlanks) => {
for (let i = 0; i < pos.length; i++) {
for (let j = 1; (
a + (j * pos[i][0]) >= 0 &&
a + (j * pos[i][0]) < hypotheticalBoard.length &&
b + (j * pos[i][1]) >= 0 &&
b + (j * pos[i][1]) < hypotheticalBoard[0].length
a + (
j * pos[i][0]
) >= 0 &&
a + (
j * pos[i][0]
) < hypotheticalBoard.length &&
b + (
j * pos[i][1]
) >= 0 &&
b + (
j * pos[i][1]
) < hypotheticalBoard[0].length
); j++) {
if (hypotheticalBoard[a + (j * pos[i][0])][b + (j * pos[i][1])] == NP) {
if (includeBlanks) list.push({ main: [a + (j * pos[i][0]), b + (j * pos[i][1])], sideEffects: [] });
} else if (compare.includes(hypotheticalBoard[a + (j * pos[i][0])][b + (j * pos[i][1])])) {
if (
hypotheticalBoard[
a + (
j * pos[i][0]
)
][
b + (
j * pos[i][1]
)
] == NP) {
if (includeBlanks)
list.push({
main: [
a + (
j * pos[i][0]
),
b + (
j * pos[i][1]
)
],
sideEffects: []
});
} else if (
compare.includes(
hypotheticalBoard[
a + (
j * pos[i][0]
)
][
b + (
j * pos[i][1]
)
]
)
) {
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])], sideEffects: [] });
list.push({
main: [
a + (
j * pos[i][0]
),
b + (
j * pos[i][1]
)
],
sideEffects: []
});
break;
} else break;
}
@ -207,19 +347,90 @@ const rbqMoves = (a, b, list, compare, hypotheticalBoard, pos, includeBlanks) =>
};
const checkForCheck = (player, hypotheticalBoard) => {
const playerIsWhite = (player == `white`) || (player == true);
const [OK, OQ, OR, OB, ON, OP, [a, b]] = playerIsWhite ? [BK, BQ, BR, BB, BN, BP, currentKingPosition.white] : [WK, WQ, WR, WB, WN, WP, currentKingPosition.black];
const playerIsWhite =
(player == `white`) ||
(player == true);
const [
OK,
OQ,
OR,
OB,
ON,
OP,
[a, b]
] =
playerIsWhite ?
[
BK,
BQ,
BR,
BB,
BN,
BP,
currentKingPosition.white
]
:
[
WK,
WQ,
WR,
WB,
WN,
WP,
currentKingPosition.black
];
const list = [];
log(`calculating if it is checked... 🤖 looking at coords ${a}, ${b}`);
// pawns because they are confusing to do the other way
if ((playerIsWhite ? hypotheticalBoard[a - 1][b + 1] : hypotheticalBoard[a + 1][b + 1]) == OP) return true;
if ((playerIsWhite ? hypotheticalBoard[a - 1][b - 1] : hypotheticalBoard[a + 1][b - 1]) == OP) return true;
if (
(
playerIsWhite ?
hypotheticalBoard[a - 1][b + 1] :
hypotheticalBoard[a + 1][b + 1]
) == OP
) return true;
if (
(
playerIsWhite ?
hypotheticalBoard[a - 1][b - 1] :
hypotheticalBoard[a + 1][b - 1]
) == OP
) return true;
// knights and kings
knMoves(a, b, list, [ON, OK], hypotheticalBoard, [...knightRelativePositions, ...kingRelativePositions]);
knMoves(
a,
b,
list,
[
ON,
OK
],
hypotheticalBoard,
[
...knightRelativePositions,
...kingRelativePositions
]
);
// bishops, rooks, queens
rbqMoves(a, b, list, [OQ, OR, OB], hypotheticalBoard, queenRelativePositions);
rbqMoves(
a,
b,
list,
[
OQ,
OR,
OB
],
hypotheticalBoard,
queenRelativePositions
);
log(`passed so far... length is ${list.length}`);
console.error()
return list.length > 0;
}
@ -290,23 +501,36 @@ const possibleMoves = (a, b) => {
knMoves(a, b, moves, [...whites, NP], board, kingRelativePositions);
break;
}
moves.filter((move) => checkForCheck(whites.includes(targetPiece), moved([a, b], move, board)));
moves.filter(
(move) =>
checkForCheck(
whites.includes(targetPiece),
moved([a, b], move, board)
)
);
return moves;
};
const highlightCells = (cells) => {
for (let i = 0; i < cells.length; i++)
document.querySelectorAll(`.cr${cells[i][0]}.cc${cells[i][1]}`)[0].classList.add(`highlighted`);
document.querySelectorAll(
`.cr${cells[i][0]}.cc${cells[i][1]}`
)[0].classList.add(`highlighted`);
}
const clearHighlightedCells = () => {
while (document.querySelectorAll(`.highlighted`).length)
document.querySelectorAll(`.highlighted`)[0].classList.remove(`highlighted`);
document.querySelectorAll(
`.highlighted`
)[0].classList.remove(`highlighted`);
}
const getMainMovesFromArrayOfObjects = (array) => {
const newArray = [];
for (const object in array) newArray.push(array[object].main);
for (const object in array)
newArray.push(
array[object].main
);
return newArray;
}
@ -314,9 +538,22 @@ const cellClick = (event) => {
const a = +(event.target.a);
const b = +(event.target.b);
clearHighlightedCells();
if ((highlighting.a != a || highlighting.b != b) && board[a][b] != NP) {
log(possibleMoves(a, b));
highlightCells([...getMainMovesFromArrayOfObjects(possibleMoves(a, b)), [a, b]]);
if (
(
highlighting.a != a ||
highlighting.b != b
) &&
board[a][b] != NP
) {
// log(possibleMoves(a, b));
highlightCells(
[
...getMainMovesFromArrayOfObjects(
possibleMoves(a, b)
),
[a, b]
]
);
highlighting.a = a;
highlighting.b = b;
} else {
@ -355,6 +592,8 @@ writePiece(6, 5, BR);
//cellClick(3, 4);
cellClick({target: {a: 7, b: 4}});
document.querySelector(`#title`).addEventListener(`click`, () => {
fetch("/post", {
method: "POST",