sourcegames/blocks/play.xtl
1⍝!/usr/bin/env xetal
2⍝│ Falling blocks: fill whole rows to clear them. Left and right (or a
3⍝│ and d) move the piece, up (or w) turns it, down (or s) moves it down
4⍝│ a row, the space bar drops it; h lets the computer player place it;
5⍝│ r starts again. The pieces fall faster every ten lines. The game moves
6⍝│ with the clock: on the page it is in a dialog, and the clock's ticks
7⍝│ (twenty a second) and your keys come to the program as events; at the
8⍝│ command line, lines such as "tick 0.05", "key LEFT" and "key space" on
9⍝│ standard input. When the well is full the program ends with the score
10⍝│ (on the page, Play or any key then starts a new game). The rules are
11⍝│ Stack.xtl's.
12"st:" u̲se< "Stack"
13
14⍝│ Show the game, then wait for the next event.
15ᵘs̲how ← { s →
16 _ ← ⎕S̲HOW ˢᵗd̲raw s
17 0 < ˢᵗs̲tatus s ? ᵘe̲nd s
18 ᵘl̲oop s
19}
20⍝│ The next event: a tick moves the game on and shows it; a key acts.
21ᵘl̲oop ← { s →
22 e ← ⎕E̲VENT @
23 "end" m̲atch ⎕E̲KIND e ? ᵘq̲uit s
24 "tick" m̲atch ⎕E̲KIND e ? ᵘs̲how s ˢᵗt̲ick f̲irst ⎕E̲AT e
25 n̲ot "key" m̲atch ⎕E̲KIND e ? ᵘl̲oop s
26 name ← f̲ormat ⎕E̲KEY e
27 "r" m̲atch name ? ᵘs̲how ˢᵗn̲ew 0
28 ᵘl̲oop s ˢᵗk̲ey name
29}
30⍝│ The end of the events: the score so far.
31ᵘq̲uit ← { s →
32 _ ← p̲rint! @ f̲ormat< "Score {f_irst st:s_core s}, lines {2 s_elect st:s_core s}, level {3 s_elect st:s_core s}."
33 "THE END"
34}
35⍝│ How the game ended (the program ends with it).
36ᵘe̲nd ← { s → @ f̲ormat< "Game over. Score {f_irst st:s_core s}, lines {2 s_elect st:s_core s}." }
37ᵘs̲how ˢᵗn̲ew 0
f̲ormat< expands to
("Score " c̲at (f̲ormat (f̲irst ˢᵗs̲core s)) c̲at ", lines " c̲at (f̲ormat (2 s̲elect ˢᵗs̲core s)) c̲at ", level " c̲at (f̲ormat (3 s̲elect ˢᵗs̲core s)) c̲at ".")
f̲ormat< expands to
"Game over. Score " c̲at (f̲ormat (f̲irst ˢᵗs̲core s)) c̲at ", lines " c̲at (f̲ormat (2 s̲elect ˢᵗs̲core s)) c̲at "."