Design tic-tac-toe
easy 70 minModel a game everybody knows: marks on a grid, turns, a full line wins.
Watch lesson4:32
UML design
Two diagrams, and five minutes at the board
The point: all arrows go out of Game. Nobody points back, so board and counts can be tested alone.
The design to reach: the board stores, the counts see wins, the game enforces
1/5The referee
Game knows whose turn it is and whether the game goes on. It is the only way to change anything, so a rule checked here is a rule.
owns (dies together)uses
Draw
In this order
- 1
Game with play
Write the full signature.
play(mark, row, col): Statusalready answers three questions of the interviewer. - 2
Board below it
putandclear, a pair. Say that the pair is for undo. - 3
Move beside it
The word "in order" on the arrow is the history.
- 4
LineCounts
addandremove, the same pair again. - 5
Two enums
Four statuses. Point at the order of checks.
Compare
The version with everything in it
The design to avoid: the game is inside the screen, and the winner is inside the board
1/4One class from keyboard to verdict
Reading, printing, turns and winning in one class. There is no way to ask who has won without somebody typing.
owns (dies together)extends (is a)
| Wish | Bad design changes | Good design changes |
|---|---|---|
| Size 4 | Three loops and the printing | One argument |
| Undo | Board, counter, player, loop | Nothing: it is there |
| A computer | makeMove with the real board | A new caller |
| A web page | run, and a copy of the rules | A new caller |
| No diagonals | checkWinner | _lines |