Design tic-tac-toe
easy 70 minModel a game everybody knows: marks on a grid, turns, a full line wins.
Watch lesson4:32
Grow the design
Start with the smallest system that works. Change it only when something breaks.
Grow it
Five steps, each forced by a sentence
| Step | Forced by | Change | Cost |
|---|---|---|---|
| 1. Nine letters and a turn | The rules | One class, forty lines | Everything in one place |
2. Board | "Any size" | Squares apart from rules | A class |
3. LineCounts | "Any size" and a move in O(1) | Counters | They must follow every change |
4. Move and undo | "Take back" | A list and the opposites | Each change needs one |
5. Player, Match | "Against the computer" | An interface around the game | The game must not trust it |
The order inside play
The class diagram is on the UML page of the solution.