sourcegames/horse-race/play.xtl
1⍝!/usr/bin/env xetal
2⍝# Horse race at the terminal: you start with 10 coins. Before each race
3⍝# pick a horse, 1 to 5, and bet a coin on it; if it wins (a share of a
4⍝# tie counts) you get 4 back. Type q to stop. The rules are those of
5⍝# HorseRace.xtl.
6ʰʳ⁼u̲se< "HorseRace"
7ᵖ⁼u̲se< "Play"
8
9⍝# The race, a round at a time, the track printed after each round.
10ᵘr̲ace ← { p →
11 q ← p ʰʳm̲ove ʰʳr̲oll p
12 shown ← p̲rint! ʰʳv̲iew q
13 gap ← p̲rint! ""
14 1 = ʰʳs̲tatus q ? q◆ ᵘr̲ace q
15}
16
17⍝# Your pick: 1 to 5, 0 to stop, or it asks again.
18ᵘa̲sk ← { c →
19 k ← (@ f̲ormat< "you have {c} coins; your horse (1 to 5, q to stop):") ᵖl̲etter "12345q"
20 k ≤ 5 ? k◆ k = 6 ? 0◆ ᵘa̲sk c
21}
22
23⍝# One race on your pick, then the next, until you stop or run out.
24ᵘb̲et ← { c →
25 k ← ᵘa̲sk c
26 k = 0 ? @ f̲ormat< "you leave with {c}"
27 shown ← p̲rint! @ f̲ormat< "THE RACE IS ON! you are on {k s_elect hr:n_ames @}"
28 w ← ʰʳw̲inners ᵘr̲ace ʰʳn̲ew 5
29 won ← f̲irst k m̲ember? w
30 said ← p̲rint! @ f̲ormat< "WINNER: {r_avel w s_elect hr:n_ames @}"
31 c ← c + (4 × won) − 1
32 told ← p̲rint! d̲isclose (1 + won) s̲elect "you lose a coin" "you win 4 coins"
33 c = 0 ? "out of coins"◆ ᵘb̲et c
34}
35ᵘb̲et 10