From 5a24af906eb3d40994c408f3ccc6fb7d98bb1fec Mon Sep 17 00:00:00 2001 From: egga22 <84204028+egga22@users.noreply.github.com> Date: Sat, 7 Jun 2025 21:13:38 -0500 Subject: [PATCH] fix checkmate color logic --- chess.js | 34 +++++++++++++++------------------- index.html | 4 ++-- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/chess.js b/chess.js index d827a4e..3bb1e09 100644 --- a/chess.js +++ b/chess.js @@ -65,12 +65,14 @@ document.addEventListener("DOMContentLoaded", () => { piece.src = `images/${initialBoard[row][col]}.svg`; piece.style.width = "70px"; piece.style.height = "70px"; - + // 🔹 **Add class and data attributes for movement logic** piece.classList.add("piece"); piece.dataset.color = initialBoard[row][col].includes("-w") ? "w" : "b"; piece.dataset.type = initialBoard[row][col].split("-")[0]; - + piece.dataset.moved = "false"; + piece.id = `piece${row}${col}`; + square.appendChild(piece); } @@ -162,7 +164,6 @@ document.addEventListener("DOMContentLoaded", () => { if (isCheckmate()) { displayCheckmatePopup(); } else { - console.log("Calling switchTurn() from movePiece()"); // Debugging log switchTurn(); } } @@ -275,11 +276,9 @@ document.addEventListener("DOMContentLoaded", () => { const botMove = () => { if (gameMode !== "onePlayer" || turn !== "b") { - console.log("Bot move skipped: gameMode =", gameMode, ", turn =", turn); // Debugging log return; } - - console.log("Bot move executing..."); // Debugging log + const pieces = Array.from(document.querySelectorAll('.piece')) .filter(p => p.dataset.color === 'b'); @@ -302,7 +301,6 @@ document.addEventListener("DOMContentLoaded", () => { } const randomMove = allMoves[Math.floor(Math.random() * allMoves.length)]; - console.log("Bot moving piece:", randomMove.piece.dataset.type, "to", randomMove.toRow, randomMove.toCol); // Debugging log movePieceToSquare(randomMove.piece, randomMove.toRow, randomMove.toCol); }; @@ -425,7 +423,9 @@ document.addEventListener("DOMContentLoaded", () => { } }; const isCheckmate = () => { - const color = turn; + const color = turn === 'w' ? 'b' : 'w'; + const boardCopy = createBoardCopy(); + const inCheck = isKingInCheck(boardCopy, color); for (let row = 0; row < 8; row++) { for (let col = 0; col < 8; col++) { const piece = document.querySelector(`[data-row='${row}'][data-col='${col}'] .piece`); @@ -437,7 +437,7 @@ document.addEventListener("DOMContentLoaded", () => { } } } - return true; + return inCheck; }; const displayCheckmatePopup = () => { const winner = turn === 'w' ? 'Black' : 'White'; @@ -538,20 +538,18 @@ document.addEventListener("DOMContentLoaded", () => { }; const switchTurn = () => { turn = turn === 'w' ? 'b' : 'w'; - console.log("Turn switched to:", turn); // Debugging log // If in one-player mode and it's Black's turn, make the bot move if (gameMode === "onePlayer" && turn === "b") { - console.log("Bot move triggered!"); // Debugging log setTimeout(botMove, 500); // Give a delay so it’s visually clear } }; - function toggleBotSelection() { + window.toggleBotSelection = function() { var gameMode = document.getElementById('gameModeSelect').value; var botSelection = document.getElementById('botSelection'); - botSelection.style.display = gameMode === 'bot' ? 'block' : 'none'; - } + botSelection.style.display = gameMode === 'onePlayer' ? 'block' : 'none'; + }; const promotePawn = (pawn) => { const promotionUI = document.createElement('div'); promotionUI.setAttribute('class', 'promotion-ui'); @@ -570,16 +568,14 @@ document.addEventListener("DOMContentLoaded", () => { pawn.src = `images/${type}-${color}.svg`; pawn.dataset.type = type; document.body.removeChild(document.querySelector('.promotion-ui')); - checkForCheck(); // Add this line - if (isCheckmate()) { // Add this block + checkForCheck(); + if (isCheckmate()) { displayCheckmatePopup(); } else { switchTurn(); } }; createBoard(); + toggleBotSelection(); }); - -//Comment -//extra secret comment diff --git a/index.html b/index.html index a0c3e40..a7c9d78 100644 --- a/index.html +++ b/index.html @@ -10,8 +10,8 @@