The cards and the table¶
A Snap card was a structure: a rank and a suit. A Lol card is different. There are eight kinds, and each has its own value, its own count in the deck and its own effect. So a Lol card is one of eight things.
Eight alternatives¶
inductive Card where
| raider | seer | holmgang | shieldmaiden | loki | trader | valkyrie | runestone
deriving DecidableEq, Repr, Inhabited
This is an inductive type, like Snap's Action, but with eight
constructors, written on one line with a | before each. None of them
carries any data: a Seer is a Seer. The full names are Card.raider,
Card.seer and so on.
/-- info: Games.LoL.Card.seer -/
#guard_msgs in
#eval Card.seer
/-- info: 8 -/
#guard_msgs in
#eval Card.runestone.value
/-- info: [5, 2, 2, 2, 2, 1, 1, 1] -/
#guard_msgs in
#eval Card.all.map Card.count
The first #eval shows that Repr, which deriving wrote for us, prints a
card by its full name: Games.LoL.Card.seer. Lol lives in the namespace
Games.LoL, so that is the card's name everywhere.
Functions, case by case¶
A card's value is a fact about each kind of card, so it is written as a list of cases:
def value : Card → Nat
| raider => 1 | seer => 2 | holmgang => 3 | shieldmaiden => 4
| loki => 5 | trader => 6 | valkyrie => 7 | runestone => 8
This is the style of Snap's other, with eight cases instead of two, packed
two to four on a line. Card → Nat says value takes a card and gives back
a number. It is defined between namespace Card and end Card, so its full
name is Card.value, and you can call it with a dot: c.value. Inside the
namespace, raider means Card.raider, so the patterns need no dots.
Lean checks that the cases cover every card. Leave one out and the
definition is refused, with a message naming the card you forgot. Add a
ninth card to Card, and every function written like this stops building
until you've said what it does for the new card. That is a large part of
what makes changing rules safe.
Each kind's count in the deck is written the same way:
def count : Card → Nat
| raider => 5 | seer => 2 | holmgang => 2 | shieldmaiden => 2 | loki => 2
| trader => 1 | valkyrie => 1 | runestone => 1
and there is a list of every kind, in order of value:
Nothing checks that all lists every card exactly once. If you add a card,
you have to remember this list yourself.
Building the deck¶
The deck is every kind of card, each repeated as many times as its count.
Two list functions do that. List.replicate n x is a list of n copies of
x. l.flatMap f applies f to every element of l, which gives a list
for each, and joins those lists into one.
/-- info: ["loki", "loki"] -/
#guard_msgs in
#eval (List.replicate 2 Card.loki).map Card.name
/-- info: [1, 2, 2, 3, 3, 3] -/
#guard_msgs in
#eval [1, 2, 3].flatMap fun n => List.replicate n n
(Card.name gives a card's name as a string; mapping it over a list is
easier to read than the full names.) With those two, the deck is one line:
/-- The sixteen cards. -/
def fullDeck : List Card := Card.all.flatMap fun c => List.replicate c.count c
fun c => List.replicate c.count c is a function written in place, without
a name: given c, it gives c.count copies of c.
/-- info: ["raider", "raider", "raider", "raider", "raider", "seer", "seer", "holmgang", "holmgang", "shieldmaiden",
"shieldmaiden", "loki", "loki", "trader", "valkyrie", "runestone"] -/
#guard_msgs in
#eval fullDeck.map Card.name
example : fullDeck.length = 16 := rfl
example : fullDeck.count .raider = 5 := rfl
The last two lines are proofs, as in Snap's first-proofs chapter: Lean works
out fullDeck.length and fullDeck.count .raider, and rfl checks the
answers. If someone changed a count, these would stop building.
A player¶
Every field has a default, so {} is a player with nothing: no cards, not
out, not protected, no arm rings. The field is called protected_ because
protected is a word Lean keeps for itself.
/-- A player's cards: their hand and their discards. -/
def Player.cards (p : Player) : List Card := p.hand ++ p.discards
/-- info: { hand := [], discards := [], out := false, protected_ := false, rings := 0 } -/
#guard_msgs in
#eval ({} : Player)
def bjorn : Player := { hand := [.seer], discards := [.raider, .raider] }
/-- info: ["seer", "raider", "raider"] -/
#guard_msgs in
#eval bjorn.cards.map Card.name
++ joins two lists.
The state of a game¶
structure State where
players : List Player
deck : List Card
/-- The face-down set-aside card. -/
hidden : Option Card
/-- Two-player games set three more aside, face up. -/
faceUp : List Card
turn : Nat
round : Nat
ringsToWin : Nat
rng : Rng
phase : Phase := .playing
/-- What each player knows about other players' hands. -/
known : List Known := []
/-- Players who were just shown a card and have yet to nod. No one plays
until they all have. -/
awaiting : List Nat := []
deriving DecidableEq, Repr, Inhabited
Most of it you can read from the rules: the players, the deck, the hidden
card (an Option, because it may have been drawn), the face-up cards, whose
turn it is, the round and the ring target. rng is the random generator
that shuffles each new round. The last two fields, known and awaiting,
are about what players have been shown. They are the subject of a later
chapter.
Players are a list, and a player is found by their position in it, their seat:
getD i default gives the element at position i, or the default value if
there isn't one. So asking for a seat that doesn't exist gives an empty
player rather than an error. That keeps the rules simple, and step refuses
moves by seats that don't exist before any of this matters.
Two more readings of the state: the seats still in the round, and the seats a card may target.
def active (s : State) : List Nat := (List.range s.players.length).filter fun i => !(s.player i).out
/-- Other players a card may target. -/
def targets (s : State) (me : Nat) : List Nat :=
s.active.filter fun i => i != me && !(s.player i).protected_
List.range n is [0, 1, …, n - 1]. filter keeps the elements for which
the function says true. ! is not, && is and, and != is not
equal, all on Bool. So the targets are the active seats other than me
that aren't protected.
Here is a small table to try them on:
/-- Three players. Seat 0 is to play and holds two cards; seat 2 is protected. -/
def table : State :=
{ players := [{ hand := [.raider, .seer] }, { hand := [.holmgang] }, { hand := [.trader], protected_ := true }],
deck := [.loki, .raider], hidden := some .shieldmaiden, faceUp := [],
turn := 0, round := 1, ringsToWin := 5, rng := Engine.Rng.ofSeed 0 }
/-- info: ["holmgang"] -/
#guard_msgs in
#eval (table.player 1).hand.map Card.name
/-- info: [] -/
#guard_msgs in
#eval (table.player 9).hand.map Card.name
/-- info: [0, 1, 2] -/
#guard_msgs in
#eval table.active
/-- info: [1] -/
#guard_msgs in
#eval table.targets 0
/-- info: 7 -/
#guard_msgs in
#eval table.cards.length
Every seat is still in, but seat 0 can only target seat 1: seat 2 is
protected. cards gathers every card in play, in a fixed order, which is
what the project's proof that no card is lost is about:
/-- Every card in play, in a fixed order. Conservation is stated over this. -/
def cards (s : State) : List Card :=
s.players.flatMap Player.cards ++ s.deck ++ s.hidden.toList ++ s.faceUp
s.hidden.toList turns the Option into a list of zero or one cards.
A new game¶
/-- A new game: `n` players, all rings at zero, first round dealt to seat 0. -/
def init (n seed ringsToWin : Nat) : State × List Event :=
let s : State := { players := List.replicate n {}, deck := [], hidden := none, faceUp := [],
turn := 0, round := 0, ringsToWin, rng := Rng.ofSeed seed }
s.deal 0
init makes n empty players and deals the first round. Like Snap's step,
it gives back a pair of the new state and the events it caused; .1 takes
the state.
def fresh : State := (init 2 7 7).1
/-- info: [["trader", "seer"], ["loki"]] -/
#guard_msgs in
#eval fresh.players.map fun p => p.hand.map Card.name
/-- info: 3 -/
#guard_msgs in
#eval fresh.faceUp.length
/-- info: 9 -/
#guard_msgs in
#eval fresh.deck.length
/-- info: true -/
#guard_msgs in
#eval fresh.hidden.isSome
With two players and seed 7: seat 0 has drawn to start their turn, so holds two cards. Three cards are face up, one is hidden, and the rest are the deck.
Exercises¶
- Write
Card.beats, which says whether one card has a higher value than another. - Write
Player.discardTotal, the total value of a player's discards. It breaks ties at the end of a round. (List.sumadds up a list of numbers.) - What is the total value of the whole deck? Work it out with
#eval. - Prove that the deck holds exactly one Valkyrie.
As in Snap, put your definitions inside the namespace of the type they are
about, so dot calls find them: here, namespace Games.LoL.
Solution
namespace Games.LoL
def Card.beats (c d : Card) : Bool := c.value > d.value
/-- info: true -/
#guard_msgs in
#eval Card.trader.beats .seer
def Player.discardTotal (p : Player) : Nat := (p.discards.map Card.value).sum
/-- info: 2 -/
#guard_msgs in
#eval ({ discards := [.raider, .raider] } : Player).discardTotal
/-- info: 54 -/
#guard_msgs in
#eval (fullDeck.map Card.value).sum
example : fullDeck.count .valkyrie = 1 := rfl
end Games.LoL