If you have tried the Hard mode on TicToe.org, you already know it cannot be beaten. You can draw against it if you play perfectly, but you will never win. Not once.
That is not luck. It is not a quirk of the code. It is a mathematical guarantee. The computer is running something called the minimax algorithm, and once you understand how it works, it is hard not to find it a little bit brilliant.
Here is the full story.
The basic idea
Minimax works by looking at every possible game that could still be played from the current position. Every possible move you could make. Every possible response to that move. Every possible response to that response. All the way to the end of the game.
For each possible ending, it assigns a score. Win for the computer: good. Win for you: bad. Draw: neutral.
Then it works backwards. If the computer can choose a move that leads to a win no matter what you do, it takes it. If it cannot win, it picks the move that guarantees at least a draw. It never guesses. It never takes a risk. It simply picks the best possible outcome available to it given every possible thing you might do next.
That is minimax in one paragraph. The rest is just the details.
Why it works so well on tic tac toe
Tic tac toe is a small game. There are at most 9 moves in a game, and the total number of possible games is around 255,168. That sounds like a lot but for a computer it is trivial. A modern computer can work through all of them in a fraction of a second.
This is why minimax is so well suited to tic tac toe specifically. The game tree (all the possible games branching out from any position) is small enough that the algorithm can explore every single branch completely, every time it needs to make a move. It is not approximating or cutting corners. It is checking everything.
Games like chess have so many possible positions (more than the number of atoms in the observable universe) that computers have to use shortcuts and approximations. Tic tac toe is simple enough that no shortcuts are needed.
Walking through an example
Say it is the computer's turn and the board looks like this:
X | _ | _
_ | O | _
_ | _ | X
The computer looks at every empty square and asks: if I play here, what are all the possible ways this game could end?
For each empty square it plays out every possible continuation, all the way to the end of the game, and scores each outcome. Then it picks the square that leads to the best guaranteed outcome.
In this case, the computer has X in two opposite corners with O in the centre. If it takes another corner, it can set up a fork: two ways to win at the same time. You can only block one. The computer knows this because it has already played out all the continuations and seen that a fork from this position is unstoppable.
So it takes the corner. Not because it got lucky. Because it already knew.
Maximising and minimising
The name "minimax" comes from what each player is trying to do.
The computer is the maximising player. It always tries to pick the move that leads to the highest possible score (a win, or failing that a draw).
You are the minimising player. The algorithm assumes you will always pick the move that leads to the lowest possible score from the computer's perspective (your win, or failing that a draw).
So the computer does not just plan for what you might do. It plans for what you would do if you played perfectly. If it can win against a perfect opponent, it wins. If it cannot win against a perfect opponent, it settles for a draw and makes sure you cannot win either.
This is why you can draw against the Hard mode but never win. Against perfect play, tic tac toe always ends in a draw. The algorithm knows this, so the best it will ever let you achieve is exactly that.
The same idea powers chess and Go engines
Minimax (and its more efficient variants like alpha-beta pruning) is the foundation of how computers play most strategy games. Chess engines like Stockfish use the same basic logic, just with many layers of optimisation to handle the much larger game tree. Go engines used minimax for decades before a different approach (neural networks combined with tree search) finally beat top human players in 2016.
The difference between the chess engine on your phone and the Hard mode on TicToe.org is not the concept. It is just the size of the problem.
Try it yourself
The best way to understand minimax is to play against it. Load up the Hard mode and try to win. You will not manage it, but pay attention to how it responds to your moves. When you set up what looks like a winning position, it blocks it. When you have two threats at once, it picks the more dangerous one to block and accepts the loss. It never panics. It never makes a mistake.
That is minimax. Every move, perfectly calculated.
Sources
- Minimax algorithm - Wikipedia, full explanation of the algorithm and its applications
- Game tree search - Wikipedia, how computers explore possible game states
- How computers play tic tac toe - freeCodeCamp, practical guide to implementing minimax