Snap¶
Snap is the first game because its rules are small enough to hold in your head, which leaves room for learning the language. These are the rules, as the engine's rules file states them at the top:
- A shuffled 52-card deck is dealt alternately, face down.
- Players alternately turn their top face-down card onto their own face-up pile.
- Either player may call snap at any time. It is valid if both face-up piles are non-empty and their top cards have the same rank: the caller takes both face-up piles, face down, under their stack. A false call gives the caller's face-up pile to the opponent, under their stack.
- A player who must flip with no face-down cards first turns their face-up pile over to form a new stack.
- A player with no cards at all loses. Reaching
maxFlipsflips is a draw, since Snap need not end.
The last rule is there because a game of Snap can go on forever, and an engine that has to answer every move needs games that end.
The files¶
Snap lives in two files, and its proofs in two more:
| File | What it holds |
|---|---|
Games/Snap/Rules.lean |
The rules: the cards, the state of a game, and step, which applies a move |
Games/Snap/Game.lean |
How the engine shows Snap to players: what each one sees, the moves offered, the words on screen |
Proofs/Snap/Guarantees.lean |
What the rules are proven to ensure, stated in plain words |
Proofs/Snap/Bookkeeping.lean |
The small facts those proofs are built from |
This section reads Rules.lean from top to bottom, learning the language as
it goes, then tests it, changes it, and finally proves things about it.
In this section¶
- Cards and hands: values, types and structures.
- The deck: functions and lists.
- Choices: types with alternatives, and pattern matching.
- A move: the state of a game, and the function that changes it.
- Testing the rules: checking examples, and playing thousands of random games.
- Changing the rules: making your own Snap.
- First proofs: statements, and how to prove them.
- What always holds: no card is ever lost, and no game goes past its flip limit.
- What players can't see: proving that the face-down cards stay secret.