Tic-Tac-Toe by Text: The Math of a Game You Never Have to Lose, and How to Beat a Computer That Slips

Updated: Nov 2
Tic-tac-toe is the first strategy game most people learn and the first one most people solve. Once both players know what they are doing, every game is a draw. This post covers how Games Textapp plays it, where the game came from, the numbers behind it, and how to beat an opponent that is very good but not perfect.
How the game plays by text
The board is nine squares numbered 1 to 9, left to right and top to bottom, so 1 is the top left corner, 5 is the centre and 9 is the bottom right. You are X and the app is O. To move, name a square: say 5 for the centre, or 7 for the bottom left corner. The board comes back after every move with the open squares still showing their numbers. Who goes first alternates from game to game.
Games Textapp is an AI, and its tic-tac-toe player follows three rules. If it can complete three in a row, it does. If you are about to complete three in a row, it blocks you. Otherwise it plays the best available move three times in four; the fourth time it picks an open square at random. That last rule is the whole reason a win is possible.
Where the game comes from
Three-in-a-row games are old. Wikipedia's tic-tac-toe article says a version played in the Roman Empire around the first century BC was called terni lapilli, "three pebbles at a time", though the evidence for how it was played is thin. The British name is younger: the same article traces the first print reference to noughts and crosses to an 1858 issue of Notes and Queries.
The game also has a place in computing history. In 1952 A. S. "Sandy" Douglas, a doctoral student at Cambridge, wrote a noughts and crosses program for the EDSAC computer as part of a thesis on human-computer interaction. According to Wikipedia's article on OXO), the board appeared on one of the machine's small cathode-ray screens, the player chose squares with a rotary telephone dial, and the computer could play a perfect game. The name OXO came later.
A quarter of a century on, Danny Hillis and Brian Silverman built a tic-tac-toe machine out of children's construction toys. The Computer History Museum's catalogue entry for the original Tinkertoy computer dates it to 1978 and says it contains about 10,000 wooden parts plus fishing line and sinkers. The human always moves first, and it never loses.
The numbers behind a solved game
Tic-tac-toe is small enough to count completely. Jesper Juul enumerated every possible game and found 255,168 of them, stopping each game at a win or a full board. Of those, 131,184 are won by the first player, 77,904 by the second and 46,080 are drawn. Those figures count rotated and mirrored games separately.
Treat boards that are rotations or reflections of each other as the same and the totals shrink. The Wikipedia article, citing Schaefer's Mathematical Recreations, gives 26,830 games up to symmetry. It also reports, citing Bolon, that there are only 138 distinct finished positions: 91 won by X, 44 won by O and 3 drawn.
None of that means the first player wins. Those counts include every blunder anyone could make. With best play on both sides, the article notes, the game always ends in a draw. Neither player can force a win, and neither ever has to lose.
Opening principles: centre, corner, and the weak edge
Squares are not equal, because they sit on different numbers of winning lines. The centre is on four lines (one row, one column, two diagonals), each corner is on three, and each edge square is on only two. More lines means more ways to build a threat, which is why the standard advice is to open in the centre or a corner. An edge opening does not lose with correct play, but it gives your opponent the easiest path to a draw.
The winning idea in tic-tac-toe is the fork: a move that creates two threats at once, so your opponent can block only one. The classic program by Newell and Simon, as summarised in the Wikipedia article, ranks making a fork right after winning and blocking. Every forced win in the game comes from a fork.
An original test: how often can you beat the app?
To see how much the random fourth move matters, we wrote a short Python script that imports the app's own move code and plays it thousands of times. Against a perfect player, one that never makes a losing move and picks at random among the safe ones, the app played 5,000 games with each starting order.
When the perfect player went first, it won 22.9% of games and drew the rest. When the app went first, the perfect player won only 1.3%. The perfect player never lost.
Against a player who chooses squares completely at random, the app won 78.5% of games when the random player went first, and 96.9% when the app did. The random player won 1.4% and 0.1% respectively. So the app punishes careless play hard, while a careful player can expect roughly one win in five when going first.
We then fixed the perfect player's first move and played 4,000 more games for each opening. Opening in a corner won 26.4% of games, an edge won 21.7%, and the centre only 13.6%. The reason is in the replies. Checking every position with the same script, after a corner opening only one reply avoids a loss for O, the centre. After a centre opening, any of the four corners is safe. So when the app picks at random, a corner opening leaves it seven ways to go wrong, and a centre opening only four.
The practical plan: when you move first, say 1, 3, 7 or 9 for a corner. If the app answers anywhere but 5, look for the fork. If it takes the centre, play for the draw and wait for the next slip.
Try it by text
Text Hi to (725) 252-4524 and ask for tic-tac-toe. Name a square by its number, ask for the rules before you start, and ask for your record any time. No app, no account and nothing to download; it works on any phone that can text. Your first 30 messages are free, then one $9.99 a month subscription covers every Textapp. Text STOP at any time to end messages. Message and data rates may apply. More at textapp.co/apps/games.



Comments