librarygames/blocks/Stack.xtl
The falling blocks' rules, a library: blocks.xtl (scripted), play.xtl (the game: keys and the clock) and the web page all use these. The well is a matrix (0 empty, else the color of the piece that left a block there) and a piece is a small square matrix of 0s and 1s: it turns by reversing its rows and transposing, it fits where every one of its blocks lands on an empty cell, full rows are found by a row-wise and, and cleared by keeping only the other rows. A game's state is a tuple: (the well, the piece's shape, its kind, its row and column, the next kind, the score, the lines, the seconds toward the next step down, the seconds played, how it stands: 0 playing, 2 the well full).
The data
ʰn̲ums : Char -> Float
A list of numbers from blocks.toml.
ʰn̲ums ← { k → 'ˢᵛn̲um e̲ach "blocks.toml" ⎕L̲IST k }
ʰr̲ead : (Num a, Truthy a) => Int -> a
Piece k's shape: its rows from blocks.toml as a square of 0s and 1s.
ʰr̲ead ← { k → rows ← "blocks.toml" ⎕L̲IST d̲isclose k s̲elect names n ← t̲ally rows (n c̲at n) r̲eshape 0 + (ᵗj̲oin rows) = f̲irst "#" }
ʰs̲hape : Int -> Int
A piece's shape as it first comes.
ʰs̲hape ← { k → d̲isclose k s̲elect SHAPES }
Pieces in the well
ʰc̲ells : Num a => (a, Int, Int) -> (Int, Int)
The well's cells under the blocks of shape M at row r, column c (from 0): their rows and their columns.
ʰc̲ells ← { (M, r, c) → i ← (w̲here r̲avel 0 < M) − 1 n ← t̲ally M (r + i d̲iv n, c + i m̲od n) }
ʰf̲its : (Num a, Num b, Truthy c) => (a, b, Int, Int) -> c
Whether shape M fits at row r, column c: every block inside the well and on an empty cell.
ʰf̲its ← { (W, M, r, c) → (i, j) ← ʰc̲ells (M, r, c) n̲ot '∧ r̲/ (i ≥ 0) ∧ (i < DEEP) ∧ (j ≥ 0) ∧ j < WIDE ? 0 = 1 '∧ r̲/ 0 = (1 + (WIDE × i) + j) s̲elect r̲avel W }
ʰl̲anding : (Num a, Num b) => (a, b, Int, Int) -> Int
The lowest row at or below r where M fits at column c.
ʰl̲anding ← { (W, M, r, c) → q ← r + (r̲ange DEEP + 1) − 1 ok ← '{ x → ʰf̲its (W, M, x, c) } e̲ach q r + (f̲irst (w̲here n̲ot ok) c̲at 1 + t̲ally q) − 2 }
ʰl̲eave : (Num a, Truthy a, Num b) => (a, b, a, Int, Int) -> a
The well with shape M of kind k left at row r, column c.
ʰl̲eave ← { (W, M, k, r, c) → (i, j) ← ʰc̲ells (M, r, c) (s̲hape W) r̲eshape (r̲avel W) + k × 0 + (r̲ange WIDE × DEEP) m̲ember? 1 + (WIDE × i) + j }
ˡc̲lear : Num a => a -> a
The well with its full rows cleared, and how many: the rows kept, and as many empty rows on top.
"st:" u̲se< "Stack"
ˢᵗc̲lear 3 2 r̲eshape 1 1 0 1 2 2 0 0 0 0 0 1
ˡc̲lear ← { W → full ← '∧ r̲/₂ 0 < W n ← '+ r̲/ 0 + full ((n c̲at 2 s̲elect s̲hape W) r̲eshape 0) c̲at (n̲ot full) r̲eplicate W }
ʰf̲ull : (Num a, Num b, Truthy b) => a -> b
How many rows of W are full.
ʰf̲ull ← { W → '+ r̲/ 0 + '∧ r̲/₂ 0 < W }
ʰl̲evel : Int -> Int
The level, from the lines; the seconds between steps down there.
ʰl̲evel ← { ln → 1 + ln d̲iv PER }
ʰp̲ause : Int -> Float
ʰp̲ause ← { ln → (3 s̲elect speed) m̲ax (f̲irst speed) − (2 s̲elect speed) × f̲loat (ʰl̲evel ln) − 1 }
ʰo̲ver : (Truthy a, Num b) => a -> b
0 when the next piece fits, 2 when it does not (the game over).
ʰo̲ver ← { ok → ok ? 0◆ 2 }
ʰn̲ext : (Num a, Any b, Any c, Any d, Num e, Num f) => (a, Int, b, c, d) -> (a, Int, Int, e, Int, Int, b, c, Float, d, f)
The next piece into the well at the top, in the middle, the one after drawn at random; the game over if it does not fit.
ʰn̲ext ← { (W, nx, sc, ln, t) → M ← ʰs̲hape nx c ← (WIDE − t̲ally M) d̲iv 2 (W, M, nx, 0, c, r̲oll! t̲ally names, sc, ln, 0.0, t, ʰo̲ver ʰf̲its (W, M, 0, c)) }
ˡn̲ew : (Any a, Num b, Num c, Num d, Num e, Num f) => a -> (b, Int, Int, c, Int, Int, d, e, Float, Float, f)
A new game: an empty well and the first piece.
ˡn̲ew ← { z → ʰn̲ext ((DEEP c̲at WIDE) r̲eshape 0, r̲oll! t̲ally names, 0, 0, 0.0) }
ʰs̲ettle : (Num a, Truthy a, Num b, Any c, Any d, Any e, Num f, Num g) => (a, b, a, Int, Int, Int, Int, Int, c, d, e) -> (a, Int, Int, f, Int, Int, Int, Int, Float, d, g)
The piece left where it is: full rows cleared and scored, the next piece in.
ʰs̲ettle ← { (W, M, k, r, c, nx, sc, ln, acc, t, st) → left ← ʰl̲eave (W, M, k, r, c) n ← ʰf̲ull left ʰn̲ext (ˡc̲lear left, nx, sc + (ʰl̲evel ln) × (1 + n) s̲elect POINTS, ln + n, t) }
ʰd̲own : (Any a, Num b) => (Int, Int, Int, Int, Int, Int, Int, Int, Float, a, b) -> (Int, Int, Int, Int, Int, Int, Int, Int, Float, a, b)
A step down, or, where it cannot go down, settled.
ʰd̲own ← { s → (W, M, k, r, c, nx, sc, ln, acc, t, st) ← s ʰf̲its (W, M, r + 1, c) ? (W, M, k, r + 1, c, nx, sc, ln, acc, t, st) ʰs̲ettle s }
ˡt̲ick : 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)
A tick of the clock, dt seconds: the piece steps down each time its level's pause has gone by. A game over stays as it is.
ˡt̲ick ← { s dt → (W, M, k, r, c, nx, sc, ln, acc, t, st) ← s st > 0 ? s (acc + dt) < ʰp̲ause ln ? (W, M, k, r, c, nx, sc, ln, acc + dt, t + dt, st) ʰd̲own (W, M, k, r, c, nx, sc, ln, 0.0, t + dt, st) }
Your keys
ˡt̲urn : a -> a
Shape M turned a quarter clockwise: its rows reversed, then transposed.
"st:" u̲se< "Stack"
ˢᵗt̲urn 2 3 r̲eshape 1 1 1 0 1 0 0 1 1 1 0 1
ˡt̲urn ← { M → o̲\ r̲ev M }
ʰt̲urned : (Num a, Num b, Any c, Any d, Any e, Any f, Any g, Any h, Any i) => (a, b, c, Int, Int, d, e, f, g, h, i) -> (a, b, c, Int, Int, d, e, f, g, h, i)
The piece turned, if it fits there or a column to either side.
ʰt̲urned ← { s → (W, M, k, r, c, nx, sc, ln, acc, t, st) ← s N ← ˡt̲urn M ʰf̲its (W, N, r, c) ? (W, N, k, r, c, nx, sc, ln, acc, t, st) ʰf̲its (W, N, r, c − 1) ? (W, N, k, r, c − 1, nx, sc, ln, acc, t, st) ʰf̲its (W, N, r, c + 1) ? (W, N, k, r, c + 1, nx, sc, ln, acc, t, st) s }
ʰs̲lid : (Num a, Num b, Any c, Any d, Any e, Any f, Any g, Any h, Any i) => (a, b, c, Int, Int, d, e, f, g, h, i) -> Int -> (a, b, c, Int, Int, d, e, f, g, h, i)
The piece a column over (d: -1 or 1), if it fits.
ʰs̲lid ← { s d → (W, M, k, r, c, nx, sc, ln, acc, t, st) ← s ʰf̲its (W, M, r, c + d) ? (W, M, k, r, c + d, nx, sc, ln, acc, t, st) s }
ʰd̲ropped : (Num a, Truthy a, Num b, Any c, Any d, Any e, Num f, Num g) => (a, b, a, Int, Int, Int, Int, Int, c, d, e) -> (a, Int, Int, f, Int, Int, Int, Int, Float, d, g)
The piece dropped to where it lands, two points a row, and settled.
ʰd̲ropped ← { (W, M, k, r, c, nx, sc, ln, acc, t, st) → q ← ʰl̲anding (W, M, r, c) ʰs̲ettle (W, M, k, q, c, nx, sc + 2 × q − r, ln, acc, t, st) }
The computer player
ʰt̲urns : (Num a, Num b, Any c, Any d, Any e, Any f, Any g, Any h, Any i, Num j) => (a, b, c, Int, Int, d, e, f, g, h, i) -> j -> (a, b, c, Int, Int, d, e, f, g, h, i)
The piece turned q times and moved dx columns (each as far as it fits), then dropped.
ʰt̲urns ← { s q → q = 0 ? s◆ (ʰt̲urned s) ʰt̲urns q − 1 }
ʰs̲lides : (Num a, Num b, Any c, Any d, Any e, Any f, Any g, Any h, Any i, Num j, Truthy j) => (a, b, c, Int, Int, d, e, f, g, h, i) -> j -> (a, b, c, Int, Int, d, e, f, g, h, i)
ʰs̲lides ← { s d → d = 0 ? s◆ (s ʰs̲lid (d > 0) − d < 0) ʰs̲lides d − (d > 0) − d < 0 }
ʰa̲fter : (Num a, Truthy a, Num b, Any c, Any d, Any e, Num f, Num g, Truthy g, Num h, Num i) => (a, b, a, Int, Int, Int, Int, Int, c, d, e) -> (f, g) -> (a, Int, Int, h, Int, Int, Int, Int, Float, d, i)
ʰa̲fter ← { s (q, dx) → ʰd̲ropped (s ʰt̲urns q) ʰs̲lides dx }
ʰw̲orth : (Num a, Num b) => (a, b) -> Float
What a well is worth to the computer player: the lines a move cleared count for it; the columns' heights, the holes (empty cells under a block) and the bumps between neighboring columns count against it.
ʰw̲orth ← { (W, lines) → F ← 0 < W top ← '∨ s̲\ F h ← '+ r̲/ 0 + top holes ← '+ r̲/ 0 + r̲avel top ∧ n̲ot F bump ← '+ r̲/ a̲bs (1 d̲rop h) − (WIDE − 1) t̲ake h (0.76 × f̲loat lines) − (0.51 × f̲loat '+ r̲/ h) + (0.36 × f̲loat holes) + 0.18 × f̲loat bump }
ʰv̲alue : (Any a, Num b) => (Int, Int, Int, Int, Int, Int, Int, Int, Float, a, b) -> (Int, Int) -> Float
A move's worth: the well it leaves (far below any other if it ends the game).
ʰv̲alue ← { s (q, dx) → t ← s ʰa̲fter (q, dx) (W, M, k, r, c, nx, sc, ln, acc, u, st) ← t st > 0 ? -1000.0 ʰw̲orth (W, ln − 2 s̲elect ˡs̲core s) }
ˡa̲i : (Any a, Num b) => (Int, Int, Int, Int, Int, Int, Int, Int, Float, a, b) -> (Int, Int)
The computer player's move for the piece: how many quarter turns and how many columns over (negative to the left), the best of every turn and every column tried, each worked out to where the piece lands.
ˡa̲i ← { s → i ← (r̲ange 4 × 1 + WIDE) − 1 q ← i d̲iv 1 + WIDE dx ← (i m̲od 1 + WIDE) − WIDE d̲iv 2 w ← '{ n → s ʰv̲alue (n s̲elect q, n s̲elect dx) } e̲ach 1 + i b ← f̲irst g̲rade n̲eg w (b s̲elect q, b s̲elect dx) }
ˡk̲ey : (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)
A key: left and right (or a and d) move the piece, up (or w) turns it, down (or s) moves it a row down, the space bar drops it, h lets the computer player place it; other keys, and keys after the game is over, do nothing.
ˡk̲ey ← { s name → (W, M, k, r, c, nx, sc, ln, acc, t, st) ← s key ← ("LEFT" "RIGHT" "UP" "DOWN" "a" "d" "w" "s" " " "h") i̲ndexOf e̲nclose name (key > 10) ∨ st > 0 ? s key = 10 ? s ʰa̲fter ˡa̲i s key = 9 ? ʰd̲ropped s q ← 1 + (key − 1) m̲od 4 q < 3 ? s ʰs̲lid (2 × q) − 3 q = 3 ? ʰt̲urned s ʰd̲own s }
What it shows
ˡs̲tatus : (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
0 playing, 2 the well full.
ˡs̲tatus ← { (W, M, k, r, c, nx, sc, ln, acc, t, st) → st }
ˡs̲core : (Any a, Num b) => (Int, Int, Int, Int, Int, Int, Int, Int, Float, a, b) -> Int
The score, the lines, the level.
ˡs̲core ← { (W, M, k, r, c, nx, sc, ln, acc, t, st) → sc c̲at ln c̲at ʰl̲evel ln }
ˡw̲ell : (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
The well with the piece in it (its kind where its blocks are).
ˡw̲ell ← { (W, M, k, r, c, nx, sc, ln, acc, t, st) → ʰl̲eave (W, M, k, r, c) }
ˡp̲iece : (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)
The piece: its shape, then its row and column, and the next kind.
ˡp̲iece ← { (W, M, k, r, c, nx, sc, ln, acc, t, st) → (M, r c̲at c, nx) }
ʰf̲ill : Num a => (Char, a) -> Char
A block's fill: its color, or none for an outline.
ʰf̲ill ← { (col, ghost) → ghost = 1 ? "none"◆ col }
ʰb̲lock : (Num a, Num b, Num c) => (a, b, Int, Float, Float, c) -> Box Char
A block of kind k at row i, column j of the well (x0, y0 its corner); ghost: only an outline, where the piece would land.
ʰb̲lock ← { (i, j, k, x0, y0, ghost) → col ← d̲isclose k s̲elect colors fill ← ʰf̲ill (col, ghost) 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\"/>")
ʰb̲locks : (Num a, Num b, Num c) => (a, b, Int, Float, Float, c) -> Box Char
Blocks of kind k at the cells (i, j), all at once.
ʰ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 }
ʰs̲ettled : Int -> Box Char
The blocks left in the well, each its kind's color.
ʰs̲ettled ← { W → at ← (w̲here r̲avel 0 < W) − 1 '{ n → ʰb̲lock (n d̲iv WIDE, n m̲od WIDE, (n + 1) s̲elect r̲avel W, X0, Y0, 0) } e̲ach at }
ʰs̲ays : (Num b, Num c) => (a, Int, b, c) -> Char
What the top line says.
ʰs̲ays ← { (sc, ln, t, st) → st = 2 ? @ f̲ormat< "Game over. Score {sc} | Lines {ln} | Time {f_loor t} s" @ f̲ormat< "Score {sc} | Lines {ln} | Level {h:l_evel 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")
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"
ˡd̲raw : (Num a, Any c, Num d, Num e) => (Int, a, Int, Int, Int, Int, b, Int, c, d, e) -> Char
The picture, as SVG text: the well and the blocks in it, where the piece would land (an outline), the piece, the next piece beside the well, and the top line.
ˡd̲raw ← { (W, M, k, r, c, nx, sc, ln, acc, t, st) → v ← 0.0 0.0 480.0 c̲at Y0 + 16.0 + CELL × f̲loat DEEP 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\"/>" (gi, gj) ← ʰc̲ells (M, ʰl̲anding (W, M, r, c), c) ghost ← ʰb̲locks (gi, gj, k, X0, Y0, 1) (i, j) ← ʰc̲ells (M, r, c) piece ← (e̲nclose "<g class=\"you\">") c̲at (ʰb̲locks (i, j, k, X0, Y0, 0)) c̲at e̲nclose "</g>" N ← ʰs̲hape nx (ni, nj) ← ʰc̲ells (N, 0, 0) nxt ← ʰb̲locks (ni, nj, nx, X0 + 40.0 + CELL × f̲loat WIDE, Y0 + 40.0, 0) label ← (X0 + 40.0 + CELL × f̲loat WIDE, Y0 + 22.0, 22.0) ˢᵛw̲ords "Next" top ← (16.0, 42.0, 24.0) ˢᵛw̲ords ʰs̲ays (sc, ln, t, st) 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 }
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\"/>")