sourcegames/guess/guess.xtl

1⍝!/usr/bin/env xetal 2⍝# Guess the number, scripted (seed 1), with the rules of NumberGuess.xtl. 3ᵍ⁼u̲se< "NumberGuess" 4 5⍝ A secret, and three guesses answered at once. 6t ← ᵍn̲ew ᵍtop 7t ᵍm̲ove 10 50 90 8ᵍr̲eply t ᵍm̲ove 50 9 10⍝# The halving player on this secret: guess the middle of what is still 11⍝# possible, narrow, repeat. Each guess is printed with its answer, and 12⍝# the result is how many guesses it took. 13ᵘp̲lay ← { t b → 14 k ← f̲irst r̲avel ᵍg̲uess b 15 r ← t ᵍm̲ove k 16 shown ← p̲rint! @ f̲ormat< "{k} {g:r_eply r}"
f̲ormat< expands to
((f̲ormat (k)) c̲at "  " c̲at (f̲ormat (ᵍr̲eply r)))
17 r = 0 ? 1◆ 1 + t ᵘp̲lay b ᵍn̲arrow r 18} 19start ← 2 1 r̲eshape 1 c̲at ᵍtop 20t ᵘp̲lay start 21 22⍝ What three guesses say: every candidate checked against every guess 23⍝ at once; the secrets still possible. 24w̲here t ᵍp̲ossible 50 75 62 25 26⍝ Every game at once: all 100 secrets, a column each, played by the 27⍝ same player in lockstep. The state is three rows: the ranges' low and 28⍝ high ends, and how many wrong guesses each game has had so far. 29s ← r̲ange ᵍtop 30ᵘr̲ound ← { st → 31 b ← 2 t̲ake st 32 r ← s ᵍm̲ove ᵍg̲uess b 33 (b ᵍn̲arrow r) c̲at (3 s̲elect st) + r ≠ 0 34} 35st0 ← (3 c̲at ᵍtop) r̲eshape (ᵍtop r̲eshape 1) c̲at (ᵍtop r̲eshape ᵍtop) c̲at ᵍtop r̲eshape 0 36st ← 7 'ᵘr̲ound p̲ower st0 37'+ r̲/ s = ᵍg̲uess 2 t̲ake st ⍝ games won after 7 rounds: all of them 38n ← 1 + 3 s̲elect st ⍝ the guesses each secret needed 3910 t̲ake n 40'+ r̲/₂ (r̲ange 7) '= t̲able n ⍝ games won in 1, 2, ... 7 guesses 41'm̲ax r̲/ n ⍝ never more than 7: 2^7 > 100 42 43⍝ What the game guarantees, checked by a_ssert< (the condition as 44⍝ written; silent when it holds, a line on standard error, which fails 45⍝ the tests, when it does not). 46ok ← "7 >= 'm_ax r_/ n" a̲ssert< "every secret found in at most 7 guesses (2^7 > 100)"
a̲ssert< expands to
({ @ → (7 ≥ 'm̲ax r̲/ n) ? @◆ ⎕E̲RR "assertion failed: 7 >= 'm_ax r_/ n (every secret found in at most 7 guesses (2^7 > 100)) [games/guess/guess.xtl:46]"◆ @ } @)
47ok ← "g:top = '+ r_/ s = g:g_uess 2 t_ake st" a̲ssert< "all games won after 7 rounds"
a̲ssert< expands to
({ @ → (ᵍtop = '+ r̲/ s = ᵍg̲uess 2 t̲ake st) ? @◆ ⎕E̲RR "assertion failed: g:top = '+ r_/ s = g:g_uess 2 t_ake st (all games won after 7 rounds) [games/guess/guess.xtl:47]"◆ @ } @)