sourceextensions/scene/demos/voxels-rubik-solve.xtl
1⍝│ Voxels, 13: the Rubik's cube solved. Scramble it, or turn it with the
2⍝│ six buttons, then Solve: the cube's turns so far are handed to the
3⍝│ Eigencube library (X_eTaL-libraries, at LIBRARIES_COMMIT), which holds
4⍝│ the cube as 26 rotation matrices and solves it by search, stage by
5⍝│ stage -- the first layer by plain search over the twelve turns, the
6⍝│ middle and last layers over known move sequences (slot inserts, an
7⍝│ edge flip, Sune, a corner cycle), which is what makes it fast enough
8⍝│ (2 to 3 seconds; a hundred and some moves). Then Step through the
9⍝│ solution a turn at a time, Back to undo one, or Play it through, every
10⍝│ turn animated on the voxel cube. The two models are checked to agree
11⍝│ (tests/rubik-eigencube.xtl).
12⍝│ Cover: videos/voxels-rubik-solve-cover.jpg Eigencube cover art: a learning game for all ages
13⍝│ Run: just demo scene voxels-rubik-solve
14⍝│ the buttons, or keys: u r f turn (Shift for '), Space scramble,
15⍝│ Enter solve, n step, b back, p play or pause, 0 solved again, drag
16⍝│ to look around, q quit
17
18"rb:" u̲se< "Rubik"
19"rp:" u̲se< "RubikPlay"
20"ec:" u̲se< "Eigencube"
21"sc:" u̲se< "Scene"
22
23w ← "Voxels 13: Rubik's cube, solved -- X_eTaL" ˢᶜo̲pen! 640 600
24k ← w ˢᶜc̲amera! 0.6 0.45 7.5 0.25
25_ ← (ʳᵇsolved, 0, 0.0) ʳᵇd̲raw w
26
27⍝━━ The two models
28
29⍝│ Rubik's turns (1 to 12: U U' D D' R R' L L' F F' B B') as
30⍝│ Eigencube's (U U' D D' F F' B B' R R' L L'), and back: the same vector
31map ← 1 2 3 4 9 10 11 12 5 6 7 8
32
33⍝━━ The buttons
34
35⍝│ the turns: six buttons, their turns, the keys that press them
36names ← "U U' R R' F F'"
37turns ← 1 2 5 6 9 10
38keys ← "uUrRfF"
39top ← 490.0
40⍝│ the controls: a second row, wider buttons
41controls ← "Scramble Solve Back Step Play"
42top2 ← 545.0
43cw ← 100.0
44
45⍝ both rows drawn, the turn under way's button lit
46ᵘb̲uttons ← { i →
47 a ← (w, names, top, 640.0, 100, ʳᵖbw) ʳᵖd̲rawRow turns i̲ndexOf a̲bs i
48 b ← (w, controls, top2, 640.0, 200, cw) ʳᵖd̲rawRow 0
49 _ ← w ˢᶜo̲verlay! a c̲at b
50 i
51}
52o ← ᵘb̲uttons 0
53title ← ((f̲loat w) c̲at 1.0 20.0 20.0 14.0 1.0 1.0 1.0) ˢᶜl̲abel! "Turns:"
54
55⍝ the status line (solving, the solution, solved)
56ᵘt̲ell ← { m →
57 _ ← ((f̲loat w) c̲at 3.0 20.0 44.0 14.0 0.6 1.0 0.6) ˢᶜl̲abel! m
58 m
59}
60
61"keys: u r f turn (Shift for '), Space scramble, Enter solve, n step, b back, p play or pause, 0 solved, q quit; or click the buttons"
62
63⍝━━ Playing
64
65⍝│ The state is a tuple: (the turning -- RubikPlay's colors, turns made,
66⍝│ turns waiting, turn under way and its frame -- the solution, in
67⍝│ Rubik's numbers; how many of its turns are made; the mode, 0 still, 1
68⍝│ playing, 2 solving next frame; whether it runs).
69
70⍝ the last 12 turns made, shown above the cube
71ᵘs̲ay ← { st z →
72 (_, h, _, _, _) ← st
73 _ ← ((f̲loat w) c̲at 2.0 100.0 20.0 14.0 1.0 1.0 0.6) ˢᶜl̲abel! ʳᵖn̲ames (0 − 12 m̲in t̲ally h) t̲ake h
74 st
75}
76
77⍝│ u:s_crambled n: n random turns, never the same face twice in a row
78ᵘs̲crambled ← { n →
79 (_, ts) ← (n, 0 r̲eshape 0) ᵘm̲ore 0
80 ts
81}
82ᵘm̲ore ← { (n, ts) z →
83 n = 0 ? (n, ts)
84 t ← r̲oll! 12
85 prev ← f̲irst -1 t̲ake 0 c̲at ts
86 ((t + 1) d̲iv 2) = (prev + 1) d̲iv 2 ? (n, ts) ᵘm̲ore 0
87 (n − 1, ts c̲at t) ᵘm̲ore 0
88}
89
90⍝│ u:s_olution st: the turns that solve the cube made so far (Rubik's
91⍝│ numbers), found by Eigencube from the same turns
92ᵘs̲olution ← { st →
93 (_, h, _, _, _) ← st
94 (m, _, _) ← 1 ᵉᶜs̲olve (h s̲elect map) ᵉᶜd̲o ᵉᶜsolved
95 m s̲elect map
96}
97
98⍝ the turn or control of event v: a turn 1 to 12 (positive), a control
99⍝ 1 to 5 (negative), else 0
100ᵘp̲ressed ← { v →
101 b ← (v, 6, top, 640.0, ʳᵖbw) ʳᵖh̲it 0
102 b > 0 ? b s̲elect turns
103 c ← (v, 5, top2, 640.0, cw) ʳᵖh̲it 0
104 c > 0 ? 0 − c
105 i ← keys i̲ndexOf f̲irst -1 t̲ake v
106 ((5 = t̲ally v) × i ≤ 6) ? i s̲elect turns
107 ks ← (v m̲atch "key Space") c̲at (v m̲atch "key Enter") c̲at (v m̲atch "key b") c̲at (v m̲atch "key n") c̲at v m̲atch "key p"
108 0 < '+ r̲/ ks ? 0 − f̲irst w̲here ks
109 0
110}
111
112⍝ a turn by hand: queued; any solution is stale
113ᵘt̲urnBy ← { t (st, sol, pos, mode, on) →
114 z ← ᵘt̲ell " "
115 (t ʳᵖq̲ueue st, 0 r̲eshape 0, 0, 0, on)
116}
117
118⍝ the controls: 1 scramble, 2 solve, 3 back, 4 step, 5 play or pause
119ᵘc̲ontrol ← { k all →
120 (st, sol, pos, mode, on) ← all
121 0 = ʳᵖi̲dle st ? all
122 k = 1 ? ᵘs̲cramble all
123 k = 2 ? ᵘs̲olveSoon all
124 k = 3 ? ᵘb̲ack all
125 k = 4 ? ᵘs̲tep all
126 ᵘp̲lay all
127}
128ᵘs̲cramble ← { (st, sol, pos, mode, on) →
129 z ← ᵘt̲ell "Scrambled: 20 turns"
130 ((ᵘs̲crambled 20) ʳᵖq̲ueue st, 0 r̲eshape 0, 0, 0, on)
131}
132ᵘs̲olveSoon ← { (st, sol, pos, mode, on) →
133 (c, _, _, _, _) ← st
134 c m̲atch ʳᵇsolved ? (st, sol, pos, mode, on) ᵘs̲ayIt "Already solved"
135 z ← ᵘt̲ell "Solving..."
136 (st, sol, pos, 2, on)
137}
138ᵘs̲ayIt ← { all m →
139 z ← ᵘt̲ell m
140 all
141}
142ᵘb̲ack ← { (st, sol, pos, mode, on) →
143 pos = 0 ? (st, sol, pos, mode, on)
144 z ← ᵘt̲ell "Solution: " c̲at (f̲ormat pos − 1) c̲at " of " c̲at (f̲ormat t̲ally sol) c̲at " made"
145 (ʳᵖu̲ndo st, sol, pos − 1, 0, on)
146}
147ᵘs̲tep ← { (st, sol, pos, mode, on) →
148 pos ≥ t̲ally sol ? (st, sol, pos, 0, on)
149 z ← ᵘt̲ell "Solution: " c̲at (f̲ormat pos + 1) c̲at " of " c̲at (f̲ormat t̲ally sol) c̲at " made"
150 (((pos + 1) s̲elect sol) ʳᵖq̲ueue st, sol, pos + 1, mode, on)
151}
152ᵘp̲lay ← { (st, sol, pos, mode, on) →
153 pos ≥ t̲ally sol ? (st, sol, pos, 0, on)
154 (st, sol, pos, 1 − mode, on)
155}
156
157⍝ the solve, a frame after "Solving..." was shown
158ᵘs̲olveNow ← { (st, sol, pos, mode, on) →
159 solution ← ᵘs̲olution st
160 (_, h, _, _, _) ← st
161 _ ← p̲rint! "solved by " c̲at (f̲ormat t̲ally solution) c̲at " turns; the cube solved by them: " c̲at f̲ormat (ʳᵖa̲pplied h c̲at solution) m̲atch ʳᵇsolved
162 z ← ᵘt̲ell "Solution: " c̲at (f̲ormat t̲ally solution) c̲at " turns -- Step or Play"
163 (st, solution, 0, 0, on)
164}
165
166⍝ playing: the next turn of the solution whenever the cube is still
167ᵘp̲layOn ← { all →
168 (st, sol, pos, mode, on) ← all
169 0 = (mode = 1) × ʳᵖi̲dle st ? all
170 pos ≥ t̲ally sol ? (st, sol, pos, 0, on) ᵘs̲ayIt "Solved in " c̲at (f̲ormat t̲ally sol) c̲at " turns"
171 ᵘs̲tep all
172}
173
174ᵘr̲eset ← { (st, sol, pos, mode, on) →
175 d ← (ʳᵇsolved, 0, 0.0) ʳᵇd̲raw w
176 z ← ᵘt̲ell " "
177 (ʳᵖstart ᵘs̲ay 0, 0 r̲eshape 0, 0, 0, on)
178}
179
180ᵘm̲ade ← { (_, h, _, _, _) → t̲ally h }
181ᵘu̲nder ← { (_, _, _, i, _) → i }
182ᵘs̲ayIf ← { yes st →
183 yes ? st ᵘs̲ay 0
184 st
185}
186
187⍝│ one frame: an event (a turn, a control, solved again, quit), the
188⍝│ solve if it is due, playing, then a frame of turning: 4 frames a
189⍝│ quarter turn while more wait, 5 while playing, else 8
190ᵘf̲rame ← { all →
191 (st, sol, pos, mode, on) ← all
192 0 = on ? all
193 v ← ˢᶜn̲ext! w
194 ((v m̲atch "close") + v m̲atch "key q") > 0 ? (st, sol, pos, mode, 0)
195 (v m̲atch "key 0") ? ᵘr̲eset all
196 solving ← (mode = 2) ᵘs̲olveIf all
197 p ← ᵘp̲ressed v
198 played ← ᵘp̲layOn (p, solving) ᵘa̲ct 0
199 (cube, moves, place, phase, lamp) ← played
200 (_, _, q, _, _) ← cube
201 fr ← ((phase = 1) × 5) + (phase ≠ 1) × 8 − 4 × 0 < t̲ally q
202 n ← ᵘm̲ade cube
203 was ← ᵘu̲nder cube
204 advanced ← (w, fr) ʳᵖa̲dvance cube
205 _ ← (was ≠ ᵘu̲nder advanced) ᵘl̲ightIf ᵘu̲nder advanced
206 ((n ≠ ᵘm̲ade advanced) ᵘs̲ayIf advanced, moves, place, phase, lamp)
207}
208ᵘs̲olveIf ← { yes all →
209 yes ? ᵘs̲olveNow all
210 all
211}
212ᵘa̲ct ← { (p, all) z →
213 p > 0 ? p ᵘt̲urnBy all
214 p < 0 ? (0 − p) ᵘc̲ontrol all
215 all
216}
217ᵘl̲ightIf ← { yes i →
218 yes ? ᵘb̲uttons i
219 i
220}
221
222last ← 100000 'ᵘf̲rame p̲ower (ʳᵖstart, 0 r̲eshape 0, 0, 0, 1)
223_ ← ˢᶜc̲lose! w
224⍝ (an accessor, not a top-level tuple pattern: xetal doc would list a
225⍝ hidden %N value for one, ask E10)
226ᵘc̲olorsOf ← { ((c, _, _, _, _), _, _, _, _) → c }
227ᵘt̲urnsOf ← { ((_, h, _, _, _), _, _, _, _) → h }
228ᵘm̲adeOf ← { (_, _, pos, _, _) → pos }
229c ← ᵘc̲olorsOf last
230h ← ᵘt̲urnsOf last
231pos ← ᵘm̲adeOf last
232"turns made in all, and the solution's turns made:"
233(t̲ally h) c̲at pos
234"the cube solved at the end:"
235c m̲atch ʳᵇsolved
236"bye"