Tic Tac Toe game implementation with minimax decision algorithm. Single player game in which opponent will be an AI. AI will use minimax algorithm for making decision by traversing all possible moves ahead of current of his opponent.
While in Multi player game opponent won't be an AI.
Game class will expose various methods to play game.
Get instance of game
// single player
var g = game();
// multi player
var g = game(2);
Board placing
/**
* board placing =
* x/y | y0 | y1 | y2 |
* --------------------------
* x0 | x0y0 | x0y1 | x0y2 |
* x1 | x1y0 | x1y1 | x1y2 |
* x2 | x2y0 | x2y1 | x2y2 |
*/
this.board = [
[null, null, null],
[null, null, null],
[null, null, null]
];
Some of available methods
- player turn
turn
. return true: first player turn, false: second player/AI turn depends on game is single player or multi player
// player turn
g.turn;
- player making move
makeMove(x, y)
where x and y is player move position
// e.g.
g.makeMove(0, 1);
// result
this.board = [
[null, true, null],
[null, null, null],
[null, null, null]
];
winner()
get winner.
g.winner();
/* result
* 0 : player 1 wins
* 1 : player 2 wins / AI wins
* -1 : tie
*/
minimax(player)
, Minimax is used for AI for making decision for next move for AI.player
will befalse
in case of AI's move.
g.minimax(false);
For more other utility methods - tic-tac-toe-minimax