sourcegames/robot-chase/robot-chase.xtl
1⍝!/usr/bin/env xetal
2⍝# Robot chase, scripted (seed 1), with the rules of RobotChase.xtl.
3ʳ⁼u̲se< "RobotChase"
4
5⍝ A new game: you (P) and 12 robots (R) on 13 random squares.
6s ← ʳn̲ew 0
7ʳv̲iew s
8
9⍝ The robots are one 2 by 12 matrix (rows, columns), and so is you,
10⍝ repeated by a table; their step is the sign of the difference, for
11⍝ every robot and both coordinates at once.
12R ← ʳr̲obots s
13P ← (ʳp̲os s) 'l̲eft t̲able r̲ange ʳn
14R
15P − R
16d ← P − R
17(d > 0) − d < 0
18
19⍝ A made-up position to watch the rules: you at row 8, column 8; robots
20⍝ at (7, 6), (9, 6) and (8, 5); the other nine dead. The first two step
21⍝ onto (8, 7) together and crash, leaving a wreck; next turn the third
22⍝ steps onto (8, 6), then into the wreck, and is destroyed: you win.
23rows ← 7 9 8 c̲at 9 r̲eshape 0
24cols ← 6 6 5 c̲at 9 r̲eshape 0
25alive ← 1 1 1 c̲at 9 r̲eshape 0
26t ← 8 8 3 0 0 0 c̲at rows c̲at cols c̲at alive c̲at 256 r̲eshape 0
27ʳv̲iew t
28t ← t ʳm̲ove 5
29ʳv̲iew t
30ʳa̲live t
31t ← t ʳm̲ove 5
32ʳv̲iew t
33ʳa̲live t
34ʳs̲tatus t ⍝ 1: won
35
36⍝ Stepping west into the wreck instead loses (status 2). Read right to
37⍝ left, the parentheses make the wait happen first.
38t0 ← 8 8 3 0 0 0 c̲at rows c̲at cols c̲at alive c̲at 256 r̲eshape 0
39lost ← (t0 ʳm̲ove 5) ʳm̲ove 4
40ʳm̲essage lost
41ʳs̲tatus lost
42
43⍝ The long-range scan of the new game: robots, wrecks and you, summed in
44⍝ 4 by 4 regions (the board as a 4 by 4 by 4 by 4 array).
45ʳl̲rs s
46
47⍝ What the game guarantees, checked by a_ssert< (the condition as
48⍝ written; silent when it holds, a line on standard error, which fails
49⍝ the tests, when it does not).
50ok ← "1 = r:s_tatus t" a̲ssert< "the three robots collide: won"
51ok ← "2 = r:s_tatus lost" a̲ssert< "walking into a robot: caught"
a̲ssert< expands to
({ @ → (1 = ʳs̲tatus t) ? @◆ ⎕E̲RR "assertion failed: 1 = r:s_tatus t (the three robots collide: won) [games/robot-chase/robot-chase.xtl:50]"◆ @ } @)