Design tic-tac-toe
easy 70 minModel a game everybody knows: marks on a grid, turns, a full line wins.
Watch lesson4:32
Patterns
Command without the name, and three that stay out
The point: the useful pattern here is a move that can be done and undone. The rest would be decoration.
Used
Already in the code
Refused
And when I would take them
| Pattern | Why not now | When |
|---|---|---|
| Strategy for win rules | One rule | Two rules in one program: hand LineCounts in |
| Strategy for players | Nobody chooses | The day the computer plays |
| Memento | A move is smaller than a picture | Jump to any position, or share one |
| Observer | play returns the status | Two screens and a sound |
| State | One event | Pauses, offers of a draw, clocks |
| Singleton | Two games are two objects | Never |
Undo by opposites
- Stores nine moves for a whole game
- Four lines of code
- Each change needs an owner with an opposite
Undo by pictures
- Stores a board for each move: n squared each
- Nothing to get wrong
- Right when boards are small values, as in a user interface
Principles
SOLID, one proof each
| Letter | Proof here |
|---|---|
| S | No diagonals? Only _lines changes. Other storage? Only Board |
| O | A computer is a new caller, Game is not opened |
| L | Every player may be put in either seat |
| I | A screen needs rows and status, a player needs legal: small questions, no big interface |
| D | Game makes its parts itself. One rule, so nothing to invert yet. Say that this is a choice |