Jigsaw Puzzle: low-level design

Design a jigsaw puzzle game. A picture is cut into pieces, the player drags pieces onto a board, and the game tells whether a piece fits where it is placed. The puzzle is done when every piece is in place.

Stage 1: Edges and pieces

Say what an edge is and what a piece is.

'Does this piece fit?' has no model to answer it. `Edge` has a shape (flat, tab, blank) and an id for the exact curve. `matches` is true when one is a tab, the other a blank and the ids agree. A `Piece` has four edges. Fit becomes a comparison of two edges, with no coordinates involved. The id lets two pieces with the same shape still be told apart.

Stage 2: Cells and the board

Give pieces somewhere to go.

Placement needs a grid and a notion of neighbours. `Board` is a grid of `Cell`s. `neighbours` returns the cells that touch, and `isComplete` checks that every cell holds a piece. The board owns positions, the piece owns shape, and neither knows the other's detail. A 2-D array is enough. A sparse map helps only for very large puzzles.

Stage 3: The fit rule: Strategy

Decide if a piece fits a cell.

Edge matching, border rules and image matching are different kinds of 'fit'. A `FitRule` with `fits`. `ShapeFit` checks the touching edges of each neighbour and that border pieces are on the border. A harder mode (match the image too) is a new rule (**Strategy**). Only one rule exists today. The interface costs a few lines and the variants are easy to imagine.

Stage 4: The game

Run a puzzle: place, remove, finish.

Pieces start in a tray, and nobody counts moves or says when the puzzle is solved. `PuzzleGame` keeps a tray of unplaced pieces and a board. `place` moves a piece from the tray to a cell if the board accepts it. A piece lives in exactly one of the tray or a cell, so it cannot be in two places. Undo would store each move (memento) so it can be replayed backwards.