For those who are struggling with Tic-Tac-Toe, here is a simple but effective implementation of the MiniMax algorithm:
If you look carefully in the javascript, a board is basically an array made up of objects, and individual cells are those objects.
That means that whatever happens to the board in the miniMax function, gets passed back to the calling function, in this case, the makeMove function, because objects are passed to functions by reference, not by value.
Within the minimax function, when it iterates through each possible move, it creates a new board, called simBoard (simulated Board) and passes that to the recursive miniMax function. When the script is finished recursing, the original board makes it’s move based on the maximum score each empty move could possibly produce. If it’s the player’s turn, it uses the minimum possible score each empty move could possible produce.
Any questions or further explaination needed, please feel free to reach out!
Thanks,
James