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

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

StepForced byChangeCost
1. Nine letters and a turnThe rulesOne class, forty linesEverything in one place
2. Board"Any size"Squares apart from rulesA class
3. LineCounts"Any size" and a move in O(1)CountersThey must follow every change
4. Move and undo"Take back"A list and the oppositesEach change needs one
5. Player, Match"Against the computer"An interface around the gameThe game must not trust it

The order inside play

Everything above "Store" is a question. Everything below cannot fail.

The class diagram is on the UML page of the solution.