Solved – Tic Tac Toe AI with Machine Learning

algorithmsgamesmachine learningreinforcement learning

I am new to machine learning and as one of my first projects I decided to make a tic tac toe bot. What I've done so far is a rgb/hex color recognition with the naive bayes algorithm, which was really simple to make. Now I am struggling a bit with making a tic tac toe bot.

My question is how should I proceed with this, is there a dataset, and should I take naive bayes again, or decision tree would be better for tic tac toe? (once again, purpose is just that I learn and dive deeper in machine learning)

Best Answer

Presumably you don't want an AI which looks ahead a few moves and brute-forces the best move. I guess you want an AI which will evaluate the strength of each possible move and choose the best.

One way you can approach this is to train an AI to take an input of the board and an input of where to play next and output a probability that this move will lead to a win.

You can create your own data by playing this AI against itself or against a player which plays randomly. This is more involved than using a dataset with the best moves listed for many positions, it's an option if you can't find such a dataset or if you want a challenge.

One possible way to create your own data and use it to iteratively improve the AI is the following:

  1. Let the AI play a few moves and then pause the game
  2. Select a random move to play (random allows the AI to learn from moves it wouldn't normally make)
  3. Record the state of the game and the new move
  4. Let the AI finish the game and record the result

This approach will create game data with many positions and many actions taken with the expected win/loss/draw result. You can use this data to train an AI to predict the result of the game if a given move is played. Repeat this training cycle to iteratively improve the AI.

Related Question