AlgoViz

Design tic-tac-toe

easy 70 min

Model 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. 1

    A line has n squares

    Row, column or one of two diagonals. True for a square board.

  2. 2

    A square is counted once

    Board.put refuses a taken square, so no square adds twice to a line.

  3. 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. 4

    And undo keeps it

    remove uses the same list of lines as add, 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

Methodn = 3n = 100n = 1,000Extra memory
Whole board: (2n + 2) lines of n2420,2002,002,000none
Lines of the last move: up to 4n124004,000none
Counters: up to 44442n + 2 for each mark
For n = 3 the difference is 20 looks, less than a microsecond. Say that. The reason to count is the question "any size", and nothing else.

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.

RuleRight toolLooks for a move
Full line, any nCounters4
k in a row on n by n, k < nWalk from the last moveup to 8(k - 1)
Any shape from a listTest each shape that contains the squarenumber of such shapes times its size
3 by 3 only, speed mattersNine bits and a table of 512 answers1

Bits

512 answers

Python
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])
One number for each mark. Putting is an or, the win check is one look into a table, and taking back is a xor. This is the form that programs use when they look at many positions.
Edit and run it in your browser