From cad3dba99198d46aad80c0e4823fb7a43b3d7c69 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Mon, 18 Jan 2021 20:10:11 -0500 Subject: [PATCH] Connect 4, if the other player misses a turn, play for them --- commands/games-mp/connect-four.js | 22 +++++++--------------- package.json | 2 +- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/commands/games-mp/connect-four.js b/commands/games-mp/connect-four.js index 5f6d08ba..d313a8c0 100644 --- a/commands/games-mp/connect-four.js +++ b/commands/games-mp/connect-four.js @@ -88,13 +88,11 @@ module.exports = class ConnectFourCommand extends Command { playerTwoEmoji = colors[choice] || choice; } } - let AIEngine = null; - if (opponent.bot) AIEngine = new Connect4AI(); + const AIEngine = new Connect4AI(); const board = this.generateBoard(); let userTurn = true; let winner = null; const colLevels = [5, 5, 5, 5, 5, 5, 5]; - let lastTurnTimeout = false; let lastMove = 'None'; while (!winner && board.some(row => row.includes(null))) { const user = userTurn ? msg.author : opponent; @@ -106,7 +104,7 @@ module.exports = class ConnectFourCommand extends Command { } else { await msg.say(stripIndents` ${user}, which column do you pick? Type \`end\` to forefeit. - ${AIEngine ? `I placed mine in **${lastMove}**.` : `Previous Move: **${lastMove}**`} + ${opponent.bot ? `I placed mine in **${lastMove}**.` : `Previous Move: **${lastMove}**`} ${this.displayBoard(board, playerOneEmoji, playerTwoEmoji)} ${nums.join('')} @@ -123,15 +121,10 @@ module.exports = class ConnectFourCommand extends Command { time: 60000 }); if (!turn.size) { - await msg.say('Sorry, time is up!'); - if (lastTurnTimeout) { - winner = 'time'; - break; - } else { - lastTurnTimeout = true; - userTurn = !userTurn; - continue; - } + await msg.say('Sorry, time is up! I\'ll pick their move for them.'); + AIEngine.playAI('hard'); + userTurn = !userTurn; + continue; } const choice = turn.first().content; if (choice.toLowerCase() === 'end') { @@ -139,13 +132,12 @@ module.exports = class ConnectFourCommand extends Command { break; } i = Number.parseInt(choice, 10) - 1; - if (AIEngine) AIEngine.play(i); + AIEngine.play(i); lastMove = i + 1; } board[colLevels[i]][i] = sign; colLevels[i]--; if (this.verifyWin(board)) winner = userTurn ? msg.author : opponent; - if (lastTurnTimeout) lastTurnTimeout = false; userTurn = !userTurn; } this.client.games.delete(msg.channel.id); diff --git a/package.json b/package.json index 49bc974c..1061d3c4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "126.6.6", + "version": "126.6.7", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {