Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 18 additions & 27 deletions chess.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ document.addEventListener("DOMContentLoaded", () => {
square.style.display = "flex";
square.style.alignItems = "center";
square.style.justifyContent = "center";
square.style.position = "relative"; // Allow absolute dots
square.style.backgroundColor = (row + col) % 2 === 0 ? "#f0d9b5" : "#b58863";

// 🔹 **Add row and column data for movement logic**
Expand All @@ -65,12 +66,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);
}

Expand Down Expand Up @@ -104,14 +107,8 @@ document.addEventListener("DOMContentLoaded", () => {
movePiece(square);
removeMoveDots();
selectedPiece = null;
checkForCheck(); // Ensure this line is here
if (isCheckmate()) {
displayCheckmatePopup();
} else {
switchTurn();
if (gameMode === "onePlayer" && turn === "b") {
setTimeout(botMove, 500); // Bot moves automatically after white
}
if (gameMode === "onePlayer" && turn === "b") {
setTimeout(botMove, 500);
}
}
} else if (piece && piece.dataset.color === selectedPiece.dataset.color) {
Expand Down Expand Up @@ -162,7 +159,6 @@ document.addEventListener("DOMContentLoaded", () => {
if (isCheckmate()) {
displayCheckmatePopup();
} else {
console.log("Calling switchTurn() from movePiece()"); // Debugging log
switchTurn();
}
}
Expand Down Expand Up @@ -275,11 +271,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');
Expand All @@ -302,7 +296,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);
};

Expand Down Expand Up @@ -425,7 +418,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`);
Expand All @@ -437,7 +432,7 @@ document.addEventListener("DOMContentLoaded", () => {
}
}
}
return true;
return inCheck;
};
const displayCheckmatePopup = () => {
const winner = turn === 'w' ? 'Black' : 'White';
Expand Down Expand Up @@ -538,20 +533,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');
Expand All @@ -570,16 +563,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
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
<div class="settings-container">
<label for="gameModeSelect">Game Mode:</label>
<select id="gameModeSelect" onchange="toggleBotSelection()">
<option value="2p">Two Players</option>
<option value="bot">One Player (vs. Bot)</option>
<option value="twoPlayer">Two Players</option>
<option value="onePlayer">One Player (vs. Bot)</option>
</select>
</div>

Expand Down