sourcegames/blocks/blocks.xtl
1⍝!/usr/bin/env xetal
2⍝│ Falling blocks, scripted (seed 1), with the rules of Stack.xtl: the
3⍝│ well a matrix, a piece a small square of 0s and 1s, turning by
4⍝│ reversing and transposing, full rows cleared by keeping the others.
5"st:" u̲se< "Stack"
6
7⍝ A piece turns a quarter clockwise by reversing its rows and
8⍝ transposing: the T, and the T turned once and twice.
9T ← 3 3 r̲eshape 0 1 0 1 1 1 0 0 0
10T
11ˢᵗt̲urn T
12ˢᵗt̲urn ˢᵗt̲urn T
13
14⍝ Full rows are found by a row-wise and, and cleared by keeping only the
15⍝ other rows (as many empty rows go on top): a small well of 4 rows, the
16⍝ second and fourth full.
17w ← 4 3 r̲eshape 0 0 1 2 2 2 0 3 0 4 4 4
18w
19ˢᵗc̲lear w
20
21⍝ A new game: the first piece at the top of the 10 by 20 well, its row
22⍝ and column, and the next piece's kind.
23start ← ˢᵗn̲ew 0
24ˢᵗp̲iece start
25
26⍝ Two seconds of the clock: the piece steps down every 0.8 seconds at
27⍝ level 1. Then left twice, a turn, and the space bar: it drops to where
28⍝ it lands, two points a row. The well, with the next piece at the top:
29ᵘw̲ait ← { s n → n = 0 ? s◆ (s ˢᵗt̲ick 0.05) ᵘw̲ait n − 1 }
30waited ← start ᵘw̲ait 40
31ᵘp̲ress ← { s k → s ˢᵗk̲ey k }
32dropped ← (((waited ᵘp̲ress "LEFT") ᵘp̲ress "LEFT") ᵘp̲ress "UP") ᵘp̲ress " "
33ˢᵗw̲ell dropped
34
35⍝ The computer player: for the piece, every turn and every column tried,
36⍝ each worked out to where it lands, and the well it leaves weighed
37⍝ (lines for it; heights, holes and bumps against). Its move (quarter
38⍝ turns, columns over), then thirty pieces placed by it: the score,
39⍝ lines and level.
40ˢᵗa̲i dropped
41ᵘa̲uto ← { s n → (n = 0) ∨ 0 < ˢᵗs̲tatus s ? s◆ (s ᵘp̲ress "h") ᵘa̲uto n − 1 }
42later ← dropped ᵘa̲uto 30
43ˢᵗs̲core later
44_ ← ⎕S̲HOW ˢᵗd̲raw later
45
46⍝ What the game guarantees, checked by a_ssert< (silent when it holds).
47_ ← "'& r_/_12 (st:t_urn^4 T) = T" a̲ssert< "four quarter turns are no turn"
48_ ← "0 = '+ r_/ '& r_/_2 0 < st:c_lear w" a̲ssert< "no full row is left after clearing"
49_ ← "0 = st:s_tatus later" a̲ssert< "still playing"
a̲ssert< expands to
({ @ → ('∧ r̲/₁₂ (ˢᵗt̲urn⁴ T) = T) ? @◆ ⎕E̲RR "assertion failed: '& r_/_12 (st:t_urn^4 T) = T (four quarter turns are no turn) [games/blocks/blocks.xtl:47]"◆ @ } @)
a̲ssert< expands to
({ @ → (0 = '+ r̲/ '∧ r̲/₂ 0 < ˢᵗc̲lear w) ? @◆ ⎕E̲RR "assertion failed: 0 = '+ r_/ '& r_/_2 0 < st:c_lear w (no full row is left after clearing) [games/blocks/blocks.xtl:48]"◆ @ } @)