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.
- Arduino Uno: https://educ8s.tv/part/ArduinoUno
- Touch Screen: https://educ8s.tv/part/TouchScreen28
- 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.
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
Can u publish an unbeatable version pls.
I downloaded the game of tic tac toe code from your site but does not work:
When checking and loading the game code does not give me any error but the display 2.8 TFT LCD touch screen driver 9341 and nothing appears on the serial monitor
I get the message “starting” and pressing the display will start the game on the serial monitor but the display is blank.
Can you give me some advice, thanks.
Change the function to following values:
void initDisplay()
{
tft.reset();
tft.begin(0x9341);
tft.setRotation(3);
}
Thanks that worked for me!!
Sorry can you send the fixed code by email
zeho-369@hotmail.com
Kindly send the fixed code to mail maimoona.ahmed45@gmail.com
Sorry can you send the fixed code by email chen.zying36@gmail.com thanks
Sorry can you send the fixed code by email dejuco@gmx.de
will I don’t know how you got it to work, but you needed a Semicolon after the initDisplay(); then it worked.
greetings from slovakia u are fucking amazaing MIKEEEl :**** we love u
Hello, unfortunately I can’t get it to work. Can you email me the code?
dejuco@gmx.de
problem is
why here… Error compiling for board arduino? can u tall me what happening bro
try with another usb port, or change the board. if ur using leonardo use uno
ERROR COMPILING FOR ARDUINO GENUINO UNO?????
Arduino: 1.8.5 (Mac OS X), Board: “Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)”
/var/folders/wx/cnqslqk54v108jzw56bv6gm80000gn/T//ccLwVbuK.ltrans1.ltrans.o: In function `drawCircle(int, int)’:
/Users/apple/Downloads/TicTacToeEasy-2/TicTacToeEasy/TicTacToeEasy.ino:521: undefined reference to `circle’
/Users/apple/Downloads/TicTacToeEasy-2/TicTacToeEasy/TicTacToeEasy.ino:521: undefined reference to `circle’
/var/folders/wx/cnqslqk54v108jzw56bv6gm80000gn/T//ccLwVbuK.ltrans1.ltrans.o: In function `drawX(int, int)’:
/Users/apple/Downloads/TicTacToeEasy-2/TicTacToeEasy/TicTacToeEasy.ino:526: undefined reference to `x_bitmap’
/Users/apple/Downloads/TicTacToeEasy-2/TicTacToeEasy/TicTacToeEasy.ino:526: undefined reference to `x_bitmap’
collect2: error: ld returned 1 exit status
exit status 1
Error compiling for board Arduino/Genuino Uno.
Invalid library found in /Users/apple/Documents/Arduino/libraries/AFMotor: /Users/apple/Documents/Arduino/libraries/AFMotor
Invalid library found in /Users/apple/Documents/Arduino/libraries/NewPing: /Users/apple/Documents/Arduino/libraries/NewPing
Invalid library found in /Users/apple/Documents/Arduino/libraries/AFMotor: /Users/apple/Documents/Arduino/libraries/AFMotor
Invalid library found in /Users/apple/Documents/Arduino/libraries/NewPing: /Users/apple/Documents/Arduino/libraries/NewPing
Invalid library found in /Users/apple/Documents/Arduino/libraries/AFMotor: /Users/apple/Documents/Arduino/libraries/AFMotor
Invalid library found in /Users/apple/Documents/Arduino/libraries/NewPing: /Users/apple/Documents/Arduino/libraries/NewPing
Invalid library found in /Users/apple/Documents/Arduino/libraries/AFMotor: /Users/apple/Documents/Arduino/libraries/AFMotor
Invalid library found in /Users/apple/Documents/Arduino/libraries/NewPing: /Users/apple/Documents/Arduino/libraries/NewPing
Invalid library found in /Users/apple/Documents/Arduino/libraries/AFMotor: /Users/apple/Documents/Arduino/libraries/AFMotor
Invalid library found in /Users/apple/Documents/Arduino/libraries/NewPing: /Users/apple/Documents/Arduino/libraries/NewPing
Invalid library found in /Users/apple/Documents/Arduino/libraries/AFMotor: /Users/apple/Documents/Arduino/libraries/AFMotor
Invalid library found in /Users/apple/Documents/Arduino/libraries/NewPing: /Users/apple/Documents/Arduino/libraries/NewPing
This report would have more information with
“Show verbose output during compilation”
option enabled in File -> Preferences.
Hi IEE,
Did you ever resolve this problem ?
I am getting exactly the same errors when compiling.
But I am also using updated versions of all 3 libraries.
Which versions of the libraries were you using when you solved the problem ?
Thanks in advance.
Arduino: 1.8.6 (Windows 7), Board: “Arduino/Genuino Uno”
C:\Users\Istep\AppData\Local\Temp\cc5ELxTp.ltrans2.ltrans.o: In function `drawCircle’:
C:\Users\Istep\AppData\Local\Temp\Rar$DI03.028\TicTacToeEasy/TicTacToeEasy.ino:526: undefined reference to `circle’
C:\Users\Istep\AppData\Local\Temp\Rar$DI03.028\TicTacToeEasy/TicTacToeEasy.ino:526: undefined reference to `circle’
C:\Users\Istep\AppData\Local\Temp\cc5ELxTp.ltrans2.ltrans.o: In function `drawX(int, int)’:
C:\Users\Istep\AppData\Local\Temp\Rar$DI03.028\TicTacToeEasy/TicTacToeEasy.ino:531: undefined reference to `x_bitmap’
C:\Users\Istep\AppData\Local\Temp\Rar$DI03.028\TicTacToeEasy/TicTacToeEasy.ino:531: undefined reference to `x_bitmap’
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board Arduino/Genuino Uno.
This report would have more information with
“Show verbose output during compilation”
option enabled in File -> Preferences.
I have the same problem did you solved it?
Ulan pezeveenk gittik aldık çalışmıyo yazacan koda sokayım
Hello, I’m just trying to rebuild this project, but when I insert the code I get the following error when checking:
exit status 1 error compiling for board arduino
I have updated the links to the libraries. It should work now.
Same Error, Exit Status 1, Please Email Corrected Code
Download the libraries again, the latest version of each one and it will work. Thanks
Hi educ8s,
I am getting exactly the same errors when compiling, even though I am using the latest versions of all 3 libraries. I have downloaded them using IDE, or should I be following your links ? If so, please include them in your reply. Keep up the good work – regards.
Hi educ8S,
I have the same problem
exit status 1
Error compilando para la tarjeta Arduino Uno.
Can you help me?
Best regards
Hello Fran, I just checked the code. You are right, the code didn’t compile with the version of the TFTLCD library I was providing anymore. I have updated the link above. Download the latest versions of all three libraries and it will work. Thanks
Hello, I am a beginner at arduino. What should I do with “graphic c” and “tic tac toe easy ” files?
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board Arduino Uno.
WHAT IS THE PROBLEM????
C:\…\Adafruit_TFTLCD\pin_magic.h:166:28: warning: backslash and newline separated by space
#define write8inline(d) {\
C:\…\Adafruit_TFTLCD\pin_magic.h:172:30: warning: backslash and newline separated by space
PORTG |= (d & 0x10) << 1; \
C:\…\Adafruit_TFTLCD\pin_magic.h:287:33: warning: backslash and newline separated by space
#define read8inline(result) { \
C:\…\Adafruit_TFTLCD\Adafruit_TFTLCD.cpp: In member function 'void Adafruit_TFTLCD::init()':
C:\…\Adafruit_TFTLCD\Adafruit_TFTLCD.cpp:117:3: error: 'textsize' was not declared in this scope
textsize = 1;
^~~~~~~~
C:\…\Adafruit_TFTLCD\Adafruit_TFTLCD.cpp:117:3: note: suggested alternative: 'textsize_x'
textsize = 1;
^~~~~~~~
textsize_x
Multiple libraries were found for "Adafruit_TFTLCD.h"
Used: C:\…\Adafruit_TFTLCD
Not used: C:\…\Adafruit_TFTLCD_Library
exit status 1
Error compiling for board Arduino Uno.
I have the same issue did someone solved it or not?
Arduino: 1.8.6 (Windows 7), Board: “Arduino/Genuino Uno”
C:\Users\Istep\AppData\Local\Temp\cc5ELxTp.ltrans2.ltrans.o: In function `drawCircle’:
C:\Users\Istep\AppData\Local\Temp\Rar$DI03.028\TicTacToeEasy/TicTacToeEasy.ino:526: undefined reference to `circle’
C:\Users\Istep\AppData\Local\Temp\Rar$DI03.028\TicTacToeEasy/TicTacToeEasy.ino:526: undefined reference to `circle’
C:\Users\Istep\AppData\Local\Temp\cc5ELxTp.ltrans2.ltrans.o: In function `drawX(int, int)’:
C:\Users\Istep\AppData\Local\Temp\Rar$DI03.028\TicTacToeEasy/TicTacToeEasy.ino:531: undefined reference to `x_bitmap’
C:\Users\Istep\AppData\Local\Temp\Rar$DI03.028\TicTacToeEasy/TicTacToeEasy.ino:531: undefined reference to `x_bitmap’
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board Arduino/Genuino Uno.
Arduino: 1.8.18 (Windows Store 1.8.55.0) (Windows 10), Board: “Arduino Uno”
C:\Users\Arrow\AppData\Local\Temp\ccQWkZ9D.ltrans0.ltrans.o: In function `drawX’:
C:\Users\Arrow\Downloads\TicTacToeEasy/TicTacToeEasy.ino:531: undefined reference to `x_bitmap’
C:\Users\Arrow\Downloads\TicTacToeEasy/TicTacToeEasy.ino:531: undefined reference to `x_bitmap’
C:\Users\Arrow\AppData\Local\Temp\ccQWkZ9D.ltrans0.ltrans.o: In function `drawCircle’:
C:\Users\Arrow\Downloads\TicTacToeEasy/TicTacToeEasy.ino:526: undefined reference to `circle’
C:\Users\Arrow\Downloads\TicTacToeEasy/TicTacToeEasy.ino:526: undefined reference to `circle’
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board Arduino Uno.
This report would have more information with
“Show verbose output during compilation”
option enabled in File -> Preferences.
error coming please hel
Arduino: 1.8.19 (Windows Store 1.8.57.0) (Windows 10), Board: “Arduino Uno”
C:\Users\DELL\AppData\Local\Temp\ccmAcbTh.ltrans0.ltrans.o: In function `drawX’:
C:\Users\DELL\Downloads\TicTacToeEasy/TicTacToeEasy.ino:531: undefined reference to `x_bitmap’
C:\Users\DELL\Downloads\TicTacToeEasy/TicTacToeEasy.ino:531: undefined reference to `x_bitmap’
C:\Users\DELL\AppData\Local\Temp\ccmAcbTh.ltrans0.ltrans.o: In function `drawCircle’:
C:\Users\DELL\Downloads\TicTacToeEasy/TicTacToeEasy.ino:526: undefined reference to `circle’
C:\Users\DELL\Downloads\TicTacToeEasy/TicTacToeEasy.ino:526: undefined reference to `circle’
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board Arduino Uno.
This report would have more information with
“Show verbose output during compilation”
option enabled in File -> Preferences.
Can you please help me with this error? Thank you
Arduino: 1.8.19 (Windows 10), Board: “Arduino Uno”
C:\Users\d\AppData\Local\Temp\ccffQvzl.ltrans0.ltrans.o: In function `drawX’:
C:\Users\d\AppData\Local\Temp\Temp1_TicTacToeEasy (1).zip\TicTacToeEasy/TicTacToeEasy.ino:531: undefined reference to `x_bitmap’
C:\Users\d\AppData\Local\Temp\Temp1_TicTacToeEasy (1).zip\TicTacToeEasy/TicTacToeEasy.ino:531: undefined reference to `x_bitmap’
C:\Users\d\AppData\Local\Temp\ccffQvzl.ltrans0.ltrans.o: In function `drawCircle’:
C:\Users\d\AppData\Local\Temp\Temp1_TicTacToeEasy (1).zip\TicTacToeEasy/TicTacToeEasy.ino:526: undefined reference to `circle’
C:\Users\d\AppData\Local\Temp\Temp1_TicTacToeEasy (1).zip\TicTacToeEasy/TicTacToeEasy.ino:526: undefined reference to `circle’
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board Arduino Uno.
This report would have more information with
“Show verbose output during compilation”
option enabled in File -> Preferences.
Arduino: 1.8.18 (Windows 10), Board: “Arduino Uno”
In file included from C:\Users\ben dover\OneDrive\Desktop\arduino-1.8.18\libraries\TFTLCD-Library-master/Adafruit_TFTLCD.h:15:0,
from C:\Users\ben dover\OneDrive\Documents\Arduino\libraries\sketch_nov27a\sketch_nov27a.ino:2:
C:\Users\ben dover\OneDrive\Desktop\arduino-1.8.18\libraries\Adafruit-GFX-Library-1.11.3/Adafruit_GFX.h:12:10: fatal error: Adafruit_I2CDevice.h: No such file or directory
#include
^~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
exit status 1
Error compiling for board Arduino Uno.
This report would have more information with
“Show verbose output during compilation”
option enabled in File -> Preferences.