gekro
GitHub LinkedIn

Dots and Boxes

Play Dots and Boxes against a computer at three levels, or against a friend on the same screen. Three board sizes, undo, keyboard play, and a session tally.

A You 0

B Computer 0

Turn Your turn (A)

  • A: You, blue lines and boxes, moves first
  • B: The computer, amber lines and boxes

Lines left 0

This session -

Your wins here 0

Tap or click the gap between two dots to draw a line. Close the fourth side of a box to claim it with your letter, then draw again. On a keyboard, arrow keys move between lines (Home and End jump along a row), Enter or Space draws. Undo takes back your last turn, including the computer's reply and any chain it took. Every line has a tap area at least 28 px thick; on a small phone the 5 x 5 board has small boxes, so use 4 x 4 or 3 x 3 if taps miss.

How the computer plays

At every level the computer takes a box when it can. Easy otherwise draws a random line. Medium avoids drawing the third side of a box while a line that gives nothing away exists, and when every line gives something away it picks the one that hands you the fewest boxes. Hard plays like Medium until 16 or fewer lines are left. From there it solves the rest of the game exactly, trying every order of the remaining lines (about a second at most), which is how it finds the double-dealing move: declining the last two boxes of a chain so that you must open the next one. Before that point it opens the smallest chain first and declines the last two boxes of a chain when keeping control is worth more than those two boxes. Hard is strong at the end of a game and only rule-based before it, so it can be beaten.

As-is, no warranty. These apps are free under their listed license and run entirely in your browser. Use at your own risk — don't blame me if your PC catches fire, your dog runs away, or the math turns out wrong. Verify anything that actually matters. None of this is professional financial, medical, legal, or engineering advice.

© 2026 Rohit Burani · MIT · Built at gekro.com · View source ↗

Guide

How to play

The board is a grid of dots. Players take turns drawing one line between two neighbouring dots. When a line completes the fourth side of a box, that player claims the box, which is marked with their letter (A or B), and must draw again. When every line is drawn, whoever holds more boxes wins. The 3 x 3 and 5 x 5 boards have an odd number of boxes (9 and 25), so they cannot end in a tie; the 4 x 4 board has 16 and can.

Choose a Board size: 3 x 3 boxes (4 x 4 dots, 24 lines), 4 x 4 boxes (5 x 5 dots, 40 lines) or 5 x 5 boxes (6 x 6 dots, 60 lines). Pick an Opponent: the computer, or Two players on the same screen. Against the computer, choose a Difficulty and whether you play first (A) or second (B). A is always the player who moves first, with blue lines; B has amber lines. A box you close is filled in your colour and ringed, with your letter in the middle. Changing any setting starts a new game.

  • Mouse or touch: press the gap between two dots. Each line has a tap area about 28 px thick (24 px on the 5 x 5 board at 320 px wide), and the line you or the computer drew last has a ring around it.
  • Keyboard: arrow keys move between lines, Home and End jump to the ends of a row, Enter or Space draws the line.
  • Undo takes back your last turn, including the computer’s reply and any chain of boxes it took, so it is your turn again. With two players it takes back the last turn, which may be a chain, and the status line names whose turn it is again. Once a game is over, undo is off.
  • Copy result copies a short summary of your session tally and the page address.

The board sits on a soft violet and teal panel with violet dots. A box shows the letter of whoever claimed it, not only a colour, so the two sides can be told apart without relying on colour. The result is also written out in words below the board.

How it works

A board of n x n boxes has n x n boxes and 2 x n x (n + 1) lines: n x (n + 1) horizontal and the same number vertical. The whole rule set is one small function: draw a line, count the boxes it completes (a single line can complete two), give those boxes to the mover, and hand over the turn only if none were completed. The test suite checks the line and box counts for all three sizes, that a game of only legal lines always ends after exactly the total number of lines, and that the two scores always add up to the number of boxes.

The computer

LevelWhat it does
EasyTakes a box when it can, otherwise draws a random line
MediumTakes a box when it can; otherwise draws a line that does not give a box away; if every line does, the one that gives away the fewest
HardMedium, plus end-game chain control

A line is safe when no box beside it already has two sides drawn, because drawing it would make a third side and let the other player complete the box. Medium plays a safe line whenever one exists, and the tests check that on hundreds of positions.

Near the end of a game the safe lines run out and every move opens something. The boxes with two sides drawn then form chains (runs of boxes you can take one after another) and loops (rings of boxes). The engine finds them by linking boxes that have two sides drawn and share an undrawn line.

Hard uses that in two ways:

  1. Exact solving. When 16 or fewer lines are undrawn, Hard works out the best result of every possible continuation. Only the set of undrawn lines matters, so the search stores each position’s value once (a memo table indexed by which lines are drawn) instead of recomputing it. This is the part that finds the double-dealing move: when you are about to take the last two boxes of a chain, you can instead draw the far end of the pair. That hands the opponent both boxes with one line, but they then have to open the next chain for you. Sometimes that is worth more than the two boxes, and the exact search sees when. The search is cut off after one second and Hard then falls back to the rules below. A test constructs an endgame where declining the last two boxes is the only winning move and checks that Hard plays it.
  2. Rules before that point. With more lines left it plays safe lines, then opens the smallest chain first (a chain of two from the middle, so it cannot be declined), and declines the last two boxes of a chain when a quick estimate says keeping control is worth more than those two boxes.

The exact solver is checked against a plain recursive search with no memo table on random positions with up to seven lines left, and the two always agree. On a fixed set of seeded games, Hard beat Easy in 30 of 30 in the last run, from either seat and on the 3 x 3, 4 x 4 and 5 x 5 boards; the test requires at least 24 of 30 (80%). The search stops after one second, and the test allows a Hard move with 16 lines left up to two and a half seconds to leave room for slow machines.

Computer moves are spaced about 350 ms apart so a chain of boxes can be followed, and happen at once if your system asks for reduced motion. The search runs on the page itself; no background thread is used.

Privacy and storage

Nothing is uploaded and there is no account. The number of games you have won against the computer, for each board size and level, is saved in your browser’s local storage as small whole numbers inside a small JSON object under one key. Only a finished win against the computer counts, and only if you did not use undo in that game. Ties, losses and two-player games are not saved. The session tally and the current game are kept in memory only, so they reset when you leave the page. The address records only the board size, opponent, level and seat. Clearing site data removes the saved wins.

Limits

  • Hard is exact only for the last 16 lines. Before that it follows rules of thumb about chains, so a long plan can still beat it. It is a good opponent, not a perfect one.
  • Easy is meant to be beatable: it never looks ahead and happily gives boxes away.
  • The 5 x 5 board has small boxes on a narrow phone. Lines keep a tap area of 28 px, or 24 px at 320 px wide, but 4 x 4 or 3 x 3 is easier to play there.
  • With two players there is no clock, no names and no saved games.
  • The tally and the current game are not saved if you reload the page.
  • Undo is not available after the game ends.

Everything runs locally in your browser.

All Fun tools →

For informational purposes only. Not financial, medical, or legal advice. You are solely responsible for how you use these tools.