I made a self-learning AI to play Tic Tac Toe!

AI (Artificial Intelligence) and blockchain technology are in my opinion virtually indissoluble. I have already seen so many extraordinary projects that use machine learning and neural networks in order to achieve the biggest efficiency and performance, for instance NOIA or Golem.

Additionally, AI is widely used to improve the performance of devices surrounding us. Traffic lights, personal assistants, autonomous cars or recommendations are just some of the areas where we can meet AI every day.

Being fascinated about endless possibilities of this technology in revolutionizing everyday life and blockchain I decided to use my humble IT skills and develop my own AI. At the beginning of this exciting path I decided not to use complex mathematical formulas and algorithms (even though I am aware that they are the most efficient), but to do it on my own, not so complicated way.

tic tac toe.png

What was my goal?

I sometimes enjoy playing Tic Tac Toe, so I wanted to create a worthy opponent for me :) There are already plenty of algorithms that makes the Tic Tac Toe AI virtually unbeatable, for instance the Minimax algorithm. However, they are based on input provided by a human, which means there is said: "in this situation, this decision will have the best impact on your probability of the victory". I wanted to avoid such approach and create something similar to machine learning.

How can the program learn itself?

Firstly, as AI has no input provided by human basing on which it would be making decisions it needs to collect enough data about the opponent. AI has this advantage that it can learn itself virtually endlessly, as it does not get bored or exhausted. Therefore my idea was simple: let the AI play 10,000 times with itself using a random made decisions (it took about 30 minutes, even for a computer). Every time circles win, the state of board after every move in this particular game is saved to the file.

Thanks to this, AI has acquired a file with plenty of patterns that usually lead to the victory of circles. However, still the biggest challenge was to interpret these patterns properly as I have met several problems:

  • how to avoid repetitiousness in the game
  • what if there are more patterns that are matching current situation on the board
  • what if there are no patterns that are matching current situation on the board

My solution: every time program has to make a decision, it analyses every saved pattern and compares it to the current situation on the board. Usually there are several patterns with the same level of similarity, so one of them is chosen randomly. If none is applicable, the move is executed completely randomly.

Results

For me results are pretty satisfying as the probability of victory with the randomly choosing opponent has been increased by 100%. Here are some games that AI played with me (I play with lines):

I am sure that is not the end of my journey with AI as I am strongly determined to develop a true neural network and I will certainly share the results of my work with you. Additionally I am currently preparing an article about benefits and risks of AI, so stay tuned :)

Here is a Github repository with source code for anyone interested.

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now