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

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

PatternWhereWithout it
Command, in smallMove with play and undoCopies of the whole board
FacadeGame in front of board and countsCallers who forget to count
State as an enumStatusFlags that disagree
Null is not allowed outrows() gives textCallers who test for None

Refused

And when I would take them

PatternWhy not nowWhen
Strategy for win rulesOne ruleTwo rules in one program: hand LineCounts in
Strategy for playersNobody choosesThe day the computer plays
MementoA move is smaller than a pictureJump to any position, or share one
Observerplay returns the statusTwo screens and a sound
StateOne eventPauses, offers of a draw, clocks
SingletonTwo games are two objectsNever

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

LetterProof here
SNo diagonals? Only _lines changes. Other storage? Only Board
OA computer is a new caller, Game is not opened
LEvery player may be put in either seat
IA screen needs rows and status, a player needs legal: small questions, no big interface
DGame makes its parts itself. One rule, so nothing to invert yet. Say that this is a choice