programgames/horse-race/play.xtl

source · imports hr: games/horse-race/HorseRace.xtl; p: lib/Play.xtl

ᵘr̲ace : Int -> Int

function · line 10

The race, a round at a time, the track printed after each round.

ᵘr̲ace ← { p →
  q ← p ʰʳm̲ove ʰʳr̲oll p
  shown ← p̲rint! ʰʳv̲iew q
  gap ← p̲rint! ""
  1 = ʰʳs̲tatus q ? q◆ ᵘr̲ace q
}

ᵘa̲sk : a -> Int

function · line 18

Your pick: 1 to 5, 0 to stop, or it asks again.

ᵘa̲sk ← { c →
  k ← (@ f̲ormat< "you have {c} coins; your horse (1 to 5, q to stop):") ᵖl̲etter "12345q"
  k ≤ 5 ? k◆ k = 6 ? 0◆ ᵘa̲sk c
}
f̲ormat< expands to
("you have " c̲at (f̲ormat (c)) c̲at " coins; your horse (1 to 5, q to stop):")
Used in: ᵘa̲sk, ᵘb̲et

ᵘb̲et : Int -> Char

function · line 24

One race on your pick, then the next, until you stop or run out.

ᵘb̲et ← { c →
  k ← ᵘa̲sk c
  k = 0 ? @ f̲ormat< "you leave with {c}"
  shown ← p̲rint! @ f̲ormat< "THE RACE IS ON! you are on {k s_elect hr:n_ames @}"
  w ← ʰʳw̲inners ᵘr̲ace ʰʳn̲ew 5
  won ← f̲irst k m̲ember? w
  said ← p̲rint! @ f̲ormat< "WINNER: {r_avel w s_elect hr:n_ames @}"
  c ← c + (4 × won) − 1
  told ← p̲rint! d̲isclose (1 + won) s̲elect "you lose a coin" "you win 4 coins"
  c = 0 ? "out of coins"◆ ᵘb̲et c
}
f̲ormat< expands to
("you leave with " c̲at (f̲ormat (c)))
f̲ormat< expands to
("THE RACE IS ON! you are on " c̲at (f̲ormat (k s̲elect ʰʳn̲ames @)))
f̲ormat< expands to
("WINNER: " c̲at (f̲ormat (r̲avel w s̲elect ʰʳn̲ames @)))