programgames/blocks/play.xtl
Falling blocks: fill whole rows to clear them. Left and right (or a and d) move the piece, up (or w) turns it, down (or s) moves it down a row, the space bar drops it; h lets the computer player place it; r starts again. The pieces fall faster every ten lines. The game moves with the clock: on the page it is in a dialog, and the clock's ticks (twenty a second) and your keys come to the program as events; at the command line, lines such as "tick 0.05", "key LEFT" and "key space" on standard input. When the well is full the program ends with the score (on the page, Play or any key then starts a new game). The rules are Stack.xtl's.
ᵘs̲how : Num a => (Int, Int, Int, Int, Int, Int, Int, Int, Float, Float, a) -> Char
Show the game, then wait for the next event.
ᵘs̲how ← { s → _ ← ⎕S̲HOW ˢᵗd̲raw s 0 < ˢᵗs̲tatus s ? ᵘe̲nd s ᵘl̲oop s }
ᵘl̲oop : Num a => (Int, Int, Int, Int, Int, Int, Int, Int, Float, Float, a) -> Char
The next event: a tick moves the game on and shows it; a key acts.
ᵘl̲oop ← { s → e ← ⎕E̲VENT @ "end" m̲atch ⎕E̲KIND e ? ᵘq̲uit s "tick" m̲atch ⎕E̲KIND e ? ᵘs̲how s ˢᵗt̲ick f̲irst ⎕E̲AT e n̲ot "key" m̲atch ⎕E̲KIND e ? ᵘl̲oop s name ← f̲ormat ⎕E̲KEY e "r" m̲atch name ? ᵘs̲how ˢᵗn̲ew 0 ᵘl̲oop s ˢᵗk̲ey name }
ᵘq̲uit : Num a => (Int, Int, Int, Int, Int, Int, Int, Int, Float, Float, a) -> Char
The end of the events: the score so far.
ᵘq̲uit ← { s → _ ← 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}." "THE END" }
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 ".")
ᵘe̲nd : Num a => (Int, Int, Int, Int, Int, Int, Int, Int, Float, Float, a) -> Char
How the game ended (the program ends with it).
ᵘe̲nd ← { s → @ f̲ormat< "Game over. Score {f_irst st:s_core s}, lines {2 s_elect st:s_core s}." }
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 "."