-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcube.js
528 lines (495 loc) · 19 KB
/
cube.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*
Cube representation. Eight corners (c) with three orientations. Twelve edges (e) which may be flipped.
Separately (not strictly part of the cube), a view (v) specifying U, D, L, R, F, B. This controls the "camera" when displaying.
Corners (above view);
Permutation: Top: 1 4 Bottom: 5 8
2 3 6 7
Orientation: 1 = U/D on F/B
2 = U/D on L/R
3 = U/D on U/D (oriented)
Edges (above view):
Permutation: Top: 1 Middle: 5 8 Bottom: 9
2 4 10 12
3 6 7 11
Orientation: F/B moves flip (not F2/B2, which is like F F; canceling)
Permutation of zero (0) means that this piece is not to be considered in comparisons and renders gray.
*/
var Cube = (function () {
var solved = {
c: [{ p: 1, o: 3 },
{ p: 2, o: 3 },
{ p: 3, o: 3 },
{ p: 4, o: 3 },
{ p: 5, o: 3 },
{ p: 6, o: 3 },
{ p: 7, o: 3 },
{ p: 8, o: 3 }],
e: [{ p: 1, o: 1 },
{ p: 2, o: 1 },
{ p: 3, o: 1 },
{ p: 4, o: 1 },
{ p: 5, o: 1 },
{ p: 6, o: 1 },
{ p: 7, o: 1 },
{ p: 8, o: 1 },
{ p: 9, o: 1 },
{ p: 10, o: 1 },
{ p: 11, o: 1 },
{ p: 12, o: 1 }],
v: [0, 1, 2, 3, 4, 5],
m: {}
}
function compare(cube0, cube1) {
for (var c = 0; c < 8; c++) {
var c0 = cube0.c[c];
var c1 = cube1.c[c];
if (c0.p != 0 && c1.p != 0 && (c0.p != c1.p || c0.o != c1.o)) return false;
}
for (var e = 0; e < 12; e++) {
var e0 = cube0.e[e];
var e1 = cube1.e[e];
if (e0.p != 0 && e1.p != 0 && (e0.p != e1.p || e0.o != e1.o)) return false;
}
return true;
}
function map(mapping, cube) {
// corners
function co(m, o) {
if (m) {
switch (m) {
case 0: return o;
case 1: return o == 1 ? 1 : o == 2 ? 3 : 2;
case 2: return o == 2 ? 2 : o == 1 ? 3 : 1;
case 3: return o == 3 ? 3 : o == 2 ? 1 : 2;
}
} else return o;
}
var cs = cube.c.slice(0);
if (mapping.c) {
for (var j in mapping.c) {
var m = mapping.c[j];
var i = m.i;
var n = m.p || i;
var x = cube.c[n - 1];
var c = { o: x.o, p: x.p }; // clone
c.o = co(m.o, c.o);
cs[i - 1] = c;
}
}
// edges
function eo(m, o) {
if (m) {
return m == 1 ? o == 1 ? 2 : 1 : o;
} else return o;
}
var es = cube.e.slice(0);
if (mapping.e) {
for (var j in mapping.e) {
var m = mapping.e[j];
var i = m.i;
var n = m.p || i;
var x = cube.e[n - 1];
var e = { o: x.o, p: x.p }; // clone
e.o = eo(m.o, e.o);
es[i - 1] = e;
}
}
// view
var vs = cube.v.slice(0);
if (mapping.v) {
var m = mapping.v;
for (var i = 0; i < 6; i++) {
vs[i] = cube.v[m[i]];
}
}
var ms = JSON.parse(JSON.stringify(cube.m));
return { c: cs, e: es, v: vs, m: ms };
}
var orientations = [
[0, 1, 2, 3, 4, 5], // YR
[4, 5, 3, 2, 0, 1], // RY
[0, 1, 4, 5, 3, 2], // YG
[3, 2, 5, 4, 0, 1], // GY
[0, 1, 3, 2, 5, 4], // YO
[5, 4, 2, 3, 0, 1], // OY
[0, 1, 5, 4, 2, 3], // YB
[2, 3, 4, 5, 0, 1], // BY
[3, 2, 0, 1, 4, 5], // GR
[4, 5, 1, 0, 3, 2], // RG
[5, 4, 0, 1, 3, 2], // OG
[3, 2, 1, 0, 5, 4], // GO
[2, 3, 0, 1, 5, 4], // BO
[5, 4, 1, 0, 2, 3], // OB
[4, 5, 0, 1, 2, 3], // RB
[2, 3, 1, 0, 4, 5], // BR
[1, 0, 3, 2, 4, 5], // WR
[4, 5, 2, 3, 1, 0], // RW
[1, 0, 4, 5, 2, 3], // WB
[2, 3, 5, 4, 1, 0], // BW
[1, 0, 2, 3, 5, 4], // WO
[5, 4, 3, 2, 1, 0], // OW
[1, 0, 5, 4, 3, 2], // WG
[3, 2, 4, 5, 1, 0]] // GW
function same(cube0, cube1) {
// compare all (24) orientations
for (o in orientations) {
if (compare(map({ v: orientations[o] }, cube0), cube1)) return true;
}
return false;
}
function twist(notation, cube) {
var maps = [ // U, D, L, R, F, B twists in original orientation (remapped as necessary below)
{ c: [{ i: 1, p: 2, o: 3 }, { i: 2, p: 3, o: 3 }, { i: 3, p: 4, o: 3 }, { i: 4, p: 1, o: 3 }],
e: [{ i: 1, p: 2 }, { i: 2, p: 3 }, { i: 3, p: 4 }, { i: 4, p: 1 }]},
{ c: [{ i: 5, p: 8, o: 3 }, { i: 6, p: 5, o: 3 }, { i: 7, p: 6, o: 3 }, { i: 8, p: 7, o: 3 }],
e: [{ i: 9, p: 12 }, { i: 10, p: 9 }, { i: 11, p: 10 }, { i: 12, p: 11 }]},
{ c: [{ i: 1, p: 5, o: 2 }, { i: 2, p: 1, o: 2 }, { i: 5, p: 6, o: 2 }, { i: 6, p: 2, o: 2 }],
e: [{ i: 2, p: 5 }, { i: 6, p: 2 }, { i: 10, p: 6 }, { i: 5, p: 10 }]},
{ c: [{ i: 3, p: 7, o: 2 }, { i: 4, p: 3, o: 2 }, { i: 7, p: 8, o: 2 }, { i: 8, p: 4, o: 2 }],
e: [{ i: 4, p: 7 }, { i: 7, p: 12 }, { i: 8, p: 4 }, { i: 12, p: 8 }]},
{ c: [{ i: 2, p: 6, o: 1 }, { i: 3, p: 2, o: 1 }, { i: 6, p: 7, o: 1 }, { i: 7, p: 3, o: 1 }],
e: [{ i: 3, p: 6, o: 1 }, { i: 6, p: 11, o: 1 }, { i: 7, p: 3, o: 1 }, { i: 11, p: 7, o: 1 }]},
{ c: [{ i: 1, p: 4, o: 1 }, { i: 4, p: 8, o: 1 }, { i: 5, p: 1, o: 1 }, { i: 8, p: 5, o: 1 }],
e: [{ i: 1, p: 8, o: 1 }, { i: 5, p: 1, o: 1 }, { i: 8, p: 9, o: 1 }, { i: 9, p: 5, o: 1 }]}];
var u = maps[cube.v[0]];
var d = maps[cube.v[1]];
var l = maps[cube.v[2]];
var r = maps[cube.v[3]];
var f = maps[cube.v[4]];
var b = maps[cube.v[5]];
var x = { v: [4, 5, 2, 3, 1, 0] };
var y = { v: [0, 1, 4, 5, 3, 2] };
var z = { v: [2, 3, 1, 0, 4, 5] };
switch (notation) {
case "": return cube; // null move
// normal moves
case "U": return map(u, cube);
case "U2":
case "U2'": return map(u, map(u, cube));
case "U'": return map(u, map(u, map(u, cube)));
case "D": return map(d, cube);
case "D2":
case "D2'": return map(d, map(d, cube));
case "D'": return map(d, map(d, map(d, cube)));
case "L": return map(l, cube);
case "L2":
case "L2'": return map(l, map(l, cube));
case "L'": return map(l, map(l, map(l, cube)));
case "R": return map(r, cube);
case "R2":
case "R2'": return map(r, map(r, cube));
case "R'": return map(r, map(r, map(r, cube)));
case "F": return map(f, cube);
case "F2":
case "F2'": return map(f, map(f, cube));
case "F'": return map(f, map(f, map(f, cube)));
case "B": return map(b, cube);
case "B2":
case "B2'": return map(b, map(b, cube));
case "B'": return map(b, map(b, map(b, cube)));
// slice moves
case "M": return map(l, map(l, map(l, map(r, map(x, map(x, map(x, cube)))))));
case "M2":
case "M2'": return map(l, map(l, map(r, map(r, map(x, map(x, cube))))));
case "M'": return map(l, map(r, map(r, map(r, map(x, cube)))));
case "E": return map(u, map(d, map(d, map(d, map(y, map(y, map(y, cube)))))));
case "E2":
case "E2'": return map(u, map(u, map(d, map(d, map(y, map(y, cube))))));
case "E'": return map(u, map(u, map(u, map(d, map(y, cube)))));
case "S": return map(f, map(f, map(f, map(b, map(z, cube)))));
case "S2":
case "S2'": return map(f, map(f, map(b, map(b, map(z, map(z, cube))))));
case "S'": return map(f, map(b, map(b, map(b, map(z, map(z, map(z, cube)))))));
// wide moves
case "u":
case "Uw": return map(y, map(d, cube));
case "u2":
case "u2'":
case "Uw2":
case "Uw2'": return map(y, map(y, map(d, map(d, cube))));
case "u'":
case "Uw'": return map(y, map(y, map(y, map(d, map(d, map(d, cube))))));
case "d":
case "Dw": return map(y, map(y, map(y, map(u, cube))));
case "d2":
case "d2'":
case "Dw2":
case "Dw2'": return map(y, map(y, map(u, map(u, cube))));
case "d'":
case "Dw'": return map(y, map(u, map(u, cube)));
case "l":
case "Lw": return map(x, map(x, map(x, map(r, cube))));
case "l2":
case "l2'":
case "Lw2":
case "Lw2'": return map(x, map(x, map(r, map(r, cube))));
case "l'":
case "Lw'": return map(x, map(r, map(r, map(r, cube))));
case "r":
case "Rw": return map(x, map(l, cube));
case "r2":
case "r2'":
case "Rw2":
case "Rw2'": return map(x, map(x, map(l, map(l, cube))));
case "r'":
case "Rw'": return map(x, map(x, map(x, map(l, map(l, map(l, cube))))));
case "f":
case "Fw": return map(z, map(b, cube));
case "f2":
case "f2'":
case "Fw2":
case "Fw2'": return map(z, map(z, map(b, map(b, cube))));
case "f'":
case "Fw'": return map(z, map(z, map(z, map(b, map(b, map(b, cube))))));
case "b":
case "Bw": return map(z, map(z, map(z, map(f, cube))));
case "b2":
case "b2'":
case "Bw2":
case "Bw2'": return map(z, map(z, map(f, map(f, cube))));
case "b'":
case "Bw'": return map(z, map(f, map(f, map(f, cube))));
// relative cube orientations
case "x": return map(x, cube);
case "x2": return map(x, map(x, cube));
case "x'": return map(x, map(x, map(x, cube)));
case "y": return map(y, cube);
case "y2": return map(y, map(y, cube));
case "y'": return map(y, map(y, map(y, cube)));
case "z": return map(z, cube);
case "z2": return map(z, map(z, cube));
case "z'": return map(z, map(z, map(z, cube)));
default: throw "Unknown notation: " + notation;
}
}
function inverseAlg(twists) {
var rev = twists.reverse();
for (var i = 0; i < rev.length; i++) {
var r = rev[i];
if (r == "") {
twists[i] = "";
continue;
}
var m = r.length - 1;
switch (r[m]) {
case "'":
twists[i] = r.substr(0, m);
break;
case "2":
twists[i] = r;
break;
default:
twists[i] = r + "'";
break;
}
}
return twists;
}
function alg(alg, cube, inverse) {
var twists = alg.split(' ');
if (inverse) twists = inverseAlg(twists);
for (var t in twists) {
cube = twist(twists[t], cube);
}
return cube;
}
function random(algs, n, cube) {
for (var i = 0; i < n; i++) {
cube = alg(algs[Math.floor(Math.random() * algs.length)], cube);
}
return cube;
}
function canonical(face) {
return face.split('').sort().join('');
}
const corners = {
"UBL": 1,
"ULF": 2,
"UFR": 3,
"URB": 4,
"DLB": 5,
"DFL": 6,
"DRF": 7,
"DBR": 8,
// canonical order also
"BLU": 1,
"FLU": 2,
"FRU": 3,
"BRU": 4,
"BDL": 5,
"DFR": 7,
"BDR": 8 };
var cornerKeys = Object.keys(corners);
function cornerNum(corner) { return corners[corner.toUpperCase()]; }
const edges = {
"UB": 1,
"UL": 2,
"UF": 3,
"UR": 4,
"BL": 5,
"FL": 6,
"FR": 7,
"BR": 8,
"DB": 9,
"DL": 10,
"DF": 11,
"DR": 12,
// canonical order also
"BU": 1,
"LU": 2,
"FU": 3,
"RU": 4,
"BD": 9 };
var edgeKeys = Object.keys(edges);
function edgeNum(edge) { return edges[edge.toUpperCase()]; }
const centers = {
"U": 0,
"D": 1,
"L": 2,
"R": 3,
"F": 4,
"B": 5 };
var centerKeys = Object.keys(centers);
function centerNum(center) { return centers[center.toUpperCase()]; }
function getPiece(piece, cube) {
if (piece.length == 1) { // center
return centerKeys[cube.v[centerNum(piece)]];
} else {
var pov = "";
for (var i = 0; i < piece.length; i++) {
pov += centerKeys[cube.v[centers[piece[i]]]];
}
pov = canonical(pov);
switch (piece.length) {
case 2: // edge
return edgeKeys[cube.e[edgeNum(pov) - 1].p - 1];
case 3: // corner
return cornerKeys[cube.e[cornerNum(pov) - 1].p - 1];
default: throw "Invalid piece name: " + piece;
}
}
}
function maskPieces(pieces, cube) {
var cs = cube.c.slice(0);
var es = cube.e.slice(0);
var vs = cube.v.slice(0);
var ms = JSON.parse(JSON.stringify(cube.m));
for (var p in pieces) {
ms[canonical(getPiece(pieces[p], cube))] = true;
}
return { c: cs, e: es, v: vs, m: ms };
}
function faces(cube, simple) {
function twistColors(colors, corner, twist) {
var c0 = colors[0];
var c1 = colors[1];
var c2 = colors[2];
var lastToFront = corner == 0 || corner == 2 || corner == 5 || corner == 7;
switch (twist) {
case 1: return lastToFront ? c2 + c0 + c1 : c1 + c2 + c0;
case 2: return lastToFront ? c1 + c2 + c0 : c2 + c0 + c1;
case 3: return colors; // no twist
}
}
var faces = {
// centers (orientation)
_U: centerKeys[cube.v[0]],
_D: centerKeys[cube.v[1]],
_L: centerKeys[cube.v[2]],
_R: centerKeys[cube.v[3]],
_F: centerKeys[cube.v[4]],
_B: centerKeys[cube.v[5]],
// center colors
U: simple && cube.m[centerKeys[cube.v[0]]] ? '.' : centerKeys[cube.v[0]],
D: simple && cube.m[centerKeys[cube.v[1]]] ? '.' : centerKeys[cube.v[1]],
L: simple && cube.m[centerKeys[cube.v[2]]] ? '.' : centerKeys[cube.v[2]],
R: simple && cube.m[centerKeys[cube.v[3]]] ? '.' : centerKeys[cube.v[3]],
F: simple && cube.m[centerKeys[cube.v[4]]] ? '.' : centerKeys[cube.v[4]],
B: simple && cube.m[centerKeys[cube.v[5]]] ? '.' : centerKeys[cube.v[5]],
}
// corners
for (var c = 0; c < 8; c++) {
var p = cube.c[c].p;
if (p != 0) {
var colors = twistColors(cornerKeys[p - 1], c, cube.c[c].o);
var c0 = colors[0];
var c1 = colors[1];
var c2 = colors[2];
var targets = cornerKeys[c].toLowerCase();
var t0 = targets[0];
var t1 = targets[1];
var t2 = targets[2];
var masked = simple && cube.m[canonical(c0 + c1 + c2).toUpperCase()];
faces[canonical(t0.toUpperCase() + t1 + t2)] = masked ? '.' : c0;
faces[canonical(t0 + t1.toUpperCase() + t2)] = masked ? '.' : c1;
faces[canonical(t0 + t1 + t2.toUpperCase())] = masked ? '.' : c2;
}
}
// edges
for (var e = 0; e < 12; e++) {
var p = cube.e[e].p;
if (p != 0) {
var colors = edgeKeys[p - 1];
var flipped = cube.e[e].o == 2;
var c0 = flipped ? colors[1] : colors[0];
var c1 = flipped ? colors[0] : colors[1];
var edge = edgeKeys[e].toLowerCase();
var masked = simple && cube.m[canonical(c0 + c1).toUpperCase()];
faces[canonical(edge[0].toUpperCase() + edge[1])] = masked ? '.' : c0;
faces[canonical(edge[0] + edge[1].toUpperCase())] = masked ? '.' : c1;
}
}
return faces;
}
function faceColor(face, faces) {
if (face.length == 1) return faces[face];
var pov = "";
for (var i = 0; i < face.length; i++) {
var f = face[i].toUpperCase();
var m = faces['_' + f]; // map to pov
pov += (f != face[i] ? m.toLowerCase() : m); // match case
}
return faces[canonical(pov)];
}
function toString(cube) {
var fs = faces(cube);
var str = "";
var order = ["Ubl", "Ub", "Ubr", "Ul", "U", "Ur", "Ufl", "Uf", "Ufr", // U
"uLb", "uL", "ufL", "bL", "L", "fL", "dbL", "dL", "dfL", // L
"uFl", "uF", "uFr", "Fl", "F", "Fr", "dFl", "dF", "dFr", // F
"ufR", "uR", "ubR", "fR", "R", "bR", "dfR", "dR", "dbR", // R
"Dfl", "Df", "Dfr", "Dl", "D", "Dr", "Dbl", "Db", "Dbr", // D
"Bdl", "Bd", "Bdr", "Bl", "B", "Br", "Bul", "Bu", "Bur"] // B
for (var i = 0; i < order.length; i++) {
str += faceColor(order[i], fs);
}
return str;
}
function matchPattern(pattern, cube) {
var state = toString(cube);
var mapping = {};
for (var i in pattern) {
var p = pattern[i];
if (p != '.') {
if (!mapping[p]) {
mapping[p] = state[i];
continue;
}
if (mapping[p] != state[i]) return false;
}
}
return true;
}
return {
solved: solved,
alg: alg,
random: random,
same: same,
faces: faces,
faceColor: faceColor,
toString: toString,
matchPattern: matchPattern,
getPiece: getPiece,
maskPieces: maskPieces
}
}());