Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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