Skip to content

Instantly share code, notes, and snippets.

@ahmaxed
Last active February 4, 2017 10:10
Show Gist options
  • Save ahmaxed/d2a2ef869d34e01008ce6b530d641d78 to your computer and use it in GitHub Desktop.
Save ahmaxed/d2a2ef869d34e01008ce6b530d641d78 to your computer and use it in GitHub Desktop.
// 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