mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-17 21:40:51 +02:00
Fix tic-tac-toe
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
const Command = require('../../framework/Command');
|
const Command = require('../../framework/Command');
|
||||||
const tictactoe = require('tictactoe-minimax-ai');
|
const { ComputerMove } = require('tictactoe-minimax-ai');
|
||||||
const { stripIndents } = require('common-tags');
|
const { stripIndents } = require('common-tags');
|
||||||
const { verify } = require('../../util/Util');
|
const { verify } = require('../../util/Util');
|
||||||
const nums = ['1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣', '6️⃣', '7️⃣', '8️⃣', '9️⃣'];
|
const nums = ['1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣', '6️⃣', '7️⃣', '8️⃣', '9️⃣'];
|
||||||
@@ -47,7 +47,7 @@ module.exports = class TicTacToeCommand extends Command {
|
|||||||
const sign = userTurn ? 'X' : 'O';
|
const sign = userTurn ? 'X' : 'O';
|
||||||
let choice;
|
let choice;
|
||||||
if (opponent.bot && !userTurn) {
|
if (opponent.bot && !userTurn) {
|
||||||
choice = tictactoe.bestMove(this.convertBoard(sides), { computer: 'o', opponent: 'x' });
|
choice = ComputerMove(this.convertBoard(sides), { aiPlayer: 'o', huPlayer: 'x' }, 'Hard');
|
||||||
} else {
|
} else {
|
||||||
await msg.say(stripIndents`
|
await msg.say(stripIndents`
|
||||||
${user}, which side do you pick? Type \`end\` to forfeit.
|
${user}, which side do you pick? Type \`end\` to forfeit.
|
||||||
@@ -102,26 +102,38 @@ module.exports = class TicTacToeCommand extends Command {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
verifyWin(sides, player1, player2) {
|
playerWon(board, player) {
|
||||||
const evaluated = tictactoe.boardEvaluate(this.convertBoard(sides)).status;
|
if (
|
||||||
if (evaluated === 'win') return player1;
|
(board[0] === player && board[1] === player && board[2] === player) ||
|
||||||
if (evaluated === 'loss') return player2;
|
(board[3] === player && board[4] === player && board[5] === player) ||
|
||||||
if (evaluated === 'tie') return 'tie';
|
(board[6] === player && board[7] === player && board[8] === player) ||
|
||||||
|
(board[0] === player && board[3] === player && board[6] === player) ||
|
||||||
|
(board[1] === player && board[4] === player && board[7] === player) ||
|
||||||
|
(board[2] === player && board[5] === player && board[8] === player) ||
|
||||||
|
(board[0] === player && board[4] === player && board[8] === player) ||
|
||||||
|
(board[2] === player && board[4] === player && board[6] === player)
|
||||||
|
) return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
verifyWin(board, player1, player2) {
|
||||||
|
if (this.playerWon(board, player1)) return player1;
|
||||||
|
if (this.playerWon(board, player2)) return player2;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
convertBoard(board) {
|
convertBoard(board) {
|
||||||
const newBoard = [[], [], []];
|
const newBoard = [];
|
||||||
let col = 0;
|
let col = 0;
|
||||||
for (const piece of board) {
|
for (const piece of board) {
|
||||||
if (piece === 'X') {
|
if (piece === 'X') {
|
||||||
newBoard[col].push('x');
|
newBoard.push('x');
|
||||||
} else if (piece === 'O') {
|
} else if (piece === 'O') {
|
||||||
newBoard[col].push('o');
|
newBoard.push('o');
|
||||||
} else {
|
} else {
|
||||||
newBoard[col].push('_');
|
newBoard.push('_');
|
||||||
}
|
}
|
||||||
if (newBoard[col].length === 3) col++;
|
if (newBoard.length === 3) col++;
|
||||||
}
|
}
|
||||||
return newBoard;
|
return newBoard;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user