Tic Tac Toe AI

This commit is contained in:
Dragon Fire
2021-01-09 13:36:28 -05:00
parent dd6bd22444
commit 382609c5ef
2 changed files with 63 additions and 50 deletions
+61 -49
View File
@@ -1,4 +1,5 @@
const Command = require('../../structures/Command'); const Command = require('../../structures/Command');
const tictactoe = require('tictactoe-minimax-ai');
const { stripIndents } = require('common-tags'); const { stripIndents } = require('common-tags');
const { verify } = require('../../util/Util'); const { verify } = require('../../util/Util');
@@ -13,7 +14,7 @@ module.exports = class TicTacToeCommand extends Command {
args: [ args: [
{ {
key: 'opponent', key: 'opponent',
prompt: 'What user would you like to challenge?', prompt: 'What user would you like to challenge? To play against AI, choose me.',
type: 'user' type: 'user'
} }
] ]
@@ -21,17 +22,18 @@ module.exports = class TicTacToeCommand extends Command {
} }
async run(msg, { opponent }) { async run(msg, { opponent }) {
if (opponent.bot) return msg.reply('Bots may not be played against.');
if (opponent.id === msg.author.id) return msg.reply('You may not play against yourself.'); if (opponent.id === msg.author.id) return msg.reply('You may not play against yourself.');
const current = this.client.games.get(msg.channel.id); const current = this.client.games.get(msg.channel.id);
if (current) return msg.reply(`Please wait until the current game of \`${current.name}\` is finished.`); if (current) return msg.reply(`Please wait until the current game of \`${current.name}\` is finished.`);
this.client.games.set(msg.channel.id, { name: this.name }); this.client.games.set(msg.channel.id, { name: this.name });
try { try {
await msg.say(`${opponent}, do you accept this challenge?`); if (!opponent.bot) {
const verification = await verify(msg.channel, opponent); await msg.say(`${opponent}, do you accept this challenge?`);
if (!verification) { const verification = await verify(msg.channel, opponent);
this.client.games.delete(msg.channel.id); if (!verification) {
return msg.say('Looks like they declined...'); this.client.games.delete(msg.channel.id);
return msg.say('Looks like they declined...');
}
} }
const sides = ['1', '2', '3', '4', '5', '6', '7', '8', '9']; const sides = ['1', '2', '3', '4', '5', '6', '7', '8', '9'];
const taken = []; const taken = [];
@@ -41,43 +43,48 @@ module.exports = class TicTacToeCommand extends Command {
while (!winner && taken.length < 9) { while (!winner && taken.length < 9) {
const user = userTurn ? msg.author : opponent; const user = userTurn ? msg.author : opponent;
const sign = userTurn ? 'X' : 'O'; const sign = userTurn ? 'X' : 'O';
await msg.say(stripIndents` let choice;
${user}, which side do you pick? Type \`end\` to forefeit. if (opponent.bot && !userTurn) {
\`\`\` choice = tictactoe.bestMove(this.convertBoard(sides), { computer: 'o', opponent: 'x' });
${sides[0]} | ${sides[1]} | ${sides[2]} } else {
————————— await msg.say(stripIndents`
${sides[3]} | ${sides[4]} | ${sides[5]} ${user}, which side do you pick? Type \`end\` to forefeit.
————————— \`\`\`
${sides[6]} | ${sides[7]} | ${sides[8]} ${sides[0]} | ${sides[1]} | ${sides[2]}
\`\`\` —————————
`); ${sides[3]} | ${sides[4]} | ${sides[5]}
const filter = res => { —————————
if (res.author.id !== user.id) return false; ${sides[6]} | ${sides[7]} | ${sides[8]}
const choice = res.content; \`\`\`
if (choice.toLowerCase() === 'end') return true; `);
return sides.includes(choice) && !taken.includes(choice); const filter = res => {
}; if (res.author.id !== user.id) return false;
const turn = await msg.channel.awaitMessages(filter, { const choice = res.content;
max: 1, if (choice.toLowerCase() === 'end') return true;
time: 30000 return sides.includes(choice) && !taken.includes(choice);
}); };
if (!turn.size) { const turn = await msg.channel.awaitMessages(filter, {
await msg.say('Sorry, time is up!'); max: 1,
if (lastTurnTimeout) { time: 30000
winner = 'time'; });
if (!turn.size) {
await msg.say('Sorry, time is up!');
if (lastTurnTimeout) {
winner = 'time';
break;
} else {
userTurn = !userTurn;
lastTurnTimeout = true;
continue;
}
}
choice = turn.first().content;
if (choice.toLowerCase() === 'end') {
winner = userTurn ? opponent : msg.author;
break; break;
} else {
userTurn = !userTurn;
lastTurnTimeout = true;
continue;
} }
} }
const choice = turn.first().content; sides[opponent.bot && !userTurn ? choice : Number.parseInt(choice, 10) - 1] = sign;
if (choice.toLowerCase() === 'end') {
winner = userTurn ? opponent : msg.author;
break;
}
sides[Number.parseInt(choice, 10) - 1] = sign;
taken.push(choice); taken.push(choice);
if (this.verifyWin(sides)) winner = userTurn ? msg.author : opponent; if (this.verifyWin(sides)) winner = userTurn ? msg.author : opponent;
if (lastTurnTimeout) lastTurnTimeout = false; if (lastTurnTimeout) lastTurnTimeout = false;
@@ -93,13 +100,18 @@ module.exports = class TicTacToeCommand extends Command {
} }
verifyWin(sides) { verifyWin(sides) {
return (sides[0] === sides[1] && sides[0] === sides[2]) const evaluated = tictactoe.boardEvaluate(this.convertBoard(sides), { computer: 'o', opponent: 'x '});
|| (sides[0] === sides[3] && sides[0] === sides[6]) if (evaluated === 'win' || evaluated === 'loss' || evaluated === 'tie') return true;
|| (sides[3] === sides[4] && sides[3] === sides[5]) return false;
|| (sides[1] === sides[4] && sides[1] === sides[7]) }
|| (sides[6] === sides[7] && sides[6] === sides[8])
|| (sides[2] === sides[5] && sides[2] === sides[8]) convertBoard(board) {
|| (sides[0] === sides[4] && sides[0] === sides[8]) const newBoard = [];
|| (sides[2] === sides[4] && sides[2] === sides[6]); for (const piece of board) {
if (piece === 'X') newBoard.push('x');
if (piece === 'O') newBoard.push('o');
newBoard.push('_');
}
return newBoard;
} }
}; };
+2 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "xiao", "name": "xiao",
"version": "124.5.2", "version": "124.5.3",
"description": "Your personal server companion.", "description": "Your personal server companion.",
"main": "Xiao.js", "main": "Xiao.js",
"scripts": { "scripts": {
@@ -64,6 +64,7 @@
"sherlockjs": "^1.4.0", "sherlockjs": "^1.4.0",
"stackblur-canvas": "^2.4.0", "stackblur-canvas": "^2.4.0",
"tesseract.js": "^2.1.4", "tesseract.js": "^2.1.4",
"tictactoe-minimax-ai": "^1.2.1",
"winston": "^3.3.3" "winston": "^3.3.3"
}, },
"optionalDependencies": { "optionalDependencies": {