Design tic-tac-toe
easy 70 minModel a game everybody knows: marks on a grid, turns, a full line wins.
Watch lesson4:32
Win in O(1)
Why counting is right, where it breaks, and how fast it must be
The point: a line is won when one mark has been put on it n times. Counting is right because a square takes one mark, once, and each move is seen exactly once.
Argue
Why equal to n means full
- 1
A line has n squares
Row, column or one of two diagonals. True for a square board.
- 2
A square is counted once
Board.putrefuses a taken square, so no square adds twice to a line. - 3
So the count is the number of squares with this mark
It cannot be larger than n, and n means all of them.
- 4
And undo keeps it
removeuses the same list of lines asadd, so the sentence is true again after it.
The argument uses put before add. Swap the two lines in play and a refused move has already been counted. The bug shows much later, as a win with a hole in it.
Compare
Looks per move
| Method | n = 3 | n = 100 | n = 1,000 | Extra memory |
|---|---|---|---|---|
| Whole board: (2n + 2) lines of n | 24 | 20,200 | 2,002,000 | none |
| Lines of the last move: up to 4n | 12 | 400 | 4,000 | none |
| Counters: up to 4 | 4 | 4 | 4 | 2n + 2 for each mark |
Forms
Three ways to store the counters
How are the counters stored?
WorksOne number for each line, plus for X and minus for O
- How
- n or -n wins.
- Limit
- Two marks only. A sum says nothing about who is on the line. With wins of a full line it is right, because a sum of n needs n times plus.
- Choose when
- Two players, for ever.
StrongOne number for each mark and line
- How
- The base design.
- Limit
- Twice the memory: for n = 1,000 about 4,000 numbers.
- Choose when
- The default. A third player is a third column.
BreaksOnly for lines that are still alive, deleted when blocked
- How
- Drop a line when both marks are on it.
- Limit
- Undo must bring deleted counters back with the right value.
- Choose when
- Never with undo. Without it, it detects an early draw: no line alive.
Breaks
k in a row kills the counter
On a board of 10 with 5 in a row, a row can hold five X and no win: X X . X X . X . . .. Counting forgets where. The right tool is to walk from the last move in both directions, at most 2(k - 1) squares for each of four directions. That walk has its home in four in a row.
| Rule | Right tool | Looks for a move |
|---|---|---|
| Full line, any n | Counters | 4 |
| k in a row on n by n, k < n | Walk from the last move | up to 8(k - 1) |
| Any shape from a list | Test each shape that contains the square | number of such shapes times its size |
| 3 by 3 only, speed matters | Nine bits and a table of 512 answers | 1 |
Bits
512 answers
LINES = [0b111_000_000, 0b000_111_000, 0b000_000_111, 0b100_100_100, 0b010_010_010, 0b001_001_001, 0b100_010_001, 0b001_010_100]WINS = [any(m & line == line for line in LINES) for m in range(512)] # made oncex = 0for square in (4, 2, 3, 5): # the middle, top right, left and right of the middle row x |= 1 << (8 - square) print(f"{x:09b}", WINS[x])