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

Remember it

The few things worth knowing cold, and how to make them stick.

Memory hook

One pebble, four lines. Ask first, then act.

Pebbles and shells in the sand, with small sticks beside the rows that count pebbles. Nobody looks at the whole grid: a pebble moves its own sticks. The tide takes the grid and not the score.

Cheat sheet

Rules and owners
Board empty, Game turn and end, LineCounts lines
Order in play
over, turn, inside, taken, store, count, win, full, pass
Lines
2n + 2, at most 4 through a square
Win
count == n
Undo
pop, clear, uncount, turn = move.mark, PLAYING
Games
255,168 (9! = 362,880 is not it)
Positions
5,478, or 765 up to symmetry
Best play
a draw
k in a row
walk, up to 8(k - 1) looks

Spot it when you see

  • "Has somebody won?": what did the last move change?
  • "Take back": does each change have an opposite?
  • "Computer": a caller with a wish.
  • "Any size": no constants.

Tricks that stick

  • SAND: Squares store, Ask before act, N on a line, Do has an undo.
  • put and clear, add and remove, play and undo.
  • `if` and `if` for the diagonals, never `elif`.

Do not forget

  • `[[x] * 3] * 3`.
  • Index -1.
  • Full before win.
  • Switching the turn in undo.
  • Rules in the screen.
  • The real board for players.