Created
July 4, 2017 17:23
-
-
Save byanofsky/ebf3cca6896fa2468dc091a97d00ac39 to your computer and use it in GitHub Desktop.
Calculates the best move looking one move ahead
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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