programextensions/scene/demos/voxels-rubik.xtl
Voxels, 8: a Rubik's cube. 26 dark cubies with their colored stickers, drawn as voxels; the cube itself is arrays (Rubik.xtl): its 54 stickers, each where it is and which way it faces, and each of the twelve quarter turns a permutation of them, computed from the cube's geometry. This demo checks the turns -- each a permutation, four of one solved, a turn and its undoing solved, R U R' U' of order 6, R U of order 105, and U and R turning the right way -- and shows the solved cube, turning slowly; keys turn its faces. Run: just demo scene voxels-rubik u d r l f b turn a face clockwise (as you look at it), Shift with the letter counterclockwise, z undo (back to solved), Space a scramble (25 random turns), 0 reset, drag to look around, q quit
The turns, checked
The cube, drawn
k : Int
k ← w ˢᶜc̲amera! 0.6 0.45 7.5 0.3
ᵘd̲raw : Int -> Int
the stickers, one object of quads for each color
ᵘd̲raw ← { c → _ ← '{ ((f̲loat w) c̲at (10.0 + f̲loat ⍵) c̲at (⍵ + 1) s̲elect ʳᵇrgb) ˢᶜq̲uads! ⍵ ʳᵇc̲olorSquares c } m̲ap (r̲ange 6) − 1 c }
Turning it
ᵘt̲ell : (Any a, Any b) => a -> b -> b
m u:t_ell st: m printed, the state unchanged
ᵘt̲ell ← { m st → _ ← p̲rint! m st }
ᵘs̲ay : Any a => (Int, Int, a) -> (Int, Int, a)
the turns made, by name (and whether the cube is solved)
ᵘs̲ay ← { st → (c, h, _) ← st 0 = t̲ally h ? "solved, no turns" ᵘt̲ell st names ← (n̲ot ʳᵇnames = f̲irst " ") p̲artition ʳᵇnames _ ← p̲rint! "turns: " c̲at d̲isclose '{ e̲nclose (d̲isclose ⍺) c̲at " " c̲at d̲isclose ⍵ } r̲/ h s̲elect names (c m̲atch ʳᵇsolved) ? "solved" ᵘt̲ell st st }
ᵘt̲urnBy : Any a => Int -> (Int, Int, a) -> (Int, Int, a)
i u:t_urnBy st: turn i made and remembered, the cube redrawn
ᵘt̲urnBy ← { i (c, h, on) → turned ← i ʳᵇt̲urn c shown ← ᵘd̲raw turned (turned, h c̲at i, on) }
ᵘu̲ndoLast : Any a => (Int, Int, a) -> (Int, Int, a)
the last turn undone and forgotten
ᵘu̲ndoLast ← { (c, h, on) → 0 = t̲ally h ? (c, h, on) undone ← (ʳᵇu̲ndo f̲irst -1 t̲ake h) ʳᵇt̲urn c shown ← ᵘd̲raw undone (undone, -1 d̲rop h, on) }
ᵘo̲ne : Any b => (a, Int, b) -> (a, Int, b)
one random turn, never the same face as the one before, remembered (so undo can walk back to solved)
ᵘo̲ne ← { (c, h, on) → i ← r̲oll! 12 prev ← f̲irst -1 t̲ake 0 c̲at h ((i + 1) d̲iv 2) = (prev + 1) d̲iv 2 ? ᵘo̲ne (c, h, on) (i ʳᵇt̲urn c, h c̲at i, on) }
ᵘs̲cramble : Any a => (Int, Int, a) -> (Int, Int, a)
25 random turns, the cube redrawn
ᵘs̲cramble ← { st → (c, h, on) ← 25 'ᵘo̲ne p̲ower st shown ← ᵘd̲raw c (c, h, on) }
ᵘr̲eset : (Any a, Any b, Any c, Num d) => (a, b, c) -> (Int, d, c)
back to solved, no history
ᵘr̲eset ← { (_, _, on) → shown ← ᵘd̲raw ʳᵇsolved (ʳᵇsolved, 0 r̲eshape 0, on) }
ᵘq̲uit : (Any a, Any b, Any c, Num d) => (a, b, c) -> (a, b, d)
ᵘq̲uit ← { (c, h, _) → _ ← ˢᶜc̲lose! w (c, h, 0) }
ᵘf̲rame : Num a => (Int, Int, a) -> (Int, Int, a)
one frame: an event, and what it asks for
ᵘf̲rame ← { st → (_, _, on) ← st 0 = on ? st v ← ˢᶜn̲ext! w ((v m̲atch "close") + v m̲atch "key q") > 0 ? ᵘq̲uit st (v m̲atch "key z") ? ᵘs̲ay ᵘu̲ndoLast st (v m̲atch "key Space") ? ᵘs̲ay ᵘs̲cramble st (v m̲atch "key 0") ? ᵘs̲ay ᵘr̲eset st i ← letters i̲ndexOf f̲irst -1 t̲ake v ((5 = t̲ally v) × i ≤ 12) ? ᵘs̲ay i ᵘt̲urnBy st st }