sourcegames/shut-the-box/shut-the-box.xtl
1⍝!/usr/bin/env xetal
2⍝# Shut the box, scripted (seed 1), with the rules of ShutTheBox.xtl.
3ᵇ⁼u̲se< "ShutTheBox"
4
5⍝ Every set of tiles at once: the 512 subsets of 1 to 9 are the columns
6⍝ of a 9 by 512 matrix of bits; the first eight columns, and every
7⍝ subset's sum by one inner product.
8M ← ᵇs̲ubsets @
9s̲hape M
108 t̲ake₂ M
118 t̲ake ᵇs̲ums @
12
13⍝ The moves for a roll of 7 with every tile open: the subsets that fit
14⍝ and add up to 7, one per row.
15s ← ᵇn̲ew 0
16s ᵇm̲oves 7
17⍝ With 7 and 4 shut, fewer.
18t ← s ᵇs̲hut (r̲ange 9) m̲ember? 4 7
19ᵇv̲iew t
20t ᵇm̲oves 7
21
22⍝ How many moves each roll from 2 to 12 has with every tile open.
23'{ t̲ally s ᵇm̲oves ⍵ } e̲ach 2 3 4 5 6 7 8 9 10 11 12
24
25⍝# A whole game by the high-tiles player: each turn shuts the move with
26⍝# the highest tiles. Each line is the box before the roll.
27ᵘg̲ame ← { s →
28 said ← p̲rint! ᵇv̲iew s
29 r ← '+ r̲/ ᵇr̲oll s
30 0 = t̲ally s ᵇm̲oves r ? ᵇs̲core s
31 t ← s ᵇs̲hut s ᵇh̲igh r
32 0 = ᵇs̲core t ? 0
33 ᵘg̲ame t
34}
35ᵘg̲ame s ⍝ the score: 0 shuts the box
36
37⍝ What the game guarantees, checked by a_ssert< (the condition as
38⍝ written; silent when it holds, a line on standard error, which fails
39⍝ the tests, when it does not).
40ok ← "512 = 2 s_elect s_hape M" a̲ssert< "every subset of nine tiles"
41ok ← "'& r_/ 7 = (s b:m_oves 7) '+ '* i_nner r_ange 9" a̲ssert< "every move for a 7 adds up to 7"
a̲ssert< expands to
({ @ → (512 = 2 s̲elect s̲hape M) ? @◆ ⎕E̲RR "assertion failed: 512 = 2 s_elect s_hape M (every subset of nine tiles) [games/shut-the-box/shut-the-box.xtl:40]"◆ @ } @)