@@ -373,13 +373,13 @@ ${this.state.player2Username}: ${getEmojiFromSign(this.state.player2Sign)}
373
373
. setCustomId ( `connect4-4-${ this . id } ` )
374
374
. setLabel ( '4' )
375
375
. setStyle ( ButtonStyle . Secondary ) ,
376
- new ButtonBuilder ( )
377
- . setCustomId ( `connect4-5-${ this . id } ` )
378
- . setLabel ( '5' )
379
- . setStyle ( ButtonStyle . Secondary ) ,
380
376
) ;
381
377
382
378
const row2 = new ActionRowBuilder < ButtonBuilder > ( ) . addComponents (
379
+ new ButtonBuilder ( )
380
+ . setCustomId ( `connect4-5-${ this . id } ` )
381
+ . setLabel ( '5' )
382
+ . setStyle ( ButtonStyle . Secondary ) ,
383
383
new ButtonBuilder ( )
384
384
. setCustomId ( `connect4-6-${ this . id } ` )
385
385
. setLabel ( '6' )
@@ -515,7 +515,7 @@ ${this.state.player2Username}: ${getEmojiFromSign(this.state.player2Sign)}
515
515
let value = - Infinity ;
516
516
for ( const column_choice of column_choices ) {
517
517
if ( state . columns [ column_choice ] . fill < CONNECT_FOUR_ROW_COUNT ) {
518
- const newState : ConnectFourGameState = JSON . parse ( JSON . stringify ( state ) ) ;
518
+ const newState : ConnectFourGameState = JSON . parse ( JSON . stringify ( state ) ) ; // create a deep copy
519
519
this . updateState ( newState , column_choice , turn ) ;
520
520
this . setStatus ( newState , column_choice ) ; // setStatus assumes newState has already been updated with chip
521
521
value = Math . max ( value , this . miniMax ( newState , depth - 1 , ConnectFourGameSign . Player1 ) ) ;
@@ -553,6 +553,11 @@ ${this.state.player2Username}: ${getEmojiFromSign(this.state.player2Sign)}
553
553
const newState : ConnectFourGameState = JSON . parse ( JSON . stringify ( state ) ) ; // make a deep copy of game state
554
554
this . updateState ( newState , i , ConnectFourGameSign . Player2 ) ;
555
555
this . setStatus ( newState , i ) ;
556
+ // need to check if there is more than one possible guaranteed win
557
+ // in that case, if there exists an immediate win, we want the bot to choose that column
558
+ if ( newState . status === ConnectFourGameStatus . Player2Win ) {
559
+ return i ;
560
+ }
556
561
column_scores [ i ] = this . miniMax ( newState , 4 , ConnectFourGameSign . Player1 ) ;
557
562
}
558
563
}
0 commit comments