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.