sourceextensions/scene/demos/voxels-rubik.xtl

1⍝│ Voxels, 8: a Rubik's cube. 26 dark cubies with their colored stickers, 2⍝│ drawn as voxels; the cube itself is arrays (Rubik.xtl): its 54 3⍝│ stickers, each where it is and which way it faces, and each of the 4⍝│ twelve quarter turns a permutation of them, computed from the cube's 5⍝│ geometry. This demo checks the turns -- each a permutation, four of 6⍝│ one solved, a turn and its undoing solved, R U R' U' of order 6, R U 7⍝│ of order 105, and U and R turning the right way -- and shows the 8⍝│ solved cube, turning slowly; keys turn its faces. 9⍝│ Run: just demo scene voxels-rubik 10⍝│ u d r l f b turn a face clockwise (as you look at it), Shift with the 11⍝│ letter counterclockwise, z undo (back to solved), Space a scramble 12⍝│ (25 random turns), 0 reset, drag to look around, q quit 13 14"rb:" u̲se< "Rubik" 15"sc:" u̲se< "Scene" 16 17⍝━━ The turns, checked 18 19s ← ʳᵇsolved 20"each turn a permutation of the 54 stickers:" 21'{ (s̲ort ⍵ s̲elect ʳᵇmoves) m̲atch r̲ange 54 } e̲ach r̲ange 12 22ᵘf̲our ← { i → (i ʳᵇt̲urn i ʳᵇt̲urn i ʳᵇt̲urn i ʳᵇt̲urn s) m̲atch s } 23"four of each turn: solved" 24'ᵘf̲our e̲ach r̲ange 12 25ᵘb̲ack ← { i → ((ʳᵇu̲ndo i) ʳᵇt̲urn i ʳᵇt̲urn s) m̲atch s } 26"each turn then its undoing: solved" 27'ᵘb̲ack e̲ach r̲ange 12 28ᵘs̲exy ← { c → 6 ʳᵇt̲urn 2 ʳᵇt̲urn 5 ʳᵇt̲urn 1 ʳᵇt̲urn c } 29ᵘs̲exyN ← { n → (n 'ᵘs̲exy p̲ower s) m̲atch s } 30"R U R' U' solved after 6, not after 1 to 5:" 31(ᵘs̲exyN 6) c̲at 'ᵘs̲exyN e̲ach r̲ange 5 32ᵘr̲u ← { c → 1 ʳᵇt̲urn 5 ʳᵇt̲urn c } 33ᵘr̲uN ← { n → (n 'ᵘr̲u p̲ower s) m̲atch s } 34"R U solved after 105, and how many of 1 to 104:" 35(ᵘr̲uN 105) c̲at '+ r̲/ 'ᵘr̲uN e̲ach r̲ange 104 36m ← ʳᵇstickers 37top ← ((6 s̲elect₂ m) = 1) × (2 s̲elect₂ m) = 1 38"after U, the front's top row has the right face's color (2):" 39(top > 0) r̲eplicate 1 ʳᵇt̲urn s 40 41⍝━━ The cube, drawn 42 43w ← "Voxels 8: Rubik's cube -- X_eTaL" ˢᶜo̲pen! 560 560 44k ← w ˢᶜc̲amera! 0.6 0.45 7.5 0.3 45b ← ((f̲loat w) c̲at 1.0 0.1 0.1 0.11) ˢᶜq̲uads! ʳᵇbodies 46 47⍝│ the stickers, one object of quads for each color 48ᵘd̲raw ← { c → 49 _ ← '{ ((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 50 c 51} 52shown ← ᵘd̲raw s 53 54⍝━━ Turning it 55 56⍝│ The state is a tuple: (the 54 colors, the turns made so far -- 1 to 57⍝│ 12, in the order of rb:names, the history undo walks back -- and 58⍝│ whether it runs, 1 or 0). Keys: u d r l f b turn that face 59⍝│ clockwise (seen from outside it), with Shift counterclockwise; z 60⍝│ undoes the last turn; Space scrambles (25 random turns, never the 61⍝│ same face twice in a row); 0 resets. 62 63letters ← "uUdDrRlLfFbB" 64 65⍝│ m u:t_ell st: m printed, the state unchanged 66ᵘt̲ell ← { m st → 67 _ ← p̲rint! m 68 st 69} 70 71⍝│ the turns made, by name (and whether the cube is solved) 72ᵘs̲ay ← { st → 73 (c, h, _) ← st 74 0 = t̲ally h ? "solved, no turns" ᵘt̲ell st 75 names ← (n̲ot ʳᵇnames = f̲irst " ") p̲artition ʳᵇnames 76 _ ← p̲rint! "turns: " c̲at d̲isclose '{ e̲nclose (d̲isclose ⍺) c̲at " " c̲at d̲isclose ⍵ } r̲/ h s̲elect names 77 (c m̲atch ʳᵇsolved) ? "solved" ᵘt̲ell st 78 st 79} 80 81⍝│ i u:t_urnBy st: turn i made and remembered, the cube redrawn 82ᵘt̲urnBy ← { i (c, h, on) → 83 turned ← i ʳᵇt̲urn c 84 shown ← ᵘd̲raw turned 85 (turned, h c̲at i, on) 86} 87 88⍝│ the last turn undone and forgotten 89ᵘu̲ndoLast ← { (c, h, on) → 90 0 = t̲ally h ? (c, h, on) 91 undone ← (ʳᵇu̲ndo f̲irst -1 t̲ake h) ʳᵇt̲urn c 92 shown ← ᵘd̲raw undone 93 (undone, -1 d̲rop h, on) 94} 95 96⍝│ one random turn, never the same face as the one before, remembered 97⍝│ (so undo can walk back to solved) 98ᵘo̲ne ← { (c, h, on) → 99 i ← r̲oll! 12 100 prev ← f̲irst -1 t̲ake 0 c̲at h 101 ((i + 1) d̲iv 2) = (prev + 1) d̲iv 2 ? ᵘo̲ne (c, h, on) 102 (i ʳᵇt̲urn c, h c̲at i, on) 103} 104 105⍝│ 25 random turns, the cube redrawn 106ᵘs̲cramble ← { st → 107 (c, h, on) ← 25 'ᵘo̲ne p̲ower st 108 shown ← ᵘd̲raw c 109 (c, h, on) 110} 111 112⍝│ back to solved, no history 113ᵘr̲eset ← { (_, _, on) → 114 shown ← ᵘd̲raw ʳᵇsolved 115 (ʳᵇsolved, 0 r̲eshape 0, on) 116} 117 118ᵘq̲uit ← { (c, h, _) → 119 _ ← ˢᶜc̲lose! w 120 (c, h, 0) 121} 122 123⍝│ one frame: an event, and what it asks for 124ᵘf̲rame ← { st → 125 (_, _, on) ← st 126 0 = on ? st 127 v ← ˢᶜn̲ext! w 128 ((v m̲atch "close") + v m̲atch "key q") > 0 ? ᵘq̲uit st 129 (v m̲atch "key z") ? ᵘs̲ay ᵘu̲ndoLast st 130 (v m̲atch "key Space") ? ᵘs̲ay ᵘs̲cramble st 131 (v m̲atch "key 0") ? ᵘs̲ay ᵘr̲eset st 132 i ← letters i̲ndexOf f̲irst -1 t̲ake v 133 ((5 = t̲ally v) × i ≤ 12) ? ᵘs̲ay i ᵘt̲urnBy st 134 st 135} 136"keys: u d r l f b turn a face clockwise, Shift+letter counterclockwise, z undo, Space scramble, 0 reset, q quit" 137last ← 100000 'ᵘf̲rame p̲ower (ʳᵇsolved, 0 r̲eshape 0, 1) 138"bye"