File tree Expand file tree Collapse file tree 1 file changed +13
-0
lines changed
Expand file tree Collapse file tree 1 file changed +13
-0
lines changed Original file line number Diff line number Diff line change @@ -25,6 +25,7 @@ let currentPlayer = PLAYERS.ONE;
2525const moveHistory = [ ] ;
2626const grid = document . getElementById ( 'grid' ) ;
2727let 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
122123const 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) => {
284288const isAITurn = ( ) => currentPlayer === PLAYERS . TWO ;
285289
286290const 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} ;
You can’t perform that action at this time.
0 commit comments