Test Your Skills in Tic-Tac-Toe by Playing Against an AI Opponent
See how Michael Klements created this Arduino Mega-powered DIY tic-tac-toe board with a built-in AI opponent.
Tic-tac-toe is a classic game that involves placing X's and O's onto a 3x3 grid of squares and trying to get 3 of your shape in a row. The game is considered "solved", which means that two players who play perfectly result in a tied match. This fact about tic-tac-toe lets a program have the possibility to never lose, a goal that Michael Klements wanted to achieve with his DIY Tic-Tac-Toe Shield that features an AI opponent.
Getting a device to play tic-tac-toe
The AI for his device isn't a neural network like most would assume, but rather a small implementation of the minimax algorithm. The algorithm is quite simple, as a tree of moves is generated for each possible turn down to a certain depth, and a score is assigned to each outcome for a given player- win, lose, or draw. By following the path that results in the highest (optimal) score, the player can maximize their chances of winning while minimizing their chances of losing. And because tic-tac-toe has so few moves and possible outcomes, this algorithm can easily fit onto a microcontroller.
Electronic components and PCB design
This device uses an Arduino Mega as its main board due to its ample amount of digital GPIO pins. Along with that, there are ten RGB LEDs, ten tactile pushbutton switches, and miscellaneous resistors to handle current consumption and pulling down the buttons. Once the components had been selected, Klements designed a PCB in Easy EDA that is able to clip onto the Mega as a shield for better portability. Once it arrived from the fabricator, he started assembling the device by first soldering the resistors and cutting off their leads.
Next, he attached the LEDs in their classic 3x3 grid, along with an extra one at the bottom. Finally, Klements soldered the pushbuttons and pin headers.
Programming a tic tac toe game
The program represents the game grid in an 2D array that represents an empty space as a 0
, player 1 as a 1
, and player 2 as a 2
. To create the minimax algorithm implementation, some shortcuts had to be taken in order to speed up the time it takes to go through the 255,168 possible game moves. Klements' version always makes the AI play a corner position if it goes first, which reduces the potential moves to just a couple thousand. Because tic-tac-toe is solved, it's impossible to win against the computer, so this device's AI can randomly move for its first two turns to give the human player a chance.
Playing against the AI
After powering on his tic-tac-toe device, Klements selected the mode from one of three choices: easy (random positioning by the AI), expert (full minimax), and two-player. The status LED indicated whose turn it was, along with denoting the current mode. A win is displayed by flashing the winning line's LEDs, with the device creating a fresh match upon the current one's completion.
You can see the entire project's build process in either its write-up or video.