Skip to content

Commit a84e032

Browse files
committed
Disable click when AI is playing
1 parent 6c26dff commit a84e032

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ let currentPlayer = PLAYERS.ONE;
2525
const moveHistory = [];
2626
const grid = document.getElementById('grid');
2727
let aiEnabled = true; // Default to AI mode instead of two human players
28+
let aiIsPlaying = false; // New flag to track when AI is making a move
2829

2930
/** Helper Functions **/
3031

@@ -120,6 +121,9 @@ const createBoard = () => {
120121
/** Event Handlers **/
121122

122123
const handleClick = e => {
124+
// Ignore clicks when AI is playing
125+
if (aiIsPlaying) return;
126+
123127
const cell = e.currentTarget;
124128
const piece = cell.querySelector(`.${CLASSES.PIECE}`);
125129
if (selectedPiece) {
@@ -284,6 +288,9 @@ const movePiece = (from, toCell) => {
284288
const isAITurn = () => currentPlayer === PLAYERS.TWO;
285289

286290
const executeAIMove = () => {
291+
// Set the flag to indicate AI is playing
292+
aiIsPlaying = true;
293+
287294
// Give a small delay so the player can see what's happening
288295
setTimeout(() => {
289296
const bestMove = findBestMove();
@@ -295,7 +302,13 @@ const executeAIMove = () => {
295302
setTimeout(() => {
296303
const targetCell = getCell(bestMove.toRow, bestMove.toCol);
297304
attemptMove(targetCell);
305+
306+
// Clear the flag when AI move is complete
307+
aiIsPlaying = false;
298308
}, 500);
309+
} else {
310+
// Clear the flag if no move is found
311+
aiIsPlaying = false;
299312
}
300313
}, 1000);
301314
};

0 commit comments

Comments
 (0)