HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Minimax for Tic-Tac-Toe

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
minimaxtoetictacfor

Problem

I have written AI for Tic-Tac-Toe using Negamax in JavaScript. The Negamax object is translated from an implementation written in Python. The code runs on a Node.js server and the player interacts with it via web socket. The code works and the game is unbeatable meaning it always wins if it can and if it can't, will force a tie.

I am looking for advice on how to optimize this implementation of MiniMax. I am also fairly new to Node.js and am wondering if this will run in a non-blocking way (assuming an instance of the object for each connected user.

Minimax Object:

```
var MiniMax = function(){
//init values and options
this.bestMove = 0;
this.MAX_DEPTH = 6;
}

MiniMax.prototype = {
//function called from game, bestmove will return the computer move
buildTree: function(board, player, cb){
this.bestMove = 0;
var alpha = this.buildTree_r(board, player, 0);
cb(this.bestMove);
},
//recursive function to build minimax tree and rate the value of the board
buildTree_r: function(board, currPlayer, depth){
if(depth > this.MAX_DEPTH){
return 0;
}
//Set the otherplayer for the next game state and to check for loss
var otherPlayer;
if(currPlayer == board.X){
otherPlayer = board.O;
} else {
otherPlayer = board.X;
}
//check for a winner in the boardstate, if currPlayer we win, else we lose in this tree
var winner = board.getWinner();
if(winner == currPlayer){
return 1;
} else if(winner == otherPlayer){
return -1;
}
//check for a full board and therefore cats game in this true
if(board.isFull()){
return 0;
}
//this is where we begin to rank moves, get an array of moves, set alpha low, instantiate parallel
//subAlpha list to movelist to remember move ranks
var moveList = board.getMoves();
var alpha = -1;
var saList = [];
for(var i=0; i<moveList.length; i++){

var boardCopy = board.copy(); //Copy current gamestate
boardCop

Solution

This would be a good place to use a ternary comparison operator:

var otherPlayer;
if(currPlayer == board.X){
  otherPlayer = board.O;
} else {
  otherPlayer = board.X;
}


Then it would become a single line:

var otherPlayer = currPlayer == board.X ? board.O : board.X;


You could do this in a few other places as well.

for(var i=0; i<moveList.length; i++){


You use spaces around your operators for the most part, you should be consistent with this.

var b = new Board();


In the Minimax object code, you use good variables names. Not so much in the Board object code.

Otherwise, this looks good to me, very clean, neat, and well documented.

Code Snippets

var otherPlayer;
if(currPlayer == board.X){
  otherPlayer = board.O;
} else {
  otherPlayer = board.X;
}
var otherPlayer = currPlayer == board.X ? board.O : board.X;
for(var i=0; i<moveList.length; i++){
var b = new Board();

Context

StackExchange Code Review Q#81988, answer score: 5

Revisions (0)

No revisions yet.