part 25
Three in a row: the whole game tree and how it is won
Three in a row has 255,168 games, and with perfect play it always ends in a draw. The complete calculation shows who has the advantage, how a fork arises, and why the starting player wins 88% of games against the Weak AI, but the second player at most 41%.
The game Lines (Čiary in the Slovak interface) has three variants, and everyone knows the smallest one: three in a row on a 3 × 3 board. My note on it from 30 October 2026 says: it is very clear-cut, losing in it is an art and it mostly ends in a draw. To that, my brief: an article on how one wins in it, what the winning combinations are and how the first and the second player fare. It is the first article of a series on the game Lines.
The answers can not only be estimated but also counted. The tree of this game is so small that it can be walked through in full: every game, every position and the value of every move under perfect play. The article walks through them first and only then looks at how the results carry over to the AI of the game (Weak, Normal, Strong) and to a player who wants to get the upper hand over it. The names of the game and of its variants are working names and wait for a trademark check; the text therefore keeps to the names of the interface.
Rules and the eight lines
Two players take turns placing one stone on an empty cell. A stone is not taken or moved. The winner is the one who first has three of their stones in a row: horizontally, vertically or diagonally. When the board is full and nobody has a line, it is a draw. In the game a stone is a disc in the colour of the player (until 30 October 2026 they were the letters X and O). Against the AI you choose who starts: You, Opponent or Random.
The article always speaks of the first and the second player, not of a human and an AI: the advantage or disadvantage depends on the order, not on who holds the mouse. In the pictures the first player is blue, the second orange, and the number on a stone is the order of the move.
On a 3 × 3 board there are exactly eight winning lines: three rows, three columns and two diagonals.
Every cell lies in one row and in one column. The centre also lies on both diagonals, each corner on one and each edge on none. From this the number of lines follows: centre 4, each corner 3, each edge 2 (the sum is 4 + 4 × 3 + 4 × 2 = 24, that is eight lines times three cells). A stone on the centre cell builds and at the same time kills the most lines, a stone on an edge the fewest.
The number of lines is a rough estimate of a cell's value, and the calculation confirms it: if the first player opens in the centre and the other moves are random, it wins 69.3% of games; after a corner 60.7% and after an edge 53.6%.
The whole game tree
A game is a sequence of moves that ends in a line or a full board. A full enumeration (every move, every reply) found 255,168 of them. The first player wins 131,184 of them, the second 77,904, and 46,080 games are draws. The distribution by length:
| moves in a game | number of games | who won |
|---|---|---|
| 5 | 1,440 | first |
| 6 | 5,328 | second |
| 7 | 47,952 | first |
| 8 | 72,576 | second |
| 9 | 127,872 | first 81,792, draw 46,080 |
There are 5,478 positions that appear in some game, of which 958 are final (a line or a full board) and 4,520 are ones in which play still goes on. If rotations and reflections are counted as the same picture, 765 positions remain, of which 627 are unfinished and 138 final (91 wins of the first, 44 of the second and 3 draws).
The calculation confirmed the numbers that Wikipedia gives. The page on game complexity (state of 22 October 2026) has for three in a row 5,478 positions, 765 positions up to symmetry, 255,168 games and 26,830 games up to symmetry. The page Tic-tac-toe (state of 21 October 2026) gives 765 positions up to symmetry, 138 final positions and their distribution 91 / 44 / 3. The first three numbers came out the same. With 26,830 it depends on what is counted: it comes out when, at each step, moves leading to the same position up to symmetry are merged; if on the other hand every game is counted with the eight symmetries, it is 255,168 / 8 = 31,896. Both numbers are correct; they count different things.
The split 51.4% / 30.5% / 18.1% (of the number of games) is not what a player sees at the table. A random player does not choose a game from a list, but moves: short games are more probable under random play than long ones. The exact calculation with the same probability for each free cell gives:
In total the first player wins 58.5%, the second 28.8% and the draw is 12.7% of games; 64.8% of games end in a line before the board is full. So the first player wins twice as often as the second, although neither of them knows anything: the advantage of the order exists even without skill.
The value of the openings under perfect play
With perfect play by both sides each of the nine openings is a draw. The first player therefore has no way to win, and the second has no way to win either. The openings differ in only one thing: how many replies of the second player hold the draw and how many lose. Up to rotations and reflections there are three.
| opening | replies of the second | draw | lose | which draw |
|---|---|---|---|---|
| centre | 8 | 4 | 4 | the four corners |
| corner (4 cells) | 8 | 1 | 7 | only the centre |
| edge (4 cells) | 8 | 4 | 4 | the centre, two adjacent corners and the opposite edge |
The same pattern is given also by Wikipedia on the Tic-tac-toe page (after a corner the second must play into the centre, after the centre into a corner, after an edge into the centre, into an adjacent corner or onto the opposite edge). With a corner, therefore, 7 of the 8 replies are wrong.
Every losing reply loses equally fast. With perfect play of the first and the best defence of the second, each of them ends in a loss of the second on the 7th move, that is, the first player's fourth stone. None of them can be stretched to eight or nine moves.
The picture shows all four kinds of mistake after a corner: the adjacent edge, the adjacent corner, the far edge and the opposite corner. In all four the first player has two threats at once after its third stone. Such a move is called a fork, and it is the core of the whole game.
Where one can go wrong
What is a mistake? A move after which the value of the position for the player gets worse (win to draw, draw to loss). On the "perfect path", that is, in positions that are reached when both sides play correctly, the share of wrong moves in each move is:
| move | plays | positions (up to symmetry) | legal moves | of them losing | share |
|---|---|---|---|---|---|
| 1 | first | 1 | 9 | 0 | 0% |
| 2 | second | 3 | 24 | 15 | 62.5% |
| 3 | first | 5 | 35 | 4 | 11.4% |
| 4 | second | 16 | 96 | 61 | 63.5% |
| 5 | first | 18 | 90 | 51 | 56.7% |
| 6 | second | 23 | 92 | 51 | 55.4% |
| 7 | first | 24 | 72 | 24 | 33.3% |
| 8 | second | 22 | 44 | 17 | 38.6% |
| 9 | first | 10 | 10 | 0 | 0% |
The share goes through positions, not games: each position is counted once. The table can be read in two interesting ways. After a correct reply of the second player to an opening in the centre or a corner, the first player cannot lose in its third move even with a random move: all 4 losing moves of the third move come only after an opening on an edge: after a reply into an adjacent corner 3 of the 7 legal moves, after a reply into the centre 1 of 7. And in moves 2, 4, 5 and 6 more than half of the legal moves lose: on the perfect path one must be careful on both sides.
Games in which nobody makes a mistake number 3,584 of 255,168, that is 1.4% (448 up to symmetry). The distribution of values is uneven: of the 4,520 positions still in play, under perfect play 2,310 remain wins for the first player, 1,158 wins for the second and only 1,052, that is 23%, draws. A draw is a narrow corridor in the tree, in which most positions are decided for one of the players.
How one wins: the fork
A threat is a line in which a player has two stones and the third cell is free. The opponent must immediately block, otherwise it loses: that is a forced reply. A fork is a move that creates two threats at once. The opponent blocks one and the player completes the other.
Every fork arises the same way: three stones of one player, of which two lines have two stones each and a free third cell. Up to rotation and reflection there are nine such shapes: six of them give two threats and three give three.
In the games that arise after a mistake of the second player (the position after the 5th move in the most precise attack, for all openings and all wrong replies), only three of these nine shapes recur:
- three corners (an L of corners): typically after an opening in a corner, when the second player replies into a corner or onto an edge;
- two corners and the centre: after an opening in a corner and a reply onto an edge, after an opening in the centre and a reply onto an edge;
- corner, centre and edge: after an opening in a corner, in the centre and on an edge.
Two of the three shapes contain the centre, which lies in four lines.
Forced replies have one more property: a block can at the same time be a move that creates a threat of its own. The most instructive case is the trap of the opposite corner. After 1. a corner, 2. the centre and 3. the opposite corner, the second player may seem to be able to move anywhere, but playing into a corner is a mistake:
When the second player plays into a corner, it creates a threat on the third corner; the first player blocks it with exactly that stone which at the same time creates a fork. Such "block and attack" moves are the reason why even a player who always blocks can lose.
One way to keep all of this in mind is the order of rules that Wikipedia gives: 1. win if you can; 2. block the threat of the opponent; 3. create a fork; 4. prevent the opponent's fork; 5. take the centre; 6. take the corner opposite the opponent's; 7. take a free corner; 8. take a free edge. The Wikipedia page gives it as the order used by the program of Newell and Simon from 1972 (in a note it refers to the work of Crowley and Siegler from 1993). The calculation checked it in full: in the 762 positions in which it says what to play, and against every opponent from both sides, there is not a single move that would worsen the value of the position. For the 4th rule one takes the only cell where the opponent would make a fork; if there are two, the player creates a threat whose forced reply is not a fork of the opponent.
Here is what a game looks like in which nobody errs:
The game in the picture is one of the perfect games with the most forced moves (5 of 9, and none has more): from the fifth move to the end every move is a block that creates a threat, which in turn must be blocked.
Against the AI of the game
The AI in the game Lines has three levels (js/realms/rokgard/games/lines/core/ai.js, DESIGN §80.3; at the time of the measurement on 30 October 2026 js/realms/taflgard/games/chains/core/chains-ai.js): Weak (level 1) looks at one move and makes mistakes, Normal (level 2) looks four moves ahead and Strong (level 3) up to fourteen moves ahead, which on a 3 × 3 board means to the end of the game. The two higher levels play perfectly on the 3 × 3 board.
- All three open in the centre. On an empty board the only candidate is the centre. When the AI starts, the second player always faces an opening in the centre, and only the four corners give it a draw.
- The Normal and the Strong AI never lose. The tool of the game (now
lines.js --solved; at the time of the measurementchains.js --solved) walks the whole tree of the opponent's moves against level 3: 1,386 games, 0 losses, 900 wins and 486 draws. A separate check passed for levels 2 and 3 through all the positions into which the AI can get against any opponent, and in each it verified that every move from its set of best moves does not worsen the value of the position: 1,194 positions of level 2 and 1,610 positions of level 3, without a single exception. - The difference between them is only in how many equally good moves they see. Of the 1,610 positions into which the Strong AI gets, both levels choose the same set of moves in 1,590; in the other 20 the Strong AI sees more drawing moves. Example: after an opening on an edge the Normal always plays into the centre, while the Strong chooses at random among the four drawing replies (two adjacent corners, the centre, the opposite edge). After the centre both play a random corner, and after a corner the centre.
- **The Weak uses three numbers from
LEVELS[1]:** it never misses a win in one move; otherwise in 8% of its moves it plays a random move, in 40% of the remaining moves it does not see the opponent's threat, and otherwise it chooses at random among the moves whose value is at least 40% of the value of the best. It therefore makes exactly those mistakes with which one wins.
What a player with a given strategy achieves
The individual results can be calculated exactly, without a sample: in each position one can compute which move the Weak AI plays and with what probability (its random stream has three branches: a random move, a move without looking at the opponent and a move with looking), and the game tree is then walked with these probabilities. The result was compared with a simulation of the real AI (20,000 games per cell of the table): they differ by at most 0.5 percentage point.
The strategies of the player:
- random: any free cell;
- blocks: wins if it can, blocks a threat, otherwise at random;
- centre and corners: as the previous one, otherwise the centre, then a corner, then an edge;
- fork: wins, blocks, makes a fork (if there is one), otherwise the centre and corners;
- 8 rules: the order of Newell and Simon from the previous section;
- perfect: at random among the moves that do not worsen the value of the game;
- best: the move that maximises the share of wins against this AI (searched over all possibilities).
Table of the full results (win / draw / loss, %):
| strategy | first player against Weak AI | second player against Weak AI |
|---|---|---|
| random | 14.3 / 7.1 / 78.6 | 1.3 / 1.5 / 97.3 |
| blocks | 68.4 / 23.0 / 8.5 | 29.6 / 43.3 / 27.1 |
| centre and corners | 74.9 / 25.1 / 0 | 41.0 / 59.0 / 0 |
| fork | 81.1 / 18.9 / 0 | 41.0 / 59.0 / 0 |
| 8 rules | 86.1 / 13.9 / 0 | 41.0 / 59.0 / 0 |
| perfect | 88.5 / 11.5 / 0 | 41.0 / 59.0 / 0 |
| best | 95.5 / 4.5 / 0 | 41.0 / 59.0 / 0 |
Three things follow from the table.
The first player against the Weak AI wins the majority of games even with the simplest strategy. A strategy that takes the centre and corners and at the same time blocks and wins, wins 74.9% of games and loses none. Each further step adds wins: the fork 6.2 points (81.1%), the eight rules a further 5 (86.1%), perfect play 2.4 (88.5%) and the best possible a further 7 (95.5%).
The second player against the Weak AI wins no more than 41%. The Weak AI opens in the centre and the second player has only four safe replies (the corners). No strategy changes this: even the "best" stays at 41.0%. All wins of the second player are mistakes of the Weak AI, which the second player waits for and uses; it cannot force them itself.
A bad reply by the second player is expensive even against the Weak AI. A player who answers the centre of the Weak AI with an edge wins, even with the best continuation, only 19.0% of games, 27.5% are draws and 53.6% are lost. A reply into a corner gives 41.0% wins and no loss at all.
Which opening the Weak AI falls for most often
The Weak AI replies to the first player's openings with the following probabilities of error (a reply after which the first player has a won position under perfect play):
| opening | first reply of the Weak AI is a mistake | afterwards: random | fork | perfect | best |
|---|---|---|---|---|---|
| corner | 86.0% | 15.3 | 77.7 | 94.0 | 95.5 |
| edge | 52.6% | 11.8 | 65.8 | 85.0 | 91.4 |
| centre | 50.0% | 20.5 | 81.1 | 80.9 | 89.2 |
The columns on the right are the share of wins of the first player (%) when it plays the given strategy after the opening. After a corner the Weak AI replies into the centre, the only correct reply, in only 14% of games. That is why a corner is the best opening against the Weak AI, although against a random opponent it is the centre and against a perfect one it makes no difference. The choice of opening depends on whom one wants to surprise.
One can also break down how the Weak AI loses. Against the "perfect" strategy (simulation of 20,000 games per row): after a corner, of the 94.0% wins 69.0% come from a fork and 25.0% from an overlooked single threat; after the centre, of 81.0% wins (exact: 80.9%), 53.3% are a fork and 27.7% an overlooked threat; after an edge, of 84.9% (exact: 85.0%), 60.8% are a fork and 24.1% an overlooked threat. Most wins against the Weak AI are therefore given by a fork. The "fork" strategy after an opening on an edge, however, loses 3.5% of games: it looks for its own fork and does not block the opponent's.
Against the Normal and the Strong AI
Against both higher levels the result of every strategy from "centre and corners" upward is the same: 100% draws, both as the first and as the second player. A win is not possible and it is not worth looking for it. The best possible result is a draw, and the rest of the game is about whether one can reach it.
| strategy | Normal: first player | Normal: second player | Strong: first player | Strong: second player |
|---|---|---|---|---|
| random | 0 / 13.5 / 86.5 | 0 / 2.0 / 98.0 | 0 / 22.3 / 77.7 | 0 / 1.7 / 98.3 |
| blocks | 0 / 91.7 / 8.3 | 0 / 43.3 / 56.7 | 0 / 83.5 / 16.5 | 0 / 45.2 / 54.8 |
| centre and corners and all better | 0 / 100 / 0 | 0 / 100 / 0 | 0 / 100 / 0 | 0 / 100 / 0 |
(Win / draw / loss, %.) A player who takes the centre and the corners and at the same time always blocks and wins never loses. The one who blocks but otherwise moves at random loses 8 – 17% of games as the first player and 55 – 57% as the second.
Losing takes skill
My observation holds for a player who knows at least the basics, and does not hold for one who moves without thinking. The numbers say this:
- A player who takes the centre and the corners and at the same time blocks and wins does not lose a single game against any of the AI opponents, as the first or as the second. That is where the impression comes from that "losing is an art" and that it "mostly ends in a draw": whoever learns once to block a threat holds the draw against each of the AI opponents.
- The random player loses very often: against the Strong AI as the first in 77.7% of games, as the second in 98.3%. It is not an art: on the 3 × 3 board many moves lead it off the right path (the table above).
- Even a player who blocks but otherwise moves at random loses: as the first 8 – 17% of games, as the second 55 – 57%. That is why blocking a threat is not enough; one must also see a fork one move ahead.
- Against the Normal and the Strong AI one mistake is enough. The fastest loss that the AI surely completes takes 5 moves for the second player and 6 for the first: it is enough not to block a threat.
- A deliberate loss is certain. A strategy that tries to lose reaches it in 100% of games against all three AI opponents, as the first and as the second (exact calculation, 30 October 2026). The AI does not miss a win in one move, so it is enough not to block its threat.
- The first player finds it harder to lose than the second. After a correct reply to an opening in the centre or a corner, the first player cannot lose at all in its third move, after an edge only with some of the 7 moves; the second player has losing moves already at its second move after every opening.
The first and the second player
The advantage of starting depends on how well both play:
| when both play | first wins | draw | second wins |
|---|---|---|---|
| randomly | 58.5% | 12.7% | 28.8% |
| "blocks" (otherwise random) | 31.1% | 51.5% | 17.4% |
| centre and corners, fork, 8 rules, perfect | 0% | 100% | 0% |
| Weak AI against Weak AI | 66.4% | 15.3% | 18.4% |
| Normal and Strong AI against each other | 0% | 100% | 0% |
The advantage of the first move in random play is double (58.5% against 28.8%); with "blocks" it is still 31.1% against 17.4%, and with good play it vanishes altogether. The advantage of starting is smaller the fewer mistakes the players make. The first player can create a threat at the earliest with its third move and win with its fifth, the second a threat with its fourth and a win with its sixth; the opponent's mistake is used one move earlier. With perfect play nothing of this remains.
Mixed pairs: the perfect first player wins 96.8% against a random second player and the rest is a draw; a random first player against a perfect second player never wins and loses 77.7%. The order nevertheless matters: against the Weak AI the same perfect play ends with 88.5% wins as the first player and 41.0% as the second.
And the other variants
The other two variants of the game have different numbers. From the measurement of the AI (docs/TUNING.md §39, 30 October 2026, 40 games per pair, therefore ±15 percentage points):
- Five in a row (15 × 15): in the game of two Weak AI opponents the first mover wins 77.5% of games (a draw counts as half), between two Normal AI opponents 100% (40 of 40), between the Normal and the Strong AI 70%;
- Gravity four (7 × 6): the first mover wins 80.0% of games between two Weak AI opponents and 60.0% between two Normal AI opponents; the Strong AI draws against the Strong one in 80% of games.
These are results of measuring the AI, not proofs about the game, but the direction is clear: on larger boards the advantage of the starting player with better play does not vanish (five in a row: the first mover wins 71.3% between two Strong AI opponents), while on 3 × 3 it is completely lost from the Normal level onwards. What follows from this for the settings of further configurations is dealt with by a separate analysis of the configurator of Lines; it is not anticipated here.
Five rules
- You are starting. Open in a corner: against the Weak AI that is the best opening (94.0% of wins with perfect continuation, up to 95.5% with the best), and against a perfect opponent it costs you nothing. Against an opponent who moves at random the centre is more advantageous (69.3% against 60.7% for a corner and 53.6% for an edge).
- You are second. After the centre answer with a corner (four corners draw, four edges lose), after a corner only with the centre, after an edge with the centre, an adjacent corner or the opposite edge. If the opponent plays a corner, you the centre and it the opposite corner, answer with an edge, not a corner.
- You want to win against the Weak AI. Start with a corner, block and look for a fork. As the second player you cannot expect more than 41% of wins: wait until the Weak AI overlooks a threat. Do not look only for your own fork and ignore the opponent's: whoever looks for it and does not block the opponent's loses (3.5% of games after an opening on an edge).
- You want to stay in a draw against a perfect opponent. Keep the order: win, block, create a fork, prevent the opponent's fork, centre, corner opposite the opponent's, free corner, free edge. Against the Normal and the Strong AI this means 100% draws.
- Before every move ask three questions. Can I win? Must I block? And is there a cell that would be a fork for me or for the opponent? If it is the only one for the opponent, take it; if there are two, make a threat whose forced reply is not the opponent's fork.
What I take from it
- A "solved" game still gives differences. Against a perfect opponent the result is always a draw, but against the Weak AI the difference between the first and the second player is 88.5% against 41.0%, and between perfect and random play 88.5% against 14.3%.
- A draw is a narrow corridor. Of the 4,520 positions still in play, 1,052 are draws, and the games without any mistake are 3,584 of 255,168.
- An AI can be calculated, not only simulated. When the tree is small, exact probabilities are cheaper and more precise than millions of games; the simulation of the real AI serves as a check.
- The Weak AI is a designed mistake. Its mistakes are named (40% of threats not seen, 8% random moves) and they can be measured: after an opening in a corner the perfect player wins 94% of games, after an edge 85%.
- Figures from the world are verified twice: by one's own calculation and by a second source. The three known numbers (255,168, 5,478, 765) came out the same; 26,830 comes out only with one of the ways of counting, and that is why the text says what is counted.
Sources and notes
- Full calculation of the tree (
tree.js, a one-off script outside the game and the blog; 30 October 2026): numbers of games and positions, values of openings, perfect games, a table of mistakes by moves, results of random play (exact probabilities), shapes of a fork, lines of play in the pictures. Verification:node sym.js(the eight symmetries: 765 positions, 138 final positions, 91 / 44 / 3, 26,830 when equivalent moves are merged, 31,896 = 255,168 / 8). The core of the enumeration is in the box above. The scripts are not part of the blog; they are kept with the notes to the article. - Numbers about the AI (
weak.js,botdist.js,strats.js,extra.js,botvsbot.js, 30 October 2026): the distribution of the move of each level in each position (level 1 from the three branches of the random stream of the functionchooseLook, levels 2 and 3 evenly from the best moves ofsearchRoot), the walk of the tree with the strategies of the player; a check by simulation with the real AI (node weak.js --sim 20000 --sim2 300: level 1 with 20,000 games per cell, levels 2 and 3 with 300; deviation from the exact values up to 0.5 point at level 1, within noise at levels 2 and 3). - How the Weak AI loses (
forks-sim.js 20000): simulation of the real AI, 20,000 games per row, splitting the wins into a fork and an overlooked single threat. - Diagrams and charts: the pictures of the boards are drawn by the one-off script
boards.jsdirectly from the calculated tree (the lines of play are the main variants fromtree.js, the threats are counted, nothing is drawn by hand) intoassets/charts/lines-*.svg; two charts are intools/charts.jswith the numbers and commands in the comments (tree.jsandweak.js, 30 October 2026). - The game and the AI:
docs/DESIGN.md§80 (rules, AI), §87 (discs),js/realms/rokgard/games/lines/core/ai.js(levels:LEVELS; at the time of the measurement, game version 0.62.3,js/realms/taflgard/games/chains/core/chains-ai.js),docs/TUNING.md§39 (measurements of the AI, the advantage of the first move in the other variants). Commands of the game that were run on 30 October 2026 under the old namechains.js(the tool is nowtools/realms/rokgard/games/lines/lines.js):node tools/realms/taflgard/games/chains/chains.js --solved(1,386 games, 0 losses, 900 wins, 486 draws, 30 October 2026) and... --matrix --games 40 --variant tic(levels 2 and 3 against each other 100% draws). - **Wikipedia, Tic-tac-toe** (read on 30 October 2026, page last edited on 21 October 2026): 765 positions up to symmetry, 138 final positions and their split 91 / 44 / 3, the drawing replies after each opening and the order of the eight rules (the program of Newell and Simon, 1972; the page refers to the work of Crowley and Siegler, 1993). The page does not give the shares of wins in random play; those come from my own calculation.
- **Wikipedia, Game complexity** (read on 30 October 2026, last edited on 22 October 2026, the section on tic-tac-toe): 5,478 positions, 765 up to symmetry, 255,168 games and 26,830 games up to symmetry. Both pages were read in the whole text; the original works they refer to (Newell and Simon, Crowley and Siegler) were not opened.
- The numbers of the other variants in the section on the first and the second player are from
docs/TUNING.md§39 (40 games per pair). They were not re-checked in this article.
What could not be verified: the claims about the program of Newell and Simon from the original work (only from Wikipedia), the origin of the numbers 255,168 and 5,478 on Wikipedia (my own calculation confirmed them; its sources were not opened), the impression of real players from the Weak level (the results are for a player with a given strategy, not for a human), the advantage of starting in the other variants beyond the measurements of the AI (in the documentation of the game). The names of the game and of its variants are working names.