Games

Tic-tac-toe with an unbeatable computer opponent

Three difficulty settings. On 'perfect' the computer plays optimal minimax — the best you can possibly get is a draw, and getting one is the real challenge.

You0
Draws0
Computer0

You are X. Your move.

The whole game is a single small JavaScript file served from this site. There is no account, no server and no tracking of how you play — and once the page has loaded it works offline.

How to play, and how to stop losing

Three in a row wins — horizontally, vertically or diagonally. You play X and move first, which is the stronger side. Choose your opponent from the dropdown: perfect, medium, easy, or another human on the same device.

On perfect, the computer uses minimax, exploring every possible continuation to the end of the game and choosing the move with the best guaranteed outcome. It cannot be beaten. It will never make a mistake, never miss a fork, and never overlook a threat. The only result available to you is a draw, and consistently getting one is a genuine skill.

On medium, the same engine plays optimally about two thirds of the time and plays randomly the rest. That gives you real chances to win, which is what most people actually want from a game.

The strategy that guarantees you never lose

Tic-tac-toe is a solved game: with perfect play from both sides, it is always a draw. Here is how to achieve your half of that.

Opening as X — take the centre. The centre square is part of four winning lines, more than any other square. Corners belong to three, edges to only two. Taking the centre immediately constrains your opponent to a single non-losing reply.

If the centre is taken — take a corner. Never open on an edge. Edge openings lose against good play because they create too few threats.

Learn to see forks. A fork is a position where you create two winning threats at once; your opponent can only block one. Nearly every tic-tac-toe win between competent players comes from a fork rather than from a missed block.

Defending as O — the corner trap. If X opens in the centre and you reply on an edge, you lose to a fork. Reply in a corner. If X opens in a corner, your only non-losing reply is the centre. Memorising just those two facts eliminates most of the losses ordinary players suffer.

Order of priority on every move: win if you can; block if they can win next; create a fork if you can; block their fork; take the centre; take the corner opposite theirs; take any empty corner; take an edge. Follow that list mechanically and you will never lose a game of tic-tac-toe again.

How the unbeatable opponent works

Minimax assumes both players play perfectly. From any position, the algorithm generates every legal move, then every reply, recursively down to positions that are won, lost or drawn. Wins score +1, losses −1, draws 0. On the computer's turn it picks the highest-scoring branch; when simulating your turn it assumes you will pick the lowest.

Tic-tac-toe is small enough for this to be exhaustive: 255,168 possible complete games, which a browser evaluates instantly. The implementation here also applies a slight discount to deeper outcomes, so the computer prefers winning in three moves over winning in five, and prefers losing slowly over losing quickly. Without that discount, a minimax player looks oddly passive when it has already won — every winning path scores the same, so it picks arbitrarily.

The same algorithm underlies chess engines, with one difference: chess has roughly 1044 positions, so exhaustive search is impossible and engines must cut the tree off early and estimate the value of positions they cannot fully calculate. Tic-tac-toe is the smallest game where minimax can be seen working in its pure form, which is exactly why it appears in every introductory AI course.

The numbers behind the game

There are 9! = 362,880 orderings of the nine squares, but many games end before the board fills, so the real total is 255,168 distinct complete games. Of those, X wins 131,184, O wins 77,904 and 46,080 are drawn — first-mover advantage in plain numbers. Accounting for rotations and reflections, there are only 765 essentially different positions, which is why the whole game can be written on one sheet of paper.

The game is genuinely ancient. A three-in-a-row game called terni lapilli was played in the Roman Empire, and grids scratched into paving stones survive across the former empire. The name "noughts and crosses" appears in British sources from the nineteenth century; "tic-tac-toe" became the standard American name in the twentieth.

Frequently asked questions

Can you beat an unbeatable tic-tac-toe AI?

No. Tic-tac-toe is a solved game and perfect play from both sides always ends in a draw. A minimax opponent never errs, so a draw is the best result available. Switch to medium difficulty if you want a chance of winning.

What is the best first move in tic-tac-toe?

The centre. It belongs to four of the eight winning lines — more than any other square — and it leaves your opponent only one reply that does not lose. If the centre is gone, take a corner; never open on an edge.

What is a fork in tic-tac-toe?

A move creating two winning threats at once, so your opponent can block only one. Almost every win between competent players comes from a fork rather than from a missed block.

How do I stop losing when I go second?

Two rules cover most cases: if X opens in the centre, reply in a corner — never an edge. If X opens in a corner, reply in the centre. Those two responses prevent the forks that cause the majority of second-player losses.

How many possible tic-tac-toe games are there?

255,168 complete games. X wins 131,184 of them, O wins 77,904, and 46,080 are drawn — a clear first-mover advantage. Ignoring rotations and reflections, there are only 765 essentially different positions.

Can I play against a friend on the same device?

Yes. Select "Two players on this device" from the opponent dropdown and take turns tapping. There is no online multiplayer, since that would require accounts and a server.