Tic-tac-toe against the Arduino
Build it step by step
0 of 7 doneWire the shift registers
Chain three chips and test with a counting pattern.
Wire the LEDs
Red and green for each cell.
Read the buttons
Print the cell number.
Draw the board
Mark a cell red or green.
Write the rules
Winner and full-board checks.
Add minimax
The Arduino picks its move.
Add the easy mode
A random move one time in three.
How do you build a tic-tac-toe against the Arduino?
Nine buttons with bicolour LEDs make a board where you play against an unbeatable minimax AI. You need Arduino Nano, Bi-colour LED red/green (3-pin) × 9, Resistor 220 Ω × 18, Tactile push button 6 mm × 9 and 74HC595 Shift Register × 3. It takes 7 steps and about 240 minutes.
- Level
- Intermediate
- Time
- About 240 min
- Parts
- 5
Parts list
- Development BoardsBuyArduino Nano × 1
- OptoelectronicsBuy
- PassivesBuyResistor 220 Ω × 18
- SwitchesBuy
- Integrated CircuitsBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Buttons 1 to 9 | Nano D2 to D9 and A0, to ground | Cell inputs (internal pull-ups) |
| First 74HC595 DS | Nano D10 | Data |
| All 74HC595 SHCP | Nano D11 | Shift clock |
| All 74HC595 STCP | Nano D12 | Latch |
| 595 chain | Q7' of each chip to DS of the next | Daisy chain |
| 595 outputs | 220 Ω to red and green legs of each LED | 18 outputs |
| LED common cathodes | Ground | Common ground |
| Supply | Nano 5V and GND | USB power |
Code
Minimax tries every possible game from the current position, scoring a win for the Arduino as 1, a loss as minus 1 and a draw as 0, and picks the move with the best guaranteed score; with only nine cells this runs in a fraction of a second on a Nano, and it never loses, so the best you can do is a draw. For an easy mode, pick a random free cell one time in three instead of the best move. Each cell has a red and a green LED driven from two outputs of the shift register chain, 18 in all. Arduino IDE, board "Arduino Nano" (try the Old Bootloader option if the upload fails). Every LED needs its own 220 Ω resistor, so check the legs of your bicolour LED: the common leg, usually the middle one, goes to ground.
Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
// Tic-tac-toe vs minimax. You are green (O), the Arduino is red (X). Board: Arduino Nano.
const int BTN[9] = {2, 3, 4, 5, 6, 7, 8, 9, A0};
const int DATA = 10, CLK = 11, LATCH = 12;
int board[9]; // 0 empty, 1 = Arduino (red), 2 = you (green)
const int WINS[8][3] = {{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6}};
int winner() {
for (int i = 0; i < 8; i++) {
int a = board[WINS[i][0]];
if (a && a == board[WINS[i][1]] && a == board[WINS[i][2]]) return a;
}
return 0;
}
bool full() { for (int i = 0; i < 9; i++) if (!board[i]) return false; return true; }
int minimax(bool aiTurn) {
int w = winner();
if (w == 1) return 1;
if (w == 2) return -1;
if (full()) return 0;
int best = aiTurn ? -2 : 2;
for (int i = 0; i < 9; i++) if (!board[i]) {
board[i] = aiTurn ? 1 : 2;
int s = minimax(!aiTurn);
board[i] = 0;
if (aiTurn ? s > best : s < best) best = s;
}
return best;
}
int bestMove() {
int bm = -1, bs = -2;
for (int i = 0; i < 9; i++) if (!board[i]) {
board[i] = 1;
int s = minimax(false);
board[i] = 0;
if (s > bs) { bs = s; bm = i; }
}
return bm;
}
void show() {
uint32_t bits = 0;
for (int i = 0; i < 9; i++) {
if (board[i] == 1) bits |= 1UL << (2 * i); // red
if (board[i] == 2) bits |= 1UL << (2 * i + 1); // green
}
digitalWrite(LATCH, LOW);
for (int b = 2; b >= 0; b--) shiftOut(DATA, CLK, MSBFIRST, (bits >> (8 * b)) & 0xFF);
digitalWrite(LATCH, HIGH);
}
void reset() { for (int i = 0; i < 9; i++) board[i] = 0; show(); }
void setup() {
for (int i = 0; i < 9; i++) pinMode(BTN[i], INPUT_PULLUP);
pinMode(DATA, OUTPUT); pinMode(CLK, OUTPUT); pinMode(LATCH, OUTPUT);
reset();
}
void loop() {
for (int i = 0; i < 9; i++) {
if (digitalRead(BTN[i]) == LOW && !board[i]) {
board[i] = 2; show(); delay(250);
if (!winner() && !full()) { board[bestMove()] = 1; show(); }
if (winner() || full()) { delay(2500); reset(); }
}
}
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Press a cell | Green LED lights, then red replies |
| You make a line | Cannot happen against the normal AI |
| Board full | Pause then reset |
| Easy mode | Sometimes makes a mistake |
If it does not work
Wrong LED lights
Check the bit order and the chain order.
Dim LEDs
Check resistor values and the common leg.
Ghost presses
Check the button ground wires.
Slow reply
Shorten the search with a first-move shortcut.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the tic-tac-toe against the Arduino?
Arduino Nano, Bi-colour LED red/green (3-pin) × 9, Resistor 220 Ω × 18, Tactile push button 6 mm × 9 and 74HC595 Shift Register × 3.
How long does the tic-tac-toe against the Arduino take to build?
About 240 minutes for a intermediate maker, in 7 steps.
How do I know the tic-tac-toe against the Arduino works?
Press a cell: Green LED lights, then red replies. You make a line: Cannot happen against the normal AI. Board full: Pause then reset.
Wrong LED lights: what should I check?
Check the bit order and the chain order.
Dim LEDs: what should I check?
Check resistor values and the common leg.
Ghost presses: what should I check?
Check the button ground wires.
Slow reply: what should I check?
Shorten the search with a first-move shortcut.
