Skip to content

Instantly share code, notes, and snippets.

@lhartikk
Last active March 30, 2017 15:49
Show Gist options
  • Save lhartikk/7a4eeaa91f874ab3b1468298320c5624 to your computer and use it in GitHub Desktop.
Save lhartikk/7a4eeaa91f874ab3b1468298320c5624 to your computer and use it in GitHub Desktop.
var minimax = function (depth, game, isMaximisingPlayer) {
if (depth === 0) {
return -evaluateBoard(game.board());
}
var newGameMoves = game.ugly_moves();
if (isMaximisingPlayer) {
var bestMove = -9999;
for (var i = 0; i < newGameMoves.length; i++) {
game.ugly_move(newGameMoves[i]);
bestMove = Math.max(bestMove, minimax(depth - 1, game, !isMaximisingPlayer));
game.undo();
}
return bestMove;
} else {
var bestMove = 9999;
for (var i = 0; i < newGameMoves.length; i++) {
game.ugly_move(newGameMoves[i]);
bestMove = Math.min(bestMove, minimax(depth - 1, game, !isMaximisingPlayer));
game.undo();
}
return bestMove;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment