Skip to content

Commit a8a9b91

Browse files
committed
modified bot logic to immediately win when it has chance
1 parent 88c2e99 commit a8a9b91

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/components/games/connectFour.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -373,13 +373,13 @@ ${this.state.player2Username}: ${getEmojiFromSign(this.state.player2Sign)}
373373
.setCustomId(`connect4-4-${this.id}`)
374374
.setLabel('4')
375375
.setStyle(ButtonStyle.Secondary),
376-
new ButtonBuilder()
377-
.setCustomId(`connect4-5-${this.id}`)
378-
.setLabel('5')
379-
.setStyle(ButtonStyle.Secondary),
380376
);
381377

382378
const row2 = new ActionRowBuilder<ButtonBuilder>().addComponents(
379+
new ButtonBuilder()
380+
.setCustomId(`connect4-5-${this.id}`)
381+
.setLabel('5')
382+
.setStyle(ButtonStyle.Secondary),
383383
new ButtonBuilder()
384384
.setCustomId(`connect4-6-${this.id}`)
385385
.setLabel('6')
@@ -515,7 +515,7 @@ ${this.state.player2Username}: ${getEmojiFromSign(this.state.player2Sign)}
515515
let value = -Infinity;
516516
for (const column_choice of column_choices) {
517517
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
519519
this.updateState(newState, column_choice, turn);
520520
this.setStatus(newState, column_choice); // setStatus assumes newState has already been updated with chip
521521
value = Math.max(value, this.miniMax(newState, depth - 1, ConnectFourGameSign.Player1));
@@ -553,6 +553,11 @@ ${this.state.player2Username}: ${getEmojiFromSign(this.state.player2Sign)}
553553
const newState: ConnectFourGameState = JSON.parse(JSON.stringify(state)); // make a deep copy of game state
554554
this.updateState(newState, i, ConnectFourGameSign.Player2);
555555
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+
}
556561
column_scores[i] = this.miniMax(newState, 4, ConnectFourGameSign.Player1);
557562
}
558563
}

0 commit comments

Comments
 (0)