Design tic-tac-toe
easy 70 minModel 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
| Rule | Owner | Test |
|---|---|---|
| X and O alternate, X begins unless told otherwise | Game | X twice is refused |
| A mark goes on an empty square inside the board | Board, asked by Game | Taken, -1, n |
| A full row, column or diagonal of one mark wins | LineCounts | All 2n + 2 lines, for both marks |
| A full board without such a line is a draw | Game | The nine squares of a drawn game |
| After the end no move is accepted | Game | One more move |
| The last move can be taken back, also after the end | Game | Win, undo, play on |
Quality
How well
| Quality | Number | What follows |
|---|---|---|
| Time for a move | The same for n = 3 and n = 1,000 | Counters instead of looking |
| Memory | n squared plus 4n + 4 numbers | Fine: n = 1,000 is a million squares |
| Refusal | Changes nothing | All checks first |
| Testable | Without keyboard or screen | No input or output in any class |
Left out
Named and not built
- 1
Names and scores
They outlive a game, so they live outside. A
Matchcan hold two names and a list of results. - 2
Screens
They call
rows()andplay. - 3
Computer player
An interface with one method, when it is wanted.
- 4
Network
Tokens decide who may say X. The game stays the same.