Real A.I. TicTacToe Bot in Node.js

Giacomo Fava
4 min readOct 25, 2018

--

A Machine Learning based bot with no hand-hard-coded rules of the game.

It’s a trap!

Premise.

Before starting to talk about my bot let me explain why the word REAL in the title. I’ve been reading different posts regarding unbeatable AI tictactoe bots (like this or this) where there is no AI at all.

There is no AI or Machine Learning in a pc that computes all the possible moves and chooses the best one on specific rules hard-coded by its creator. It’s like try to beat a calculator in a multiplications contest.

So I simply asked myself:

What if we simply let the bot figure out the game by making it learn from looking at how humans play?

(have you ever heard of Google’s AlphaGo?)

You can play against my bot by clicking here. (Try not to do always the same moves, it won’t generate different results. Different matches will help the bot learn and grow better)

The Machine Learning part.

I trained a Machine Learning Classifier based bot without having to hard-code a single rule of the game. The bot does not know that it has to put 3 elements inline, it just predict what a human player would have done and does it.

I’ve choosen to use a Decision Tree (why not a Random Forest? matter of time, it will be the next step) and trained it with every move of some real games between humans (I’ve been playing vs myself for 20 minutes, what a thrilling match!). The training set is composed by the snapshots of the board and the next move chosen by the player.

Me vs. myself part. 37

In this way when the bot has to choose the move to make, the Decision Tree predicts what a human would have done in that situation and does it. This obviously does not make the bot unbeatable (unless I enrich the training set with tons of games between tictactoe champions, if you are one of them comment here and let me know it).

Every move played by the human against the bot is also saved in the training set so that everyday the classifier can re-train himself and do a Reinforcement Learning.

998 moves later, he trapped me

EDIT: the Random Forest Classificator hasn’t improved that much the accuracy and it takes more training time then the classic Decision Tree classifier. So I’m still using the DT.

The Back-End part.

The project is hosted on Google Cloud Platform, I’ve been using a Realtime Database to store the training set. To build the bot I developed 3 simple API with a Node.js server:

  • /saveThisMove. Every move played by the user is submitted and stored in the db.
  • /askNextMove. The snapshot of the game is submitted in order to receive the prediction of the classifier.
  • /reTrainMe. Triggers the train function that re-trains the model on the new data.

Machine Learning in Javascript.

I’ve found some interesting projects like this or this but if you want to create a ML classifier in Javascript you may need to get your hands dirty.

Tensorflow.js is a great tool but it does not have all the feature its Python brother has. Furthermore TensorFlow.js is mainly focused on deep-learning and neural networks and it has no ML classifiers (’til now). In a lot of blog posts I’ve been reading about MLjs but there are still some open issues and it does not always work properly. Do you know some other similar libraries?

Next Step.

Obviously properly populate the training database, we all know that just some games between me and myself cannot be enough to have a worthy opponent.

In the next days I’ll surely try to implement a Random Forest classifier instead of a Decision Tree so as to improve the results.

EDIT: new classifiers are coming to this bot, stay tuned.

When Skynet will take over the world, I hope they’ll remember i was the one who taught them how to play tictactoe.

You can play against my bot by clicking here. (Try not to do always the same moves, it won’t generate different results. Different matches will help the bot learn and grow better)

Giacomo Fava. Biomedical Engineer. AI Enthusiast.

LinkedinInstagram

--

--

Giacomo Fava

AI Engineer. Mi occupo di disegnare modelli ML all’interno di infrastrutture web e mi piace il rock che viene dal Regno Unito.