librarygames/maze/Walls.xtl
The maze's rules, a library: maze.xtl (scripted), play.xtl (the game: arrow keys) and the web page all use these. A maze is a grid of blocks, wall or open, kept as one vector of 0s and 1s, row by row; a new one is carved from the game's seed with whole-array operations, and the way out is put at the open block farthest from the start. What you see is worked out the same way: the blocks ahead and to each side, every depth looked up at once, then drawn far to near. A game's state is a tuple: (the walls, where you are, which way you face (0 north, 1 east, 2 south, 3 west), the steps taken, the blocks seen, whether the mini map shows, the way out).
The data
size : Int
The maze's size in cells, and so in blocks: a block per cell, a block between neighbors (a wall or a way through), and a wall all around.
size ← f̲loor 'ˢᵛn̲um e̲ach "maze.toml" ⎕L̲IST "size"
WALLS : Box Char
The colors: the walls by depth, the floor, the ceiling.
WALLS ← "maze.toml" ⎕L̲IST "walls"
STEPS : Int
A step north, east, south, west, as a move along the vector.
STEPS ← (n̲eg B) c̲at 1 c̲at B c̲at -1
A new maze
ʰc̲arve : (Any a, Truthy b) => a -> b
The walls of a new maze, carved row by row in one go (the sidewinder way): each cell joins its east neighbor at random (the top row always, the last column never), which cuts each row into runs; each run below the top row opens north from one of its cells, picked at random. The runs are a running sum of where they end; the cell picked in each run is the one with the highest random key in it, every pair of cells compared in one table.
ʰc̲arve ← { z → n ← CW × CH k ← (r̲ange n) − 1 i ← k d̲iv CW j ← k m̲od CW east ← (j < CW − 1) ∧ (i = 0) ∨ 1 = r̲oll! n r̲eshape 2 ends ← 0 + n̲ot east run ← ('+ s̲\ ends) − ends key ← g̲rade r̲oll! n r̲eshape 1000000 best ← 'm̲ax r̲/₂ (0 + run '= t̲able run) × (n c̲at n) r̲eshape key north ← (i > 0) ∧ key = best cell ← 1 + (B × 1 + 2 × i) + 1 + 2 × j open ← cell c̲at (east r̲eplicate cell + 1) c̲at north r̲eplicate cell − B n̲ot (r̲ange NB) m̲ember? open }
ʰs̲hift : Int -> Int -> Int
The vector d moved k places (the block k further along in each place), FAR shifted in at the end.
ʰs̲hift ← { d k → k > 0 ? (k d̲rop d) c̲at k r̲eshape FAR ((n̲eg k) r̲eshape FAR) c̲at (NB + k) t̲ake d }
ʰf̲lood : (Int, Int) -> Int
Every open block's distance from the start, by a flood: each block one more than its nearest neighbor, all at once, again until nothing changes (walls stay FAR).
ʰf̲lood ← { (G, d) → e ← (FAR × 0 + G) m̲ax d m̲in 1 + (d ʰs̲hift 1) m̲in (d ʰs̲hift -1) m̲in (d ʰs̲hift B) m̲in d ʰs̲hift n̲eg B '∧ r̲/ e = d ? d ʰf̲lood (G, e) }
ˡn̲ew : (Any a, Num b, Num c, Truthy d, Num e) => a -> (Int, Int, b, c, d, e, Int)
A new game: a new maze, you in its bottom left cell facing north, the way out at the open block farthest from you.
ˡn̲ew ← { z → G ← ʰc̲arve z at ← 2 + B × BH − 2 d ← ʰf̲lood (G, FAR × 0 + (r̲ange NB) ≠ at) goal ← f̲irst g̲rade n̲eg d × 0 + d < FAR (G, at, 0, 0, ʰs̲ee (G, at, 0, 0 < NB r̲eshape 0), 0, goal) }
Looking
ʰw̲all : Truthy a => (Int, Int) -> a
Whether the blocks i are walls (beyond the maze counts as wall).
ʰw̲all ← { (G, i) → (i < 1) ∨ (i > NB) ∨ (1 m̲ax NB m̲in i) s̲elect G }
ʰl̲ook : (Int, Int) -> (Int, Int, Int)
What you see from at facing f, depths 0 to 5 at once: the blocks ahead (F), to their left (L) and to their right (R), as block numbers.
ʰl̲ook ← { (at, f) → k ← (r̲ange 6) − 1 F ← at + k × (1 + f) s̲elect STEPS L ← F + (1 + (f + 3) m̲od 4) s̲elect STEPS R ← F + (1 + (f + 1) m̲od 4) s̲elect STEPS (F, L, R) }
ʰd̲epth : (Int, Int) -> Int
The depth of the first wall ahead (5 when none is that near).
ʰd̲epth ← { (G, F) → f̲irst (w̲here 1 d̲rop ʰw̲all (G, F)) c̲at 5 }
ʰs̲ee : (Truthy a, Truthy b) => (Int, Int, Int, a) -> b
The blocks seen with what is in view from at facing f added: the blocks ahead up to the first wall, and those beside them.
ʰs̲ee ← { (G, at, f, seen) → (F, L, R) ← ʰl̲ook (at, f) D ← ʰd̲epth (G, F) i ← ((1 + D) t̲ake F) c̲at (D t̲ake L) c̲at D t̲ake R seen ∨ (r̲ange NB) m̲ember? i }
Your keys
ʰm̲oved : (Num a, Truthy b, Any c, Any d) => (Int, Int, Int, Int, a, b, c, d) -> (Int, Int, Int, a, b, c, d)
You at to (unless it is a wall, where you stay), a step more.
ʰm̲oved ← { (G, at, to, f, n, seen, mp, goal) → ʰw̲all (G, to) ? (G, at, f, n, seen, mp, goal) (G, to, f, n + 1, ʰs̲ee (G, to, f, seen), mp, goal) }
ˡk̲ey : (Num a, Truthy b, Num c, Any d) => (Int, Int, Int, a, b, c, d) -> Char -> (Int, Int, Int, a, b, c, d)
A key: up (or w) steps ahead, down (or s) steps back, left and right (or a and d) turn a quarter, m shows or hides the mini map; other keys do nothing.
ˡk̲ey ← { s name → (G, at, f, n, seen, mp, goal) ← s key ← ("UP" "DOWN" "LEFT" "RIGHT" "w" "s" "a" "d" "m") i̲ndexOf e̲nclose name key > 9 ? s key = 9 ? (G, at, f, n, seen, 1 − mp, goal) k ← 1 + (key − 1) m̲od 4 t ← (4 + f + (2 × k) − 7) m̲od 4 k > 2 ? (G, at, t, n, ʰs̲ee (G, at, t, seen), mp, goal) ʰm̲oved (G, at, at + (1 + (f + 2 × k − 1) m̲od 4) s̲elect STEPS, f, n, seen, mp, goal) }
What it shows
ˡs̲tatus : (Any a, Eq b, Any c, Any d, Any e, Any f, Num g, Truthy g) => (a, b, c, d, e, f, b) -> g
1 once you stand on the way out, else 0.
ˡs̲tatus ← { (G, at, f, n, seen, mp, goal) → 0 + at = goal }
ˡy̲ou : (Any a, Any c, Any d) => (a, b, b, b, c, d, b) -> b
The steps taken, which way you face, your block, the way out's block.
ˡy̲ou ← { (G, at, f, n, seen, mp, goal) → n c̲at f c̲at at c̲at goal }
ˡg̲rid : (Num a, Any b, Any c, Any d, Any e, Any f, Any g) => (a, b, c, d, e, f, g) -> a
The maze as a matrix of blocks, 1 a wall, 0 open.
ˡg̲rid ← { (G, at, f, n, seen, mp, goal) → (BH c̲at B) r̲eshape 0 + G }
ˡd̲istances : (Any a, Any b, Any c, Any d, Any e) => (Int, Int, a, b, c, d, e) -> Int
Every open block's distance in steps from where you are (the flood), walls -1, as a matrix.
ˡd̲istances ← { (G, at, f, n, seen, mp, goal) → d ← ʰf̲lood (G, FAR × 0 + (r̲ange NB) ≠ at) (BH c̲at B) r̲eshape d − (1 + FAR) × 0 + G }
ˡs̲een : (Any a, Any b, Any c, Any d, Num e, Any f, Any g) => (a, b, c, d, e, f, g) -> e
The blocks seen so far, as a matrix of 0s and 1s.
ˡs̲een ← { (G, at, f, n, seen, mp, goal) → (BH c̲at B) r̲eshape 0 + seen }
H : Float
The view's planes: a block at depth k spans from plane k to plane k+1; each plane's half width and half height, nearest first.
H ← 400.0 230.0 130.0 74.0 42.0 24.0 14.0
ʰc̲olor : Int -> Char
A wall's color at depth k.
ʰc̲olor ← { k → d̲isclose (1 + k m̲in 4) s̲elect WALLS }
ʰs̲ide : (Truthy a, Truthy b) => (Int, Float, a, b) -> Char
A side of depth k (s: -1 left, 1 right): a wall beside you there (a slanted panel), or, where that side is open, the face of the wall beyond the opening.
ʰs̲ide ← { (k, s, wall, face) → a ← (k + 1) s̲elect H b ← (k + 2) s̲elect H va ← (k + 1) s̲elect V vb ← (k + 2) s̲elect V wall ? @ f̲ormat< "<polygon points=\"{sv:w_hole CX + s * a},{sv:w_hole CY - va} {sv:w_hole CX + s * b},{sv:w_hole CY - vb} {sv:w_hole CX + s * b},{sv:w_hole CY + vb} {sv:w_hole CX + s * a},{sv:w_hole CY + va}\" fill=\"{h:c_olor k}\" stroke=\"#000000\" stroke-width=\"2\"/>" face ? @ f̲ormat< "<rect x=\"{sv:w_hole (CX + s * 2.0 * b) - b}\" y=\"{sv:w_hole CY - vb}\" width=\"{sv:w_hole 2.0 * b}\" height=\"{sv:w_hole 2.0 * vb}\" fill=\"{h:c_olor k + 1}\" stroke=\"#000000\" stroke-width=\"2\"/>" "" }
f̲ormat< expands to
("<polygon points=\"" c̲at (f̲ormat (ˢᵛw̲hole CX + s × a)) c̲at "," c̲at (f̲ormat (ˢᵛw̲hole CY − va)) c̲at " " c̲at (f̲ormat (ˢᵛw̲hole CX + s × b)) c̲at "," c̲at (f̲ormat (ˢᵛw̲hole CY − vb)) c̲at " " c̲at (f̲ormat (ˢᵛw̲hole CX + s × b)) c̲at "," c̲at (f̲ormat (ˢᵛw̲hole CY + vb)) c̲at " " c̲at (f̲ormat (ˢᵛw̲hole CX + s × a)) c̲at "," c̲at (f̲ormat (ˢᵛw̲hole CY + va)) c̲at "\" fill=\"" c̲at (f̲ormat (ʰc̲olor k)) c̲at "\" stroke=\"#000000\" stroke-width=\"2\"/>")
f̲ormat< expands to
("<rect x=\"" c̲at (f̲ormat (ˢᵛw̲hole (CX + s × 2.0 × b) − b)) c̲at "\" y=\"" c̲at (f̲ormat (ˢᵛw̲hole CY − vb)) c̲at "\" width=\"" c̲at (f̲ormat (ˢᵛw̲hole 2.0 × b)) c̲at "\" height=\"" c̲at (f̲ormat (ˢᵛw̲hole 2.0 × vb)) c̲at "\" fill=\"" c̲at (f̲ormat (ʰc̲olor k + 1)) c̲at "\" stroke=\"#000000\" stroke-width=\"2\"/>")
ʰf̲ront : Int -> Char
The wall facing you at depth k (on plane k; at depth 5, darkness), and the way out where it stands ahead (g: its depth plus 1, or nothing), on the near plane of its block.
ʰf̲ront ← { k → k ≥ 5 ? @ f̲ormat< "<rect x=\"{sv:w_hole CX - 6 s_elect H}\" y=\"{sv:w_hole CY - 6 s_elect V}\" width=\"{sv:w_hole 2.0 * 6 s_elect H}\" height=\"{sv:w_hole 2.0 * 6 s_elect V}\" fill=\"#000000\"/>" a ← (k + 1) s̲elect H v ← (k + 1) s̲elect V @ f̲ormat< "<rect x=\"{sv:w_hole CX - a}\" y=\"{sv:w_hole CY - v}\" width=\"{sv:w_hole 2.0 * a}\" height=\"{sv:w_hole 2.0 * v}\" fill=\"{h:c_olor k}\" stroke=\"#000000\" stroke-width=\"2\"/>" }
f̲ormat< expands to
("<rect x=\"" c̲at (f̲ormat (ˢᵛw̲hole CX − 6 s̲elect H)) c̲at "\" y=\"" c̲at (f̲ormat (ˢᵛw̲hole CY − 6 s̲elect V)) c̲at "\" width=\"" c̲at (f̲ormat (ˢᵛw̲hole 2.0 × 6 s̲elect H)) c̲at "\" height=\"" c̲at (f̲ormat (ˢᵛw̲hole 2.0 × 6 s̲elect V)) c̲at "\" fill=\"#000000\"/>")
f̲ormat< expands to
"<rect x=\"" c̲at (f̲ormat (ˢᵛw̲hole CX − a)) c̲at "\" y=\"" c̲at (f̲ormat (ˢᵛw̲hole CY − v)) c̲at "\" width=\"" c̲at (f̲ormat (ˢᵛw̲hole 2.0 × a)) c̲at "\" height=\"" c̲at (f̲ormat (ˢᵛw̲hole 2.0 × v)) c̲at "\" fill=\"" c̲at (f̲ormat (ʰc̲olor k)) c̲at "\" stroke=\"#000000\" stroke-width=\"2\"/>"
ʰd̲oor : Int -> Char
ʰd̲oor ← { g → 0 = t̲ally g ? "" k ← f̲irst g a ← (k + 1) s̲elect H v ← (k + 1) s̲elect V @ f̲ormat< "<rect class=\"entity\" x=\"{sv:w_hole CX - 0.45 * a}\" y=\"{sv:w_hole CY - 0.7 * v}\" width=\"{sv:w_hole 0.9 * a}\" height=\"{sv:w_hole 1.7 * v}\" fill=\"#2a9d8f\" stroke=\"#a8f0e6\" stroke-width=\"3\"/><text x=\"{sv:w_hole CX}\" y=\"{sv:w_hole CY + 0.5 * v}\" font-size=\"{sv:w_hole 0.9 * a}\" text-anchor=\"middle\" font-family=\"monospace\" font-weight=\"bold\" fill=\"#ffffff\">></text>" }
f̲ormat< expands to
"<rect class=\"entity\" x=\"" c̲at (f̲ormat (ˢᵛw̲hole CX − 0.45 × a)) c̲at "\" y=\"" c̲at (f̲ormat (ˢᵛw̲hole CY − 0.7 × v)) c̲at "\" width=\"" c̲at (f̲ormat (ˢᵛw̲hole 0.9 × a)) c̲at "\" height=\"" c̲at (f̲ormat (ˢᵛw̲hole 1.7 × v)) c̲at "\" fill=\"#2a9d8f\" stroke=\"#a8f0e6\" stroke-width=\"3\"/><text x=\"" c̲at (f̲ormat (ˢᵛw̲hole CX)) c̲at "\" y=\"" c̲at (f̲ormat (ˢᵛw̲hole CY + 0.5 × v)) c̲at "\" font-size=\"" c̲at (f̲ormat (ˢᵛw̲hole 0.9 × a)) c̲at "\" text-anchor=\"middle\" font-family=\"monospace\" font-weight=\"bold\" fill=\"#ffffff\">></text>"
ʰv̲iew : (Int, Int, Int, Int) -> Box Char
The view: the ceiling and the floor, the wall ahead, the way out if it is in sight, then each depth's sides from far to near.
ʰv̲iew ← { (G, at, f, goal) → (F, L, R) ← ʰl̲ook (at, f) D ← ʰd̲epth (G, F) Lw ← ʰw̲all (G, L) Rw ← ʰw̲all (G, R) back ← @ f̲ormat< "<rect y=\"60\" width=\"800\" height=\"250\" fill=\"{CEILING}\"/><rect y=\"310\" width=\"800\" height=\"250\" fill=\"{FLOOR}\"/>" front ← ʰf̲ront D door ← ʰd̲oor w̲here goal = D t̲ake F ks ← r̲ev (r̲ange D) − 1 sides ← '{ k → e̲nclose (ʰs̲ide (k, -1.0, (k + 1) s̲elect Lw, (k + 2) s̲elect Lw)) c̲at ʰs̲ide (k, 1.0, (k + 1) s̲elect Rw, (k + 2) s̲elect Rw) } e̲ach ks (e̲nclose back) c̲at (e̲nclose front) c̲at (e̲nclose door) c̲at sides }
f̲ormat< expands to
("<rect y=\"60\" width=\"800\" height=\"250\" fill=\"" c̲at (f̲ormat (CEILING)) c̲at "\"/><rect y=\"310\" width=\"800\" height=\"250\" fill=\"" c̲at (f̲ormat (FLOOR)) c̲at "\"/>")
ʰb̲lock : (Int, Int, Int, Float, Float) -> Box Char
A block of the mini map: a wall or the floor, the way out teal.
ʰb̲lock ← { (i, wall, out, x0, y0) → fill ← d̲isclose (1 + wall + 2 × out) s̲elect "#2b2d42" "#c9a66b" "#2a9d8f" e̲nclose @ f̲ormat< "<rect x=\"{sv:w_hole x0 + 12.0 * f_loat (i - 1) m_od B}\" y=\"{sv:w_hole y0 + 12.0 * f_loat (i - 1) d_iv B}\" width=\"12\" height=\"12\" fill=\"{fill}\"/>" }
f̲ormat< expands to
("<rect x=\"" c̲at (f̲ormat (ˢᵛw̲hole x0 + 12.0 × f̲loat (i − 1) m̲od B)) c̲at "\" y=\"" c̲at (f̲ormat (ˢᵛw̲hole y0 + 12.0 × f̲loat (i − 1) d̲iv B)) c̲at "\" width=\"12\" height=\"12\" fill=\"" c̲at (f̲ormat (fill)) c̲at "\"/>")
ʰm̲ap : (Num a, Truthy b) => (Int, Int, a, b, Int) -> Box Char
The mini map in the top right corner: the blocks seen, and you, an arrow the way you face.
ʰm̲ap ← { (G, at, f, seen, goal) → x0 ← 788.0 − 12.0 × f̲loat B y0 ← 72.0 frame ← e̲nclose @ f̲ormat< "<rect x=\"{sv:w_hole x0 - 6.0}\" y=\"{sv:w_hole y0 - 6.0}\" width=\"{sv:w_hole 12.0 + 12.0 * f_loat B}\" height=\"{sv:w_hole 12.0 + 12.0 * f_loat BH}\" fill=\"#000000\" opacity=\"0.75\" stroke=\"#888888\"/>" ids ← w̲here seen blocks ← '{ i → ʰb̲lock (i, i s̲elect G, i = goal, x0, y0) } e̲ach ids x ← x0 + 6.0 + 12.0 × f̲loat (at − 1) m̲od B y ← y0 + 6.0 + 12.0 × f̲loat (at − 1) d̲iv B you ← e̲nclose @ f̲ormat< "<g class=\"entity you\" transform=\"translate({sv:w_hole x} {sv:w_hole y}) rotate({(90 * f) - 90})\"><polygon points=\"8,0 -5,-5 -5,5\" fill=\"#e63946\"/></g>" frame c̲at blocks c̲at you }
f̲ormat< expands to
("<rect x=\"" c̲at (f̲ormat (ˢᵛw̲hole x0 − 6.0)) c̲at "\" y=\"" c̲at (f̲ormat (ˢᵛw̲hole y0 − 6.0)) c̲at "\" width=\"" c̲at (f̲ormat (ˢᵛw̲hole 12.0 + 12.0 × f̲loat B)) c̲at "\" height=\"" c̲at (f̲ormat (ˢᵛw̲hole 12.0 + 12.0 × f̲loat BH)) c̲at "\" fill=\"#000000\" opacity=\"0.75\" stroke=\"#888888\"/>")
f̲ormat< expands to
("<g class=\"entity you\" transform=\"translate(" c̲at (f̲ormat (ˢᵛw̲hole x)) c̲at " " c̲at (f̲ormat (ˢᵛw̲hole y)) c̲at ") rotate(" c̲at (f̲ormat ((90 × f) − 90)) c̲at ")\"><polygon points=\"8,0 -5,-5 -5,5\" fill=\"#e63946\"/></g>")
ʰm̲apIf : (Num a, Truthy b, Num c) => (Int, Int, a, b, Int, c) -> Box Char
The mini map when it shows, else nothing.
ʰm̲apIf ← { (G, at, f, seen, goal, mp) → mp = 0 ? 0 r̲eshape e̲nclose ""◆ ʰm̲ap (G, at, f, seen, goal) }
ʰs̲ays : (Num b, Any c) => (a, Int, b, c) -> Char
What the top line says.
ʰs̲ays ← { (n, f, won, mp) → won = 1 ? @ f̲ormat< "You found the way out in {n} steps!" face ← (1 + f) s̲elect "NESW" @ f̲ormat< "Steps {n} | Facing {face} | Arrows: step and turn | m: map" }
f̲ormat< expands to
"Steps " c̲at (f̲ormat (n)) c̲at " | Facing " c̲at (f̲ormat (face)) c̲at " | Arrows: step and turn | m: map"
ˡd̲raw : (Truthy b, Num c) => (Int, Int, Int, a, b, c, Int) -> Char
The picture, as SVG text: the view ahead (cut off at the picture's edges), the mini map when it shows, and the top line.
ˡd̲raw ← { (G, at, f, n, seen, mp, goal) → v ← 0.0 0.0 800.0 560.0 m ← ʰm̲apIf (G, at, f, seen, goal, mp) top ← (20.0, 42.0, 28.0) ˢᵛw̲ords ʰs̲ays (n, f, 0 + at = goal, mp) clip ← e̲nclose "<clipPath id=\"view\"><rect width=\"800\" height=\"560\"/></clipPath><g clip-path=\"url(#view)\">" v ˢᵛp̲icture (v ˢᵛr̲ect "fill=\"#111111\"") c̲at clip c̲at (ʰv̲iew (G, at, f, goal)) c̲at (e̲nclose "</g>") c̲at m c̲at top }