Hey guys, welcome to today’s tutorial. Today we are going to build a cool Tic Tac Toe game with the Arduino and a touchscreen TFT display.

Arduino Tic Tac Toe game project

Tic Tac Toe is a game for two players, X and O, they take turns marking the spaces in a 3×3 grid usually drawn on paper. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row wins the game. In today’s tutorial, I will be showing us how to buildd the tic tac toe game using the Arduino and a touchscreen TFT display. The game just like the normal tic tac toe game will feature two players, but in this case, the second player will be the computer. I believe this game is a great introduction to game programming and Artificial Intelligence, even though we won’t be using any Artificial Intelligence Algorithms in this game, we will understand why Artificial Intelligence Algorithms are required in more complex games.

Required Components and Where to Buy

The following components are required to build this project and they can be bought via the link in front of them.

  1. Arduino Uno: https://educ8s.tv/part/ArduinoUno
  2. Touch Screen: https://educ8s.tv/part/TouchScreen28
  3. Power Bank: https://educ8s.tv/part/Powerbank

Full disclosure: All of the links above are affiliate links. I get a small percentage of each sale they generate. Thank you for your support!

[adsense]

Schematics

Connecting the components for this project is simple, the touchscreen TFT display used for this tutorial comes as a shield and can thus be easily mounted on the Arduino without the need for a breadboard or jumper wires.

The screen snaps on the Arduino as shown in the image below.

Connecting Screen to Arduino

 

Code

Our goal with the code is simple, we will be creating three screens. The first screen will carry the start game button which when pressed, will lead to the game itself. The second screen is the gameplay screen itself where the user will be playing with the computer. The gameplay is designed such that the Arduino plays first after which the individual player can then make his/her move simply by touching the screen and so on. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row wins the game but I’ve designed the game in such a way that the computer will not be able to lose, the best a good player will get from the game is a draw.

 

When the game is over, the third screen which is the Game Over screen appears. We can then press the play again button to start the game again.

To write the code for this project, we need three libraries which are basically to help us interface with the screen. These libraries can be downloaded via the links below.

Libraries

?  Adafruit TFTLCD: https://github.com/adafruit/TFTLCD-Library

?  Adafruit GFX: https://github.com/adafruit/Adafruit-GFX-Library

? Touchscreen: https://github.com/adafruit/Touch-Screen-Library

The code for the project is quite complex and after downloading you will see it takes over 600 lines, which may be too long to explain, so I will just go over the concept.

The flowchart for the game is shown below.

When the game starts, the computer chooses to play from any random corner and then waits for the human player to make his/her own move. After the first round, the game will check each of the other rounds, if the human player can win with his/her next move. If its possible to win, the game blocks the move. In order to make the game interesting and give the human player a chance, we sometimes don’t block the moves and play on a random spot.

The complete code for this project can be downloaded from the link below.

——————–

CODE OF THE PROJECT
——————–

 

 

 

This project is great because it is easy to build and at the same time a great introduction to Artificial intelligence and game programming.

I will try to build some more advanced projects with Artificial Intelligence in the future using the more powerful Raspberry Pi so stay tuned! I would love to hear your opinion about this project. Please post your comments below and don’t forget to like the video if you find interesting. Thanks!

——————–

SUBSCRIBE ON YOUTUBE

——————–

Never miss a video: Subscribe to educ8s.tv