sourcegames/blocks/Stack.xtl

1⍝│ The falling blocks' rules, a library: blocks.xtl (scripted), play.xtl 2⍝│ (the game: keys and the clock) and the web page all use these. The 3⍝│ well is a matrix (0 empty, else the color of the piece that left a 4⍝│ block there) and a piece is a small square matrix of 0s and 1s: it 5⍝│ turns by reversing its rows and transposing, it fits where every one 6⍝│ of its blocks lands on an empty cell, full rows are found by a 7⍝│ row-wise and, and cleared by keeping only the other rows. A game's 8⍝│ state is a tuple: (the well, the piece's shape, its kind, its row and 9⍝│ column, the next kind, the score, the lines, the seconds toward the 10⍝│ next step down, the seconds played, how it stands: 0 playing, 2 the 11⍝│ well full). 12 13"sv:" u̲se< "Svg" 14"t:" u̲se< "Text" 15 16⍝━━ The data 17 18⍝│ A list of numbers from blocks.toml. 19ʰn̲ums ← { k → 'ˢᵛn̲um e̲ach "blocks.toml" ⎕L̲IST k } 20names ← "blocks.toml" ⎕L̲IST "pieces" 21colors ← "blocks.toml" ⎕L̲IST "colors" 22well ← f̲loor ʰn̲ums "well" 23WIDE ← f̲irst well 24DEEP ← 2 s̲elect well 25speed ← ʰn̲ums "speed" 26PER ← f̲loor f̲irst ʰn̲ums "level" 27POINTS ← 0 c̲at f̲loor ʰn̲ums "points" 28⍝│ Piece k's shape: its rows from blocks.toml as a square of 0s and 1s. 29ʰr̲ead ← { k → 30 rows ← "blocks.toml" ⎕L̲IST d̲isclose k s̲elect names 31 n ← t̲ally rows 32 (n c̲at n) r̲eshape 0 + (ᵗj̲oin rows) = f̲irst "#" 33} 34SHAPES ← 'ʰr̲ead m̲ap r̲ange t̲ally names 35⍝│ A piece's shape as it first comes. 36ʰs̲hape ← { k → d̲isclose k s̲elect SHAPES } 37 38⍝━━ Pieces in the well 39 40⍝│ The well's cells under the blocks of shape M at row r, column c (from 41⍝│ 0): their rows and their columns. 42ʰc̲ells ← { (M, r, c) → 43 i ← (w̲here r̲avel 0 < M) − 1 44 n ← t̲ally M 45 (r + i d̲iv n, c + i m̲od n) 46} 47⍝│ Whether shape M fits at row r, column c: every block inside the well 48⍝│ and on an empty cell. 49ʰf̲its ← { (W, M, r, c) → 50 (i, j) ← ʰc̲ells (M, r, c) 51 n̲ot '∧ r̲/ (i ≥ 0) ∧ (i < DEEP) ∧ (j ≥ 0) ∧ j < WIDE ? 0 = 1 52 '∧ r̲/ 0 = (1 + (WIDE × i) + j) s̲elect r̲avel W 53} 54⍝│ The lowest row at or below r where M fits at column c. 55ʰl̲anding ← { (W, M, r, c) → 56 q ← r + (r̲ange DEEP + 1) − 1 57 ok ← '{ x → ʰf̲its (W, M, x, c) } e̲ach q 58 r + (f̲irst (w̲here n̲ot ok) c̲at 1 + t̲ally q) − 2 59} 60⍝│ The well with shape M of kind k left at row r, column c. 61ʰl̲eave ← { (W, M, k, r, c) → 62 (i, j) ← ʰc̲ells (M, r, c) 63 (s̲hape W) r̲eshape (r̲avel W) + k × 0 + (r̲ange WIDE × DEEP) m̲ember? 1 + (WIDE × i) + j 64} 65⍝ :: Num a => a -> a 66⍝│ The well with its full rows cleared, and how many: the rows kept, and 67⍝│ as many empty rows on top. 68⍝│ >> "st:" u_se< "Stack" 69⍝│ >> st:c_lear 3 2 r_eshape 1 1 0 1 2 2 70⍝│ 0 0 71⍝│ 0 0 72⍝│ 0 1 73ˡc̲lear ← { W → 74 full ← '∧ r̲/₂ 0 < W 75 n ← '+ r̲/ 0 + full 76 ((n c̲at 2 s̲elect s̲hape W) r̲eshape 0) c̲at (n̲ot full) r̲eplicate W 77} 78⍝│ How many rows of W are full. 79ʰf̲ull ← { W → '+ r̲/ 0 + '∧ r̲/₂ 0 < W } 80⍝│ The level, from the lines; the seconds between steps down there. 81ʰl̲evel ← { ln → 1 + ln d̲iv PER } 82ʰp̲ause ← { ln → (3 s̲elect speed) m̲ax (f̲irst speed) − (2 s̲elect speed) × f̲loat (ʰl̲evel ln) − 1 } 83⍝│ 0 when the next piece fits, 2 when it does not (the game over). 84ʰo̲ver ← { ok → ok ? 0◆ 2 } 85⍝│ The next piece into the well at the top, in the middle, the one after 86⍝│ drawn at random; the game over if it does not fit. 87ʰn̲ext ← { (W, nx, sc, ln, t) → 88 M ← ʰs̲hape nx 89 c ← (WIDE − t̲ally M) d̲iv 2 90 (W, M, nx, 0, c, r̲oll! t̲ally names, sc, ln, 0.0, t, ʰo̲ver ʰf̲its (W, M, 0, c)) 91} 92⍝ :: (Any a, Num b, Num c, Num d, Num e, Num f) => a -> (b, Int, Int, c, Int, Int, d, e, Float, Float, f) 93⍝│ A new game: an empty well and the first piece. 94ˡn̲ew ← { z → ʰn̲ext ((DEEP c̲at WIDE) r̲eshape 0, r̲oll! t̲ally names, 0, 0, 0.0) } 95⍝│ The piece left where it is: full rows cleared and scored, the next 96⍝│ piece in. 97ʰs̲ettle ← { (W, M, k, r, c, nx, sc, ln, acc, t, st) → 98 left ← ʰl̲eave (W, M, k, r, c) 99 n ← ʰf̲ull left 100 ʰn̲ext (ˡc̲lear left, nx, sc + (ʰl̲evel ln) × (1 + n) s̲elect POINTS, ln + n, t) 101} 102⍝│ A step down, or, where it cannot go down, settled. 103ʰd̲own ← { s → 104 (W, M, k, r, c, nx, sc, ln, acc, t, st) ← s 105 ʰf̲its (W, M, r + 1, c) ? (W, M, k, r + 1, c, nx, sc, ln, acc, t, st) 106 ʰs̲ettle s 107} 108⍝ :: Num a => (Int, Int, Int, Int, Int, Int, Int, Int, Float, Float, a) -> Float -> (Int, Int, Int, Int, Int, Int, Int, Int, Float, Float, a) 109⍝│ A tick of the clock, dt seconds: the piece steps down each time its 110⍝│ level's pause has gone by. A game over stays as it is. 111ˡt̲ick ← { s dt → 112 (W, M, k, r, c, nx, sc, ln, acc, t, st) ← s 113 st > 0 ? s 114 (acc + dt) < ʰp̲ause ln ? (W, M, k, r, c, nx, sc, ln, acc + dt, t + dt, st) 115 ʰd̲own (W, M, k, r, c, nx, sc, ln, 0.0, t + dt, st) 116} 117 118⍝━━ Your keys 119 120⍝ :: a -> a 121⍝│ Shape M turned a quarter clockwise: its rows reversed, then 122⍝│ transposed. 123⍝│ >> "st:" u_se< "Stack" 124⍝│ >> st:t_urn 2 3 r_eshape 1 1 1 0 1 0 125⍝│ 0 1 126⍝│ 1 1 127⍝│ 0 1 128ˡt̲urn ← { M → o̲\ r̲ev M } 129⍝│ The piece turned, if it fits there or a column to either side. 130ʰt̲urned ← { s → 131 (W, M, k, r, c, nx, sc, ln, acc, t, st) ← s 132 N ← ˡt̲urn M 133 ʰf̲its (W, N, r, c) ? (W, N, k, r, c, nx, sc, ln, acc, t, st) 134 ʰf̲its (W, N, r, c − 1) ? (W, N, k, r, c − 1, nx, sc, ln, acc, t, st) 135 ʰf̲its (W, N, r, c + 1) ? (W, N, k, r, c + 1, nx, sc, ln, acc, t, st) 136 s 137} 138⍝│ The piece a column over (d: -1 or 1), if it fits. 139ʰs̲lid ← { s d → 140 (W, M, k, r, c, nx, sc, ln, acc, t, st) ← s 141 ʰf̲its (W, M, r, c + d) ? (W, M, k, r, c + d, nx, sc, ln, acc, t, st) 142 s 143} 144⍝│ The piece dropped to where it lands, two points a row, and settled. 145ʰd̲ropped ← { (W, M, k, r, c, nx, sc, ln, acc, t, st) → 146 q ← ʰl̲anding (W, M, r, c) 147 ʰs̲ettle (W, M, k, q, c, nx, sc + 2 × q − r, ln, acc, t, st) 148} 149⍝━━ The computer player 150 151⍝│ The piece turned q times and moved dx columns (each as far as it 152⍝│ fits), then dropped. 153ʰt̲urns ← { s q → q = 0 ? s◆ (ʰt̲urned s) ʰt̲urns q − 1 } 154ʰs̲lides ← { s d → d = 0 ? s◆ (s ʰs̲lid (d > 0) − d < 0) ʰs̲lides d − (d > 0) − d < 0 } 155ʰa̲fter ← { s (q, dx) → ʰd̲ropped (s ʰt̲urns q) ʰs̲lides dx } 156⍝│ What a well is worth to the computer player: the lines a move cleared 157⍝│ count for it; the columns' heights, the holes (empty cells under a 158⍝│ block) and the bumps between neighboring columns count against it. 159ʰw̲orth ← { (W, lines) → 160 F ← 0 < W 161 top ← '∨ s̲\ F 162 h ← '+ r̲/ 0 + top 163 holes ← '+ r̲/ 0 + r̲avel top ∧ n̲ot F 164 bump ← '+ r̲/ a̲bs (1 d̲rop h) − (WIDE − 1) t̲ake h 165 (0.76 × f̲loat lines) − (0.51 × f̲loat '+ r̲/ h) + (0.36 × f̲loat holes) + 0.18 × f̲loat bump 166} 167⍝│ A move's worth: the well it leaves (far below any other if it ends 168⍝│ the game). 169ʰv̲alue ← { s (q, dx) → 170 t ← s ʰa̲fter (q, dx) 171 (W, M, k, r, c, nx, sc, ln, acc, u, st) ← t 172 st > 0 ? -1000.0 173 ʰw̲orth (W, ln − 2 s̲elect ˡs̲core s) 174} 175⍝ :: (Any a, Num b) => (Int, Int, Int, Int, Int, Int, Int, Int, Float, a, b) -> (Int, Int) 176⍝│ The computer player's move for the piece: how many quarter turns and 177⍝│ how many columns over (negative to the left), the best of every turn 178⍝│ and every column tried, each worked out to where the piece lands. 179ˡa̲i ← { s → 180 i ← (r̲ange 4 × 1 + WIDE) − 1 181 q ← i d̲iv 1 + WIDE 182 dx ← (i m̲od 1 + WIDE) − WIDE d̲iv 2 183 w ← '{ n → s ʰv̲alue (n s̲elect q, n s̲elect dx) } e̲ach 1 + i 184 b ← f̲irst g̲rade n̲eg w 185 (b s̲elect q, b s̲elect dx) 186} 187⍝ :: (Any a, Num b) => (Int, Int, Int, Int, Int, Int, Int, Int, Float, a, b) -> Char -> (Int, Int, Int, Int, Int, Int, Int, Int, Float, a, b) 188⍝│ A key: left and right (or a and d) move the piece, up (or w) turns 189⍝│ it, down (or s) moves it a row down, the space bar drops it, h lets 190⍝│ the computer player place it; other keys, and keys after the game is 191⍝│ over, do nothing. 192ˡk̲ey ← { s name → 193 (W, M, k, r, c, nx, sc, ln, acc, t, st) ← s 194 key ← ("LEFT" "RIGHT" "UP" "DOWN" "a" "d" "w" "s" " " "h") i̲ndexOf e̲nclose name 195 (key > 10) ∨ st > 0 ? s 196 key = 10 ? s ʰa̲fter ˡa̲i s 197 key = 9 ? ʰd̲ropped s 198 q ← 1 + (key − 1) m̲od 4 199 q < 3 ? s ʰs̲lid (2 × q) − 3 200 q = 3 ? ʰt̲urned s 201 ʰd̲own s 202} 203 204⍝━━ What it shows 205 206⍝ :: (Any a, Any b, Any c, Any d, Any e, Any f, Any g, Any h, Any i, Any j, Any k) => (a, b, c, d, e, f, g, h, i, j, k) -> k 207⍝│ 0 playing, 2 the well full. 208ˡs̲tatus ← { (W, M, k, r, c, nx, sc, ln, acc, t, st) → st } 209⍝ :: (Any a, Num b) => (Int, Int, Int, Int, Int, Int, Int, Int, Float, a, b) -> Int 210⍝│ The score, the lines, the level. 211ˡs̲core ← { (W, M, k, r, c, nx, sc, ln, acc, t, st) → sc c̲at ln c̲at ʰl̲evel ln } 212⍝ :: (Num a, Truthy a, Num b, Any c, Any d, Any e, Any f, Any g, Any h) => (a, b, a, Int, Int, c, d, e, f, g, h) -> a 213⍝│ The well with the piece in it (its kind where its blocks are). 214ˡw̲ell ← { (W, M, k, r, c, nx, sc, ln, acc, t, st) → ʰl̲eave (W, M, k, r, c) } 215⍝ :: (Any a, Any b, Any c, Any e, Any f, Any g, Any h, Any i, Any j) => (a, b, c, d, d, e, f, g, h, i, j) -> (b, d, e) 216⍝│ The piece: its shape, then its row and column, and the next kind. 217ˡp̲iece ← { (W, M, k, r, c, nx, sc, ln, acc, t, st) → (M, r c̲at c, nx) } 218⍝│ The size of a cell, and where the well is drawn. 219CELL ← 28.0 220X0 ← 24.0 221Y0 ← 76.0 222⍝│ A block's fill: its color, or none for an outline. 223ʰf̲ill ← { (col, ghost) → ghost = 1 ? "none"◆ col } 224⍝│ A block of kind k at row i, column j of the well (x0, y0 its corner); 225⍝│ ghost: only an outline, where the piece would land. 226ʰb̲lock ← { (i, j, k, x0, y0, ghost) → 227 col ← d̲isclose k s̲elect colors 228 fill ← ʰf̲ill (col, ghost) 229 e̲nclose @ f̲ormat< "<rect x=\"{sv:w_hole x0 + CELL * f_loat j}\" y=\"{sv:w_hole y0 + CELL * f_loat i}\" width=\"{sv:w_hole CELL - 2.0}\" height=\"{sv:w_hole CELL - 2.0}\" rx=\"4\" fill=\"{fill}\" stroke=\"{col}\" stroke-width=\"2\"/>"
f̲ormat< expands to
("<rect x=\"" c̲at (f̲ormat (ˢᵛw̲hole x0 + CELL × f̲loat j)) c̲at "\" y=\"" c̲at (f̲ormat (ˢᵛw̲hole y0 + CELL × f̲loat i)) c̲at "\" width=\"" c̲at (f̲ormat (ˢᵛw̲hole CELL − 2.0)) c̲at "\" height=\"" c̲at (f̲ormat (ˢᵛw̲hole CELL − 2.0)) c̲at "\" rx=\"4\" fill=\"" c̲at (f̲ormat (fill)) c̲at "\" stroke=\"" c̲at (f̲ormat (col)) c̲at "\" stroke-width=\"2\"/>")
230} 231⍝│ Blocks of kind k at the cells (i, j), all at once. 232ʰb̲locks ← { (i, j, k, x0, y0, ghost) → '{ n → ʰb̲lock (n s̲elect i, n s̲elect j, k, x0, y0, ghost) } e̲ach r̲ange t̲ally i } 233⍝│ The blocks left in the well, each its kind's color. 234ʰs̲ettled ← { W → 235 at ← (w̲here r̲avel 0 < W) − 1 236 '{ n → ʰb̲lock (n d̲iv WIDE, n m̲od WIDE, (n + 1) s̲elect r̲avel W, X0, Y0, 0) } e̲ach at 237} 238⍝│ What the top line says. 239ʰs̲ays ← { (sc, ln, t, st) → 240 st = 2 ? @ f̲ormat< "Game over. Score {sc} | Lines {ln} | Time {f_loor t} s"
f̲ormat< expands to
("Game over. Score " c̲at (f̲ormat (sc)) c̲at " | Lines " c̲at (f̲ormat (ln)) c̲at " | Time " c̲at (f̲ormat (f̲loor t)) c̲at " s")
241 @ f̲ormat< "Score {sc} | Lines {ln} | Level {h:l_evel ln} | Time {f_loor t} s"
f̲ormat< expands to
"Score " c̲at (f̲ormat (sc)) c̲at " | Lines " c̲at (f̲ormat (ln)) c̲at " | Level " c̲at (f̲ormat (ʰl̲evel ln)) c̲at " | Time " c̲at (f̲ormat (f̲loor t)) c̲at " s"
242} 243⍝ :: (Num a, Any c, Num d, Num e) => (Int, a, Int, Int, Int, Int, b, Int, c, d, e) -> Char 244⍝│ The picture, as SVG text: the well and the blocks in it, where the 245⍝│ piece would land (an outline), the piece, the next piece beside the 246⍝│ well, and the top line. 247ˡd̲raw ← { (W, M, k, r, c, nx, sc, ln, acc, t, st) → 248 v ← 0.0 0.0 480.0 c̲at Y0 + 16.0 + CELL × f̲loat DEEP 249 box ← e̲nclose @ f̲ormat< "<rect x=\"{sv:w_hole X0 - 4.0}\" y=\"{sv:w_hole Y0 - 4.0}\" width=\"{sv:w_hole 6.0 + CELL * f_loat WIDE}\" height=\"{sv:w_hole 6.0 + CELL * f_loat DEEP}\" fill=\"#11121c\" stroke=\"#5c5f77\" stroke-width=\"3\"/>"
f̲ormat< expands to
("<rect x=\"" c̲at (f̲ormat (ˢᵛw̲hole X0 − 4.0)) c̲at "\" y=\"" c̲at (f̲ormat (ˢᵛw̲hole Y0 − 4.0)) c̲at "\" width=\"" c̲at (f̲ormat (ˢᵛw̲hole 6.0 + CELL × f̲loat WIDE)) c̲at "\" height=\"" c̲at (f̲ormat (ˢᵛw̲hole 6.0 + CELL × f̲loat DEEP)) c̲at "\" fill=\"#11121c\" stroke=\"#5c5f77\" stroke-width=\"3\"/>")
250 (gi, gj) ← ʰc̲ells (M, ʰl̲anding (W, M, r, c), c) 251 ghost ← ʰb̲locks (gi, gj, k, X0, Y0, 1) 252 (i, j) ← ʰc̲ells (M, r, c) 253 piece ← (e̲nclose "<g class=\"you\">") c̲at (ʰb̲locks (i, j, k, X0, Y0, 0)) c̲at e̲nclose "</g>" 254 N ← ʰs̲hape nx 255 (ni, nj) ← ʰc̲ells (N, 0, 0) 256 nxt ← ʰb̲locks (ni, nj, nx, X0 + 40.0 + CELL × f̲loat WIDE, Y0 + 40.0, 0) 257 label ← (X0 + 40.0 + CELL × f̲loat WIDE, Y0 + 22.0, 22.0) ˢᵛw̲ords "Next" 258 top ← (16.0, 42.0, 24.0) ˢᵛw̲ords ʰs̲ays (sc, ln, t, st) 259 v ˢᵛp̲icture (v ˢᵛr̲ect "fill=\"#1d1f2b\"") c̲at box c̲at (ʰs̲ettled W) c̲at ghost c̲at piece c̲at label c̲at nxt c̲at top 260}