Do some stuff concerning moving pieces. currently, its kinda bugged out`

This commit is contained in:
pixl_xip 2024-01-22 22:18:55 -07:00
parent c7f2bdfae2
commit 49cddcb7e4
3 changed files with 75 additions and 43 deletions

View File

@ -9,6 +9,7 @@ let username = ``;
const socket = new WebSocket(`ws://${window.location.hostname}:8080`);
socket.onopen = () => {
log('connected!');
Array.from(document.querySelectorAll(`.celldiv`)).map(e => e.addEventListener(`click`, cellClick));
document.querySelectorAll(`#joinbutton`)[0].addEventListener(`click`, () => {
username = document.querySelectorAll(`#username`)[0].value;
if (!username) return;
@ -60,6 +61,17 @@ const handleIncomingWebSocketRequest = (requestRaw) => {
printMessage(`You've already joined! How did we even get here? Try refreshing the page?`);
break;
}
break;
case `move`:
switch (request.result) {
case `accept`:
movePiece(request.move, request.player);
break;
case `reject`:
printMessage(`Your move was denied: ${request.reject}`);
break;
}
break;
}
}
@ -94,13 +106,16 @@ const highlighting = {
b: undefined,
};
const highlightedCells = [];
const correspondingMoveToHighlight = new Map();
const highlightCells = (cells) => {
const highlightCells = (moves, clickCoords) => {
const cells = [...common.getMainMovesFromArrayOfObjects(moves), [null, clickCoords]];
for (let i = 0; i < cells.length; i++) {
document.querySelectorAll(
`.cr${cells[i][1][0]}.cc${cells[i][1][1]}`
)[0].classList.add(`highlighted`);
highlightedCells.push(cells[i][1]);
correspondingMoveToHighlight.set(clickCoords, moves[i]);
}
}
@ -110,28 +125,30 @@ const clearHighlightedCells = () => {
`.highlighted`
)[0].classList.remove(`highlighted`);
highlightedCells.length = 0;
correspondingMoveToHighlight.clear();
return false;
}
const cellClick = (event) => {
const a = +(event.target.a);
const b = +(event.target.b);
clearHighlightedCells();
if (
console.log(common.deepCopy(highlightedCells), a, b);
if (highlightedCells.includes([a, b])) {
socket.send(JSON.stringify({
type: `move`,
move: correspondingMoveToHighlight.get([a, b]),
player: side,
}));
console.log(correspondingMoveToHighlight.get([a, b]));
} else if (
clearHighlightedCells() ||
(
highlighting.a != a ||
highlighting.b != b
) &&
common.board[a][b] != common.NP
) {
// log(possibleMoves(a, b));
highlightCells(
[
...common.getMainMovesFromArrayOfObjects(
common.possibleMoves(a, b)
),
[null, [a, b]]
]
);
highlightCells(common.possibleMoves(a, b), [a, b]);
highlighting.a = a;
highlighting.b = b;
} else {
@ -154,7 +171,6 @@ const drawBoard = (side) => {
celldiv.classList.add(`cc${j.toString()}`);
celldiv.classList.add(`celldiv`);
celldiv.innerText = common.board[i][j];
celldiv.addEventListener(`click`, cellClick);
celldiv.a = i.toString();
celldiv.b = j.toString();
cell.appendChild(celldiv);
@ -175,8 +191,6 @@ drawBoard(common.side);
//common.cellClick(3, 4);
cellClick({target: {a: 7, b: 4}});
document.querySelector(`#title`).addEventListener(`click`, () => {
fetch("/post", {
method: "POST",

View File

@ -415,8 +415,8 @@ export const writePiece = (a, b, piece, client=false) => {
if (client) document.querySelectorAll(`.cr${a}.cc${b}`)[0].innerText = piece;
}
export const possibleMoves = (a, b) => {
const targetPiece = board[a][b];
export const possibleMoves = (a, b, hypotheticalBoard=deepCopy(board)) => {
const targetPiece = hypotheticalBoard[a][b];
if (targetPiece == NP) return [];
const moves = [];
// example of moves:
@ -440,40 +440,40 @@ export const possibleMoves = (a, b) => {
switch (targetPiece) {
case WP:
pawnMoves(a, b, moves, blacks, board, true);
pawnMoves(a, b, moves, blacks, hypotheticalBoard, true);
break;
case WR:
rbqMoves(a, b, moves, blacks, board, rookRelativePositions, true);
rbqMoves(a, b, moves, blacks, hypotheticalBoard, rookRelativePositions, true);
break;
case WN:
knMoves(a, b, moves, [...blacks, NP], board, knightRelativePositions);
knMoves(a, b, moves, [...blacks, NP], hypotheticalBoard, knightRelativePositions);
break;
case WB:
rbqMoves(a, b, moves, blacks, board, bishopRelativePositions, true);
rbqMoves(a, b, moves, blacks, hypotheticalBoard, bishopRelativePositions, true);
break;
case WQ:
rbqMoves(a, b, moves, blacks, board, queenRelativePositions, true);
rbqMoves(a, b, moves, blacks, hypotheticalBoard, queenRelativePositions, true);
break;
case WK:
knMoves(a, b, moves, [...blacks, NP], board, kingRelativePositions);
knMoves(a, b, moves, [...blacks, NP], hypotheticalBoard, kingRelativePositions);
break;
case BP:
pawnMoves(a, b, moves, whites, board, false);
pawnMoves(a, b, moves, whites, hypotheticalBoard, false);
break;
case BR:
rbqMoves(a, b, moves, whites, board, rookRelativePositions, true);
rbqMoves(a, b, moves, whites, hypotheticalBoard, rookRelativePositions, true);
break;
case BN:
knMoves(a, b, moves, [...whites, NP], board, knightRelativePositions);
knMoves(a, b, moves, [...whites, NP], hypotheticalBoard, knightRelativePositions);
break;
case BB:
rbqMoves(a, b, moves, whites, board, bishopRelativePositions, true);
rbqMoves(a, b, moves, whites, hypotheticalBoard, bishopRelativePositions, true);
break;
case BQ:
rbqMoves(a, b, moves, whites, board, queenRelativePositions, true);
rbqMoves(a, b, moves, whites, hypotheticalBoard, queenRelativePositions, true);
break;
case BK:
knMoves(a, b, moves, [...whites, NP], board, kingRelativePositions);
knMoves(a, b, moves, [...whites, NP], hypotheticalBoard, kingRelativePositions);
break;
}
moves.filter(
@ -495,17 +495,26 @@ export const getMainMovesFromArrayOfObjects = (array) => {
return newArray;
}
export const movePiece = (move, player) => {
export const movePiece = (move, player, hypotheticalBoard=deepCopy(board), hypotheticalCurrentKingPosition=JSON.parse(JSON.stringify(currentKingPosition)), players=null, id=null, hypotheticalHasMoved=hasMoved) => {
const playerIsWhite = (
player == `white` ||
player == true
);
const pieces = (playerIsWhite ? hasMoved.white : hasMoved.black);
const pieces = (playerIsWhite ? hypotheticalHasMoved.white : hypotheticalHasMoved.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;
else canEnPassent = null;
if (hypotheticalBoard[fromA][fromB] == WK) hypotheticalCurrentKingPosition.white = [toA, toB];
if (hypotheticalBoard[fromA][fromB] == BK) hypotheticalCurrentKingPosition.black = [toA, toB];
board = moved(move, board);
return {
board: moved(move, hypotheticalBoard),
players: players,
currentKingPosition: hypotheticalCurrentKingPosition,
hasMoved: hypotheticalHasMoved,
id: id,
}
}

View File

@ -85,6 +85,7 @@ const createGame = () => {
white: null,
black: null,
},
currentKingPosition: JSON.parse(JSON.stringify(common.currentKingPosition)),
hasMoved: JSON.parse(JSON.stringify(common.hasMoved)),
id: id,
});
@ -97,20 +98,28 @@ const handleIncomingMove = (request, id) => {
console.log(`Unparseable move: ${request.move}`);
return;
}
if (common.checkForCheck(true, common.moved(request.move))) {
console.log(`Provided invalid move: it results in check`);
clients.get(id).send(JSON.stringify({
type: `reject`,
reject: `check`,
}));
return;
}
if (!inGame.has(id)) {
console.log(`Not in game!`);
clients.get(id).send(JSON.stringify({
type: `move`,
result: `reject`,
reject: `not in game`,
}))
return;
}
if (!(common.possibleMoves(...request.move.main[0]).includes(request.move))) {
console.log(`Invalid move!`);
clients.get(id).send(JSON.stringify({
type: `move`,
result: `reject`,
reject: `invalid`,
}))
}
const gameID = inGame.get(id);
const thisGame = games.get(gameID);
games.set(gameID, common.movePiece(request.move, id == thisGame.players.white, thisGame.board, thisGame.currentKingPosition, players, id, thisGame.hasMoved));
}
const startGame = (gameID) => {
@ -139,9 +148,10 @@ const handleIncomingJoin = (request, id) => {
console.log(`Recieved invalid join request: no name found`);
return;
}
if (Array.from(usernames.values).map( x => x[1] ).includes(request.username.toString())) {
console.log(Array.from(usernames.values()));
if (Array.from(usernames.values()).includes(request.username.toString())) {
console.log(`Recieved username that already exists.`);
request.send(JSON.stringify({
clients.get(id).send(JSON.stringify({
type: `reject`,
reject: `username`,
}));
@ -181,7 +191,6 @@ const handleIncomingJoin = (request, id) => {
}
clients.get(id).send(JSON.stringify({
type: `accept`,
username: request.username,
}));
}