Tic Tac Toe: Basic AI

Trying to implement a simple AI under the singlePlayerHard function. I’ve been looking at minmax and other alternatives but not sure where to begin. I want it to at least be able to block the player from scoring which should involve cycling through the wincombos array. Any ideas?

When using versus play again doesn’t work

It should work, it just refreshes the whole page.

Man, this is such a good challenge. I unfortunately followed a tutorial on how to implement the minimax algorithm (it actually has a bug in it so it’s not perfect anyways haha). I still learned a lot, but I decided I need to know more before continuing onto my next challenge.

Anyways, perhaps for your benefit, I understood that you can create an unbeatable AI by implementing like 300 if statements… doesn’t sound fun. Or you can check just a few conditions repeatedly. Essentially, you check if either the player wins, the AI wins, or its a tie, and assign a score based on those outcomes. If none of those outcomes happen, then you make a phantom move, ie you make a new board with a random move on it, and check for those end states. You keep doing this until you have an end state. Gather up all the possible moves, and the one with the highest score is the one you should implement. Sounds nice in theory, then when I tried to think of how I would code it, my brain exploded, so I followed FCC’s tutorial on youtube and I learned so much. Again still cant exactly debug it, but it was a good learning.

1 Like

Yeah, the last move was not taken into consideration