Last active
February 4, 2017 10:10
-
-
Save ahmaxed/d2a2ef869d34e01008ce6b530d641d78 to your computer and use it in GitHub Desktop.
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
// if it is the computer's turn loop over the moves and choose the move with the highest score | |
var bestMove; | |
if(player === aiPlayer){ | |
var bestScore = -10000; | |
for(var i = 0; i < moves.length; i++){ | |
if(moves[i].score > bestScore){ | |
bestScore = moves[i].score; | |
bestMove = i; | |
} | |
} | |
}else{ | |
// else loop over the moves and choose the move with the lowest score | |
var bestScore = 10000; | |
for(var i = 0; i < moves.length; i++){ | |
if(moves[i].score < bestScore){ | |
bestScore = moves[i].score; | |
bestMove = i; | |
} | |
} | |
} | |
// return the chosen move (object) from the moves array | |
return moves[bestMove]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment