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

Requirements

Five rules, four qualities, and the ones left out

The point: write the rules as sentences that can be false. Then each becomes a check and a test.

Functional

What it must do

RuleOwnerTest
X and O alternate, X begins unless told otherwiseGameX twice is refused
A mark goes on an empty square inside the boardBoard, asked by GameTaken, -1, n
A full row, column or diagonal of one mark winsLineCountsAll 2n + 2 lines, for both marks
A full board without such a line is a drawGameThe nine squares of a drawn game
After the end no move is acceptedGameOne more move
The last move can be taken back, also after the endGameWin, undo, play on

Quality

How well

QualityNumberWhat follows
Time for a moveThe same for n = 3 and n = 1,000Counters instead of looking
Memoryn squared plus 4n + 4 numbersFine: n = 1,000 is a million squares
RefusalChanges nothingAll checks first
TestableWithout keyboard or screenNo input or output in any class

Left out

Named and not built

  1. 1

    Names and scores

    They outlive a game, so they live outside. A Match can hold two names and a list of results.

  2. 2

    Screens

    They call rows() and play.

  3. 3

    Computer player

    An interface with one method, when it is wanted.

  4. 4

    Network

    Tokens decide who may say X. The game stays the same.