Skip to content

Instantly share code, notes, and snippets.

@byanofsky
Created July 4, 2017 17:23
Show Gist options
  • Save byanofsky/ebf3cca6896fa2468dc091a97d00ac39 to your computer and use it in GitHub Desktop.
Save byanofsky/ebf3cca6896fa2468dc091a97d00ac39 to your computer and use it in GitHub Desktop.
Calculates the best move looking one move ahead
var calcBestMoveOne = function(playerColor) {
// List all possible moves
var possibleMoves = game.moves();
// Sort moves randomly, so the same move isn't always picked on ties
possibleMoves.sort(function(a, b){return 0.5 - Math.random()});
// exit if the game is over
if (game.game_over() === true || possibleMoves.length === 0) return;
// Search for move with highest value
var bestMoveSoFar = null;
var bestMoveValue = Number.NEGATIVE_INFINITY;
possibleMoves.forEach(function(move) {
game.move(move);
var moveValue = evaluateBoard(game.board(), playerColor);
if (moveValue > bestMoveValue) {
bestMoveSoFar = move;
bestMoveValue = moveValue;
}
game.undo();
});
return bestMoveSoFar;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment