← Dots and Boxes against the computer

Dots and Boxes Strategy: Chains, the Double-Cross and How to Beat the Computer

The numbers on this page come from simulations of the game as it is played here — 6×6 dots, 25 boxes, you move first — using the page's own code for all three computer levels: 10,000 games for each figure on Easy and Hard, 1,000 to 3,000 against Expert. Small endgames were solved exactly by exhaustive search, and the winning line against Expert was replayed move by move on the real engine.
The short answer

Dots and Boxes is won in the second half, when the safe lines run out and whole chains change hands. Three habits do nearly all the work: never draw the third side of a box while a safe line exists, give away the shortest chain when you must, and when you are handed a chain stop two boxes short, so that the computer has to open the next one. In simulation that wins 97% of games against Easy, 88% against Hard and 75% against Expert here — and Expert, which involves no chance at all, loses 24:1 to one fixed list of moves every time.

The quiet half: no third side

A line is safe if it leaves no box with three sides. While both players draw only safe lines, nobody takes anything. On this board that lasts for half the game: with both sides picking safe lines at random, the safe lines ran out after 30 of the 60 lines on average, and never earlier than line 26 or later than line 34 in 3,000 games.

What a careless third side costs is easy to measure against Easy, the weakest level:

Your playWins against Easy
Any line at random0 of 10,000 (less than one box a game)
Take every box on offer, otherwise at random3.6%
The same, but only safe lines while one exists47%

The last row is exactly how Easy itself plays, so 47% is what you get for doing no more than it does. Everything above that comes from the second half of the game.

When the safe lines run out, the board has typically broken into about three long chains (three boxes or more) and three short ones, and the longest chain is 7 or 8 boxes on average. One position in five contains a loop. And in 91% of positions some box still has three or four open sides — a junction where chains meet — so the picture goes on changing as the first sacrifices are made.

When you have to give something away

Sooner or later every line left gives the opponent a box. Then count what each line gives and open the cheapest. That is the only difference between Hard and Easy on this page, and for a player it is the biggest single step: against Easy it lifts the win rate from 47% to 85%, against Hard from 7% to 41%, against Expert from 4% to 25%.

A chain of two boxes needs extra care, because it has three lines you can open it with, and they are not the same:

  • At either end. The opponent may take both boxes — or draw the line at the far end instead, taking nothing and leaving both boxes to you. You collect two boxes and then have to open the next chain yourself.
  • In the middle, between the two boxes. Now each box has three sides of its own, and no line gives them back. The opponent takes both and it is he who has to open something.

Expert knows the difference. In a test position with a two-box chain and four chains of five, we opened the short chain at the end: Expert declined both boxes. Opened in the middle, it took both and then had to open a chain of five. Easy and Hard take the two boxes either way.

Against Easy and Hard, therefore, it does not matter where you open a two-box chain. Against Expert it does: the same player won 75.5% of 3,000 games opening two-box chains in the middle and 68% of 1,500 opening them anywhere.

All but two: what the double-cross is worth

When you are handed a chain, the natural thing is to take all of it — and then, with no safe line left, to open the next chain for your opponent. The alternative is to take all but the last two boxes and draw the line that leaves those two to him with a single stroke. He takes them, and it is still he who must open the next chain. You pay two boxes per chain to stay in control, and you take the last chain whole.

How much that is worth can be calculated exactly. Take a position in which only rows of five boxes are left — every horizontal line drawn, the vertical ones not — and it is your opponent's turn to open one. We solved these positions by exhaustive search:

Chains of five leftBoth sides take everythingBest play (exact)
25:58:2 to you
310:5 to you11:4 to you
410:1014:6 to you

With four chains the trick turns a level ending into a win by eight boxes. In whole games the effect is just as plain. A player who takes boxes, plays safe lines at random and sacrifices the cheapest chain wins 86% against Easy, 41% against Hard and 24% against Expert. Add all-but-two and the figures become 97%, 88% and 75%; against Hard that is 4.7 more boxes a game on average.

  • Do not decline when only short chains are left. Stop two boxes short only if the cheapest thing your opponent could open next is worth three boxes or more. Declining every time, whatever is left, wins 91.5% against Easy instead of 96.7%.
  • A loop costs four, not two. In a closed ring every line closes the last pair, so to keep control you must leave the last four boxes, as two pairs. In a test position with a ring of four boxes and three chains of five, the player handed the ring comes out 11:8 ahead by declining all four and 8:11 behind by taking them (exact).
  • Take the last chain whole. After it there is nothing left to open.

The chain rule on this board

The chain rule is the part of the theory that tells you, before any box is taken, who will end up in control. Its exact form, from Berlekamp's book: the number of dots plus the number of double-crossed moves — lines that close two boxes at once — equals the number of turns in the game. This board has 36 dots. You move first, so you have the last turn exactly when the number of double-crossed moves is odd. We checked 4,500 simulated games: the identity held in every one.

The last turn is usually the last and largest chain. Between two players who both follow Hard's rule, the side with the last turn won 90% of 2,000 games and the other side 3%.

The practical form of the rule follows from it: with 36 dots the first player — you — wants an even number of long chains on the board, and the computer wants an odd number. Loops are not counted. One piece of this can be seen directly. Once the board consists of separate chains only, the lines left number 25 plus the number of chains, so the computer has to open first exactly when the number of chains of all lengths is even. That held in all 357 such positions in our games.

Now the honest part. Against the three levels here, the chain rule decides very little:

  • Counting long chains at the moment the safe lines run out predicted nothing: the all-but-two player won 88.0% against Hard with an odd count and 87.8% with an even one (3,000 games), and 75.6% and 75.3% against Expert (1,500).
  • Who has to make the first sacrifice barely matters either: 89.9% against 87.0% with Hard, 76.4% against 74.6% with Expert.
  • A player who also searched the last twelve safe lines to make sure the computer ran out first achieved that in about nine games in ten and gained at most one point of win rate.

The reason is that the rule assumes an opponent who fights for control. Easy and Hard give it back the first time they take a chain, and Expert gives it back often enough. Against them, what you do with a chain once you have it matters far more than who opens first. Against a strong human, count the chains.

How each level handles chains, and how to beat it

EasyHardExpert
Forced sacrificea random linethe cheapestthe cheapest
Handed a chaintakes all of ittakes all of ittakes all but two when that pays
Handed a looptakes all fourtakes all fourtakes all four
Two-box chain opened at the endtakes bothtakes bothdeclines when that pays
Same moves, same replies?nonoyes

And this is how simple ways of playing do against each level (you move first; choices between equal lines are made at random):

Your playvs Easyvs Hardvs Expert
Any line at random0%0%0%
Take every box, otherwise at random3.6%0.3%0.5%
+ only safe lines while one exists (Easy's own play)47%7%4%
+ the cheapest sacrifice (Hard's own play)85%41%25%
+ all but two when a long chain is left; two-box chains opened in the middle97%88%75%
+ counting the last twelve safe lines98%88%76%

10,000 games for each figure against Easy and Hard (3,000 in the last row), 3,000 against Expert (1,000 in the last row). Note the 41%: playing exactly as Hard does and moving first, you win fewer than half the games — 40% in a separate run of 5,000.

Easy and Hard lose to the same thing. They take every box they are offered, so the first long chain you receive gives you control for the rest of the game: take all but two, let the computer have the pair, and it has to open the next chain. Hard only makes you wait longer for that first chain, because it opens the shortest one it has.

Expert plays the double-cross itself, but it has three weaknesses: it takes loops whole; it picks its sacrifice by counting what a greedy player would take; and while more than fourteen lines are left it follows rules instead of calculating. In 500 games against the all-but-two player it opened 2.4 long chains a game and the player 0.8.

Expert also involves no chance, so one won game is won for ever. It answers almost any first move with the top-left line and goes on along the top edge. Dots are named here by column, a–f from the left, and row, 1–6 from the top (the game shows no labels); a1-b1 is the top-left horizontal line.

  1. Draw these eighteen lines in order: a1-a2, d5-e5, f4-f5, b4-c4, f1-f2, a4-b4, d4-d5, e2-e3, c2-c3, c5-d5, f3-f4, a4-a5, a2-a3, d3-d4, d1-d2, b2-c2, e4-f4, b3-b4.
  2. Expert answers the first seventeen with b1-c1, c1-d1, d1-e1, e1-f1, a3-b3, c3-d3, d3-e3, a6-b6, b6-c6, e6-f6, a1-b1, c6-d6, d6-e6, f2-f3, a5-a6, f5-f6, b5-c5. Your eighteenth line, b3-b4, gives up one box: Expert takes it with a3-a4 and then has to play a2-b2.
  3. That line opens everything that is left. Take all 24 boxes with 23 lines, each of which closes a box: b1-b2, c1-c2, c2-d2, b2-b3, b3-c3, d2-d3, d2-e2, e1-e2, e2-f2, e3-f3, c3-c4, c4-d4, e3-e4, d4-e4, c4-c5, b4-b5, a5-b5, e4-e5, e5-f5, b5-b6, c5-c6, d5-d6, e5-e6.

The game ends 24:1. Expert never has fewer than 24 lines left when it chooses, so its timed endgame search is not used and the speed of your device changes nothing. If Expert ever answers differently, the game's code has changed and the line no longer applies.

The rules of thumb, in order of value

  1. While a safe line exists, draw nothing else. No third sides.
  2. When you must sacrifice, count what each line gives away and open the cheapest. Open a two-box chain in the middle.
  3. Handed a chain, take all but two boxes if the cheapest thing left after it is worth three or more. In a loop, leave four.
  4. Take the last chain whole.
  5. Against a strong opponent, count long chains: moving first on this board, you want an even number.

The first two rules win 85% of games against Easy but only 41% against Hard and 25% against Expert; the third takes those to 97%, 88% and 75%.

Questions and answers

How do you win at Dots and Boxes?

Draw only safe lines while you can, sacrifice the shortest chain when you cannot, and when you are given a chain take all but the last two boxes so your opponent has to open the next one. Against the computer here those three rules win 97% of simulated games on Easy, 88% on Hard and 75% on Expert.

What is the double-cross in Dots and Boxes?

Declining the last two boxes of a chain: instead of taking them, you draw the line that leaves both to your opponent, who closes them with one stroke and then has to open the next chain. It costs two boxes (four in a loop) and keeps you in control. With four chains of five left it turns a 10:10 ending into 14:6, by exact calculation.

What is the chain rule?

Dots plus double-crossed moves equals turns. On a board with 36 dots, like this one, the first player gets the last turn when the number of long chains is even, the second when it is odd. The identity held in all 4,500 games we simulated, but against the computer levels here the count of chains predicted almost nothing, because they give control away.

Is it better to go first or second in Dots and Boxes?

It depends on the board. We solved the small ones exactly: on 2×2 boxes the first player wins 3:1, on 3×3 the second wins 6:3. The 5×5 board here is too large for that. In our games two identical players following Hard's rule split 40:60 against the one who moved first (5,000 games).

Can you beat the computer at Dots and Boxes?

Yes, on every level here. Easy and Hard never decline a box, so taking all but two of the first long chain they give you usually wins the game. Expert has no randomness: the eighteen lines listed on this page, followed by taking everything it opens, beat it 24:1 every time.

When should you sacrifice boxes?

Only when no safe line is left, and then as few as possible: against Easy, opening the cheapest chain instead of a random one raises the win rate from 47% to 85%. The other time is the double-cross, where you give up two boxes on purpose to make your opponent open the next chain.

Play Dots and Boxes against the computer →
Where to go from here
To cite this page: “Dots and Boxes Strategy: Chains, the Double-Cross and How to Beat the Computer”, You vs Computer, https://youvscomputer.com/games/dots-and-boxes-strategy.html