cube-state-engine 1.2.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.idea/workspace.xml +304 -0
- package/README.md +2 -1
- package/dist/index.d.mts +110 -92
- package/dist/index.d.ts +110 -92
- package/dist/index.js +98 -92
- package/dist/index.mjs +98 -92
- package/package.json +1 -1
- package/.idea/cube-state-engine.iml +0 -12
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
package/dist/index.d.ts
CHANGED
|
@@ -1,54 +1,43 @@
|
|
|
1
1
|
class CubeEngine {
|
|
2
2
|
MOVES = [];
|
|
3
|
+
size = 3;
|
|
4
|
+
|
|
5
|
+
constructor(initialScramble = "", options = { size: 3 }) {
|
|
6
|
+
const allowedSizes = [2, 3];
|
|
7
|
+
this.size = allowedSizes.includes(options.size) ? options.size : 3;
|
|
8
|
+
|
|
9
|
+
this.#initializeState();
|
|
3
10
|
|
|
4
|
-
constructor(initialScramble = "") {
|
|
5
11
|
// If an initial scramble string is provided, apply it without recording moves
|
|
6
12
|
if (typeof initialScramble === "string" && initialScramble.trim().length > 0) {
|
|
7
13
|
this.#applyMovesFromString(initialScramble, false);
|
|
8
|
-
// Ensure history is empty for initial position
|
|
9
14
|
this.MOVES = [];
|
|
10
15
|
}
|
|
11
16
|
}
|
|
12
17
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
]
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
[COLOR.R[3], COLOR.R[4], COLOR.R[5]],
|
|
37
|
-
[COLOR.R[6], COLOR.R[7], COLOR.R[8]],
|
|
38
|
-
],
|
|
39
|
-
BACK: [
|
|
40
|
-
// (Blue)
|
|
41
|
-
[COLOR.B[0], COLOR.B[1], COLOR.B[2]],
|
|
42
|
-
[COLOR.B[3], COLOR.B[4], COLOR.B[5]],
|
|
43
|
-
[COLOR.B[6], COLOR.B[7], COLOR.B[8]],
|
|
44
|
-
],
|
|
45
|
-
DOWN: [
|
|
46
|
-
// (Yellow)
|
|
47
|
-
[COLOR.Y[0], COLOR.Y[1], COLOR.Y[2]],
|
|
48
|
-
[COLOR.Y[3], COLOR.Y[4], COLOR.Y[5]],
|
|
49
|
-
[COLOR.Y[6], COLOR.Y[7], COLOR.Y[8]],
|
|
50
|
-
],
|
|
51
|
-
};
|
|
18
|
+
#initializeState() {
|
|
19
|
+
this.STATES = {
|
|
20
|
+
UPPER: this.#createFace("W"),
|
|
21
|
+
LEFT: this.#createFace("O"),
|
|
22
|
+
FRONT: this.#createFace("G"),
|
|
23
|
+
RIGHT: this.#createFace("R"),
|
|
24
|
+
BACK: this.#createFace("B"),
|
|
25
|
+
DOWN: this.#createFace("Y"),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Create a face matrix based on cube size
|
|
30
|
+
#createFace(color) {
|
|
31
|
+
const face = [];
|
|
32
|
+
for (let i = 0; i < this.size; i++) {
|
|
33
|
+
const row = [];
|
|
34
|
+
for (let j = 0; j < this.size; j++) {
|
|
35
|
+
row.push(color);
|
|
36
|
+
}
|
|
37
|
+
face.push(row);
|
|
38
|
+
}
|
|
39
|
+
return face;
|
|
40
|
+
}
|
|
52
41
|
|
|
53
42
|
/**
|
|
54
43
|
* Rotates the (UPPER) layer clockwise or counterclockwise.
|
|
@@ -116,6 +105,33 @@ class CubeEngine {
|
|
|
116
105
|
}
|
|
117
106
|
}
|
|
118
107
|
|
|
108
|
+
/**
|
|
109
|
+
* Rotates the (BACK) layer clockwise or counterclockwise.
|
|
110
|
+
*/
|
|
111
|
+
rotateB(clockwise = true) {
|
|
112
|
+
if (clockwise) {
|
|
113
|
+
this.#rotateB(true);
|
|
114
|
+
this.MOVES.push("B");
|
|
115
|
+
} else {
|
|
116
|
+
this.#rotateB(false);
|
|
117
|
+
this.MOVES.push("B'");
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
#rotateB(clockwise = true) {
|
|
122
|
+
// Implement B as y2 F y2
|
|
123
|
+
// Clockwise/counterclockwise direction is preserved through y2 conjugation
|
|
124
|
+
this.#rotateY(true);
|
|
125
|
+
this.#rotateY(true);
|
|
126
|
+
if (clockwise) {
|
|
127
|
+
this.#rotateF(true);
|
|
128
|
+
} else {
|
|
129
|
+
this.#rotateF(false);
|
|
130
|
+
}
|
|
131
|
+
this.#rotateY(false);
|
|
132
|
+
this.#rotateY(false);
|
|
133
|
+
}
|
|
134
|
+
|
|
119
135
|
/**
|
|
120
136
|
* Rotates the (RIGHT) layer clockwise or counterclockwise.
|
|
121
137
|
*/
|
|
@@ -203,6 +219,7 @@ class CubeEngine {
|
|
|
203
219
|
* Rotates the wide (DOWN two layers) clockwise or counterclockwise.
|
|
204
220
|
*/
|
|
205
221
|
rotateDw(clockwise = true) {
|
|
222
|
+
if (this.size === 2) return;
|
|
206
223
|
if (clockwise) {
|
|
207
224
|
this.#rotateDw(true);
|
|
208
225
|
this.MOVES.push("Dw");
|
|
@@ -226,6 +243,7 @@ class CubeEngine {
|
|
|
226
243
|
* Rotates the wide (UPPER two layers) clockwise or counterclockwise.
|
|
227
244
|
*/
|
|
228
245
|
rotateUw(clockwise = true) {
|
|
246
|
+
if (this.size === 2) return;
|
|
229
247
|
if (clockwise) {
|
|
230
248
|
this.#rotateUw(true);
|
|
231
249
|
this.MOVES.push("Uw");
|
|
@@ -249,6 +267,7 @@ class CubeEngine {
|
|
|
249
267
|
* Rotates the wide (RIGHT two layers) clockwise or counterclockwise.
|
|
250
268
|
*/
|
|
251
269
|
rotateRw(clockwise = true) {
|
|
270
|
+
if (this.size === 2) return;
|
|
252
271
|
if (clockwise) {
|
|
253
272
|
this.#rotateRw(true);
|
|
254
273
|
this.MOVES.push("Rw");
|
|
@@ -272,6 +291,7 @@ class CubeEngine {
|
|
|
272
291
|
* Rotates the wide (LEFT two layers) clockwise or counterclockwise.
|
|
273
292
|
*/
|
|
274
293
|
rotateLw(clockwise = true) {
|
|
294
|
+
if (this.size === 2) return;
|
|
275
295
|
if (clockwise) {
|
|
276
296
|
this.#rotateLw(true);
|
|
277
297
|
this.MOVES.push("Lw");
|
|
@@ -296,6 +316,7 @@ class CubeEngine {
|
|
|
296
316
|
* Rotates the middle slice (M) parallel to L/R. Clockwise corresponds to Lw followed by L'.
|
|
297
317
|
*/
|
|
298
318
|
rotateM(clockwise = true) {
|
|
319
|
+
if (this.size === 2) return;
|
|
299
320
|
if (clockwise) {
|
|
300
321
|
this.#rotateM(true);
|
|
301
322
|
this.MOVES.push("M");
|
|
@@ -447,21 +468,42 @@ class CubeEngine {
|
|
|
447
468
|
*/
|
|
448
469
|
#switchMatrix(matrix, clockwise = true) {
|
|
449
470
|
const clone = structuredClone(matrix);
|
|
471
|
+
const size = this.size;
|
|
450
472
|
|
|
451
|
-
|
|
473
|
+
// Flatten the matrix
|
|
474
|
+
let tempMatrix = [];
|
|
475
|
+
for (let i = 0; i < size; i++) {
|
|
476
|
+
tempMatrix = [...tempMatrix, ...clone[i]];
|
|
477
|
+
}
|
|
452
478
|
|
|
453
|
-
if (
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
479
|
+
if (size === 2) {
|
|
480
|
+
// For 2x2 cubes
|
|
481
|
+
if (clockwise) {
|
|
482
|
+
return [
|
|
483
|
+
[tempMatrix[2], tempMatrix[0]],
|
|
484
|
+
[tempMatrix[3], tempMatrix[1]]
|
|
485
|
+
];
|
|
486
|
+
} else {
|
|
487
|
+
return [
|
|
488
|
+
[tempMatrix[1], tempMatrix[3]],
|
|
489
|
+
[tempMatrix[0], tempMatrix[2]]
|
|
490
|
+
];
|
|
491
|
+
}
|
|
459
492
|
} else {
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
493
|
+
// For 3x3 cubes (original logic)
|
|
494
|
+
if (clockwise) {
|
|
495
|
+
return [
|
|
496
|
+
[tempMatrix[6], tempMatrix[3], tempMatrix[0]],
|
|
497
|
+
[tempMatrix[7], tempMatrix[4], tempMatrix[1]],
|
|
498
|
+
[tempMatrix[8], tempMatrix[5], tempMatrix[2]],
|
|
499
|
+
];
|
|
500
|
+
} else {
|
|
501
|
+
return [
|
|
502
|
+
[tempMatrix[2], tempMatrix[5], tempMatrix[8]],
|
|
503
|
+
[tempMatrix[1], tempMatrix[4], tempMatrix[7]],
|
|
504
|
+
[tempMatrix[0], tempMatrix[3], tempMatrix[6]],
|
|
505
|
+
];
|
|
506
|
+
}
|
|
465
507
|
}
|
|
466
508
|
}
|
|
467
509
|
|
|
@@ -489,13 +531,15 @@ class CubeEngine {
|
|
|
489
531
|
};
|
|
490
532
|
|
|
491
533
|
const layersSolved = Object.keys(temp).map((layer) => {
|
|
492
|
-
const mixedMatrix = [
|
|
493
|
-
...temp[layer][0],
|
|
494
|
-
...temp[layer][1],
|
|
495
|
-
...temp[layer][2],
|
|
496
|
-
];
|
|
497
534
|
|
|
498
|
-
|
|
535
|
+
let mixedMatrix = [];
|
|
536
|
+
for (let i = 0; i < this.size; i++) {
|
|
537
|
+
mixedMatrix = [...mixedMatrix, ...temp[layer][i]];
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
// For a 2x2 cube, there is no center; we use the first color as reference
|
|
541
|
+
// For a 3x3 cube, we use the color of the center (position 4)
|
|
542
|
+
const centerColor = this.size === 2 ? mixedMatrix[0] : mixedMatrix[4];
|
|
499
543
|
|
|
500
544
|
return mixedMatrix.every((currentColor) => currentColor === centerColor);
|
|
501
545
|
});
|
|
@@ -517,38 +561,7 @@ class CubeEngine {
|
|
|
517
561
|
* Resets the cube to the solved state and clears the move history.
|
|
518
562
|
*/
|
|
519
563
|
reset() {
|
|
520
|
-
this
|
|
521
|
-
UPPER: [
|
|
522
|
-
[COLOR.W[0], COLOR.W[1], COLOR.W[2]],
|
|
523
|
-
[COLOR.W[3], COLOR.W[4], COLOR.W[5]],
|
|
524
|
-
[COLOR.W[6], COLOR.W[7], COLOR.W[8]],
|
|
525
|
-
],
|
|
526
|
-
LEFT: [
|
|
527
|
-
[COLOR.O[0], COLOR.O[1], COLOR.O[2]],
|
|
528
|
-
[COLOR.O[3], COLOR.O[4], COLOR.O[5]],
|
|
529
|
-
[COLOR.O[6], COLOR.O[7], COLOR.O[8]],
|
|
530
|
-
],
|
|
531
|
-
FRONT: [
|
|
532
|
-
[COLOR.G[0], COLOR.G[1], COLOR.G[2]],
|
|
533
|
-
[COLOR.G[3], COLOR.G[4], COLOR.G[5]],
|
|
534
|
-
[COLOR.G[6], COLOR.G[7], COLOR.G[8]],
|
|
535
|
-
],
|
|
536
|
-
RIGHT: [
|
|
537
|
-
[COLOR.R[0], COLOR.R[1], COLOR.R[2]],
|
|
538
|
-
[COLOR.R[3], COLOR.R[4], COLOR.R[5]],
|
|
539
|
-
[COLOR.R[6], COLOR.R[7], COLOR.R[8]],
|
|
540
|
-
],
|
|
541
|
-
BACK: [
|
|
542
|
-
[COLOR.B[0], COLOR.B[1], COLOR.B[2]],
|
|
543
|
-
[COLOR.B[3], COLOR.B[4], COLOR.B[5]],
|
|
544
|
-
[COLOR.B[6], COLOR.B[7], COLOR.B[8]],
|
|
545
|
-
],
|
|
546
|
-
DOWN: [
|
|
547
|
-
[COLOR.Y[0], COLOR.Y[1], COLOR.Y[2]],
|
|
548
|
-
[COLOR.Y[3], COLOR.Y[4], COLOR.Y[5]],
|
|
549
|
-
[COLOR.Y[6], COLOR.Y[7], COLOR.Y[8]],
|
|
550
|
-
],
|
|
551
|
-
};
|
|
564
|
+
this.#initializeState();
|
|
552
565
|
this.MOVES = [];
|
|
553
566
|
}
|
|
554
567
|
|
|
@@ -579,7 +592,6 @@ class CubeEngine {
|
|
|
579
592
|
|
|
580
593
|
const exec = (fnClockwise, fnCounter) => {
|
|
581
594
|
if (isDouble) {
|
|
582
|
-
// Double turns ignore prime; do two clockwise quarter-turns
|
|
583
595
|
fnClockwise();
|
|
584
596
|
fnClockwise();
|
|
585
597
|
} else {
|
|
@@ -662,6 +674,12 @@ class CubeEngine {
|
|
|
662
674
|
() => (record ? this.rotateF(false) : this.#rotateF(false))
|
|
663
675
|
);
|
|
664
676
|
break;
|
|
677
|
+
case 'B':
|
|
678
|
+
exec(
|
|
679
|
+
() => (record ? this.rotateB(true) : this.#rotateB(true)),
|
|
680
|
+
() => (record ? this.rotateB(false) : this.#rotateB(false))
|
|
681
|
+
);
|
|
682
|
+
break;
|
|
665
683
|
case 'x':
|
|
666
684
|
exec(
|
|
667
685
|
() => (record ? this.rotateX(true) : this.#rotateX(true)),
|
package/dist/index.js
CHANGED
|
@@ -45,50 +45,15 @@ __export(src_exports, {
|
|
|
45
45
|
CubeEngine: () => CubeEngine
|
|
46
46
|
});
|
|
47
47
|
module.exports = __toCommonJS(src_exports);
|
|
48
|
-
var _CubeEngine_instances, rotateU_fn, rotateF_fn, rotateR_fn, rotateL_fn, rotateD_fn, rotateDw_fn, rotateUw_fn, rotateRw_fn, rotateLw_fn, rotateM_fn, rotateX_fn, rotateZ_fn, rotateY_fn, switchMatrix_fn, specialFlip_fn, applyMovesFromString_fn;
|
|
48
|
+
var _CubeEngine_instances, initializeState_fn, createFace_fn, rotateU_fn, rotateF_fn, rotateB_fn, rotateR_fn, rotateL_fn, rotateD_fn, rotateDw_fn, rotateUw_fn, rotateRw_fn, rotateLw_fn, rotateM_fn, rotateX_fn, rotateZ_fn, rotateY_fn, switchMatrix_fn, specialFlip_fn, applyMovesFromString_fn;
|
|
49
49
|
var CubeEngine = class {
|
|
50
|
-
constructor(initialScramble = "") {
|
|
50
|
+
constructor(initialScramble = "", options = { size: 3 }) {
|
|
51
51
|
__privateAdd(this, _CubeEngine_instances);
|
|
52
52
|
__publicField(this, "MOVES", []);
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
[COLOR.W[0], COLOR.W[1], COLOR.W[2]],
|
|
58
|
-
[COLOR.W[3], COLOR.W[4], COLOR.W[5]],
|
|
59
|
-
[COLOR.W[6], COLOR.W[7], COLOR.W[8]]
|
|
60
|
-
],
|
|
61
|
-
LEFT: [
|
|
62
|
-
// (Orange)
|
|
63
|
-
[COLOR.O[0], COLOR.O[1], COLOR.O[2]],
|
|
64
|
-
[COLOR.O[3], COLOR.O[4], COLOR.O[5]],
|
|
65
|
-
[COLOR.O[6], COLOR.O[7], COLOR.O[8]]
|
|
66
|
-
],
|
|
67
|
-
FRONT: [
|
|
68
|
-
// (Green)
|
|
69
|
-
[COLOR.G[0], COLOR.G[1], COLOR.G[2]],
|
|
70
|
-
[COLOR.G[3], COLOR.G[4], COLOR.G[5]],
|
|
71
|
-
[COLOR.G[6], COLOR.G[7], COLOR.G[8]]
|
|
72
|
-
],
|
|
73
|
-
RIGHT: [
|
|
74
|
-
// (Red)
|
|
75
|
-
[COLOR.R[0], COLOR.R[1], COLOR.R[2]],
|
|
76
|
-
[COLOR.R[3], COLOR.R[4], COLOR.R[5]],
|
|
77
|
-
[COLOR.R[6], COLOR.R[7], COLOR.R[8]]
|
|
78
|
-
],
|
|
79
|
-
BACK: [
|
|
80
|
-
// (Blue)
|
|
81
|
-
[COLOR.B[0], COLOR.B[1], COLOR.B[2]],
|
|
82
|
-
[COLOR.B[3], COLOR.B[4], COLOR.B[5]],
|
|
83
|
-
[COLOR.B[6], COLOR.B[7], COLOR.B[8]]
|
|
84
|
-
],
|
|
85
|
-
DOWN: [
|
|
86
|
-
// (Yellow)
|
|
87
|
-
[COLOR.Y[0], COLOR.Y[1], COLOR.Y[2]],
|
|
88
|
-
[COLOR.Y[3], COLOR.Y[4], COLOR.Y[5]],
|
|
89
|
-
[COLOR.Y[6], COLOR.Y[7], COLOR.Y[8]]
|
|
90
|
-
]
|
|
91
|
-
});
|
|
53
|
+
__publicField(this, "size", 3);
|
|
54
|
+
const allowedSizes = [2, 3];
|
|
55
|
+
this.size = allowedSizes.includes(options.size) ? options.size : 3;
|
|
56
|
+
__privateMethod(this, _CubeEngine_instances, initializeState_fn).call(this);
|
|
92
57
|
if (typeof initialScramble === "string" && initialScramble.trim().length > 0) {
|
|
93
58
|
__privateMethod(this, _CubeEngine_instances, applyMovesFromString_fn).call(this, initialScramble, false);
|
|
94
59
|
this.MOVES = [];
|
|
@@ -118,6 +83,18 @@ var CubeEngine = class {
|
|
|
118
83
|
this.MOVES.push("F'");
|
|
119
84
|
}
|
|
120
85
|
}
|
|
86
|
+
/**
|
|
87
|
+
* Rotates the (BACK) layer clockwise or counterclockwise.
|
|
88
|
+
*/
|
|
89
|
+
rotateB(clockwise = true) {
|
|
90
|
+
if (clockwise) {
|
|
91
|
+
__privateMethod(this, _CubeEngine_instances, rotateB_fn).call(this, true);
|
|
92
|
+
this.MOVES.push("B");
|
|
93
|
+
} else {
|
|
94
|
+
__privateMethod(this, _CubeEngine_instances, rotateB_fn).call(this, false);
|
|
95
|
+
this.MOVES.push("B'");
|
|
96
|
+
}
|
|
97
|
+
}
|
|
121
98
|
/**
|
|
122
99
|
* Rotates the (RIGHT) layer clockwise or counterclockwise.
|
|
123
100
|
*/
|
|
@@ -158,6 +135,7 @@ var CubeEngine = class {
|
|
|
158
135
|
* Rotates the wide (DOWN two layers) clockwise or counterclockwise.
|
|
159
136
|
*/
|
|
160
137
|
rotateDw(clockwise = true) {
|
|
138
|
+
if (this.size === 2) return;
|
|
161
139
|
if (clockwise) {
|
|
162
140
|
__privateMethod(this, _CubeEngine_instances, rotateDw_fn).call(this, true);
|
|
163
141
|
this.MOVES.push("Dw");
|
|
@@ -170,6 +148,7 @@ var CubeEngine = class {
|
|
|
170
148
|
* Rotates the wide (UPPER two layers) clockwise or counterclockwise.
|
|
171
149
|
*/
|
|
172
150
|
rotateUw(clockwise = true) {
|
|
151
|
+
if (this.size === 2) return;
|
|
173
152
|
if (clockwise) {
|
|
174
153
|
__privateMethod(this, _CubeEngine_instances, rotateUw_fn).call(this, true);
|
|
175
154
|
this.MOVES.push("Uw");
|
|
@@ -182,6 +161,7 @@ var CubeEngine = class {
|
|
|
182
161
|
* Rotates the wide (RIGHT two layers) clockwise or counterclockwise.
|
|
183
162
|
*/
|
|
184
163
|
rotateRw(clockwise = true) {
|
|
164
|
+
if (this.size === 2) return;
|
|
185
165
|
if (clockwise) {
|
|
186
166
|
__privateMethod(this, _CubeEngine_instances, rotateRw_fn).call(this, true);
|
|
187
167
|
this.MOVES.push("Rw");
|
|
@@ -194,6 +174,7 @@ var CubeEngine = class {
|
|
|
194
174
|
* Rotates the wide (LEFT two layers) clockwise or counterclockwise.
|
|
195
175
|
*/
|
|
196
176
|
rotateLw(clockwise = true) {
|
|
177
|
+
if (this.size === 2) return;
|
|
197
178
|
if (clockwise) {
|
|
198
179
|
__privateMethod(this, _CubeEngine_instances, rotateLw_fn).call(this, true);
|
|
199
180
|
this.MOVES.push("Lw");
|
|
@@ -206,6 +187,7 @@ var CubeEngine = class {
|
|
|
206
187
|
* Rotates the middle slice (M) parallel to L/R. Clockwise corresponds to Lw followed by L'.
|
|
207
188
|
*/
|
|
208
189
|
rotateM(clockwise = true) {
|
|
190
|
+
if (this.size === 2) return;
|
|
209
191
|
if (clockwise) {
|
|
210
192
|
__privateMethod(this, _CubeEngine_instances, rotateM_fn).call(this, true);
|
|
211
193
|
this.MOVES.push("M");
|
|
@@ -262,12 +244,11 @@ var CubeEngine = class {
|
|
|
262
244
|
isSolved() {
|
|
263
245
|
const temp = __spreadValues({}, this.STATES);
|
|
264
246
|
const layersSolved = Object.keys(temp).map((layer) => {
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
...temp[layer][
|
|
268
|
-
|
|
269
|
-
];
|
|
270
|
-
const centerColor = mixedMatrix[4];
|
|
247
|
+
let mixedMatrix = [];
|
|
248
|
+
for (let i = 0; i < this.size; i++) {
|
|
249
|
+
mixedMatrix = [...mixedMatrix, ...temp[layer][i]];
|
|
250
|
+
}
|
|
251
|
+
const centerColor = this.size === 2 ? mixedMatrix[0] : mixedMatrix[4];
|
|
271
252
|
return mixedMatrix.every((currentColor) => currentColor === centerColor);
|
|
272
253
|
});
|
|
273
254
|
return layersSolved.every((isLayerSolved) => isLayerSolved);
|
|
@@ -285,38 +266,7 @@ var CubeEngine = class {
|
|
|
285
266
|
* Resets the cube to the solved state and clears the move history.
|
|
286
267
|
*/
|
|
287
268
|
reset() {
|
|
288
|
-
this
|
|
289
|
-
UPPER: [
|
|
290
|
-
[COLOR.W[0], COLOR.W[1], COLOR.W[2]],
|
|
291
|
-
[COLOR.W[3], COLOR.W[4], COLOR.W[5]],
|
|
292
|
-
[COLOR.W[6], COLOR.W[7], COLOR.W[8]]
|
|
293
|
-
],
|
|
294
|
-
LEFT: [
|
|
295
|
-
[COLOR.O[0], COLOR.O[1], COLOR.O[2]],
|
|
296
|
-
[COLOR.O[3], COLOR.O[4], COLOR.O[5]],
|
|
297
|
-
[COLOR.O[6], COLOR.O[7], COLOR.O[8]]
|
|
298
|
-
],
|
|
299
|
-
FRONT: [
|
|
300
|
-
[COLOR.G[0], COLOR.G[1], COLOR.G[2]],
|
|
301
|
-
[COLOR.G[3], COLOR.G[4], COLOR.G[5]],
|
|
302
|
-
[COLOR.G[6], COLOR.G[7], COLOR.G[8]]
|
|
303
|
-
],
|
|
304
|
-
RIGHT: [
|
|
305
|
-
[COLOR.R[0], COLOR.R[1], COLOR.R[2]],
|
|
306
|
-
[COLOR.R[3], COLOR.R[4], COLOR.R[5]],
|
|
307
|
-
[COLOR.R[6], COLOR.R[7], COLOR.R[8]]
|
|
308
|
-
],
|
|
309
|
-
BACK: [
|
|
310
|
-
[COLOR.B[0], COLOR.B[1], COLOR.B[2]],
|
|
311
|
-
[COLOR.B[3], COLOR.B[4], COLOR.B[5]],
|
|
312
|
-
[COLOR.B[6], COLOR.B[7], COLOR.B[8]]
|
|
313
|
-
],
|
|
314
|
-
DOWN: [
|
|
315
|
-
[COLOR.Y[0], COLOR.Y[1], COLOR.Y[2]],
|
|
316
|
-
[COLOR.Y[3], COLOR.Y[4], COLOR.Y[5]],
|
|
317
|
-
[COLOR.Y[6], COLOR.Y[7], COLOR.Y[8]]
|
|
318
|
-
]
|
|
319
|
-
};
|
|
269
|
+
__privateMethod(this, _CubeEngine_instances, initializeState_fn).call(this);
|
|
320
270
|
this.MOVES = [];
|
|
321
271
|
}
|
|
322
272
|
/**
|
|
@@ -331,6 +281,28 @@ var CubeEngine = class {
|
|
|
331
281
|
}
|
|
332
282
|
};
|
|
333
283
|
_CubeEngine_instances = new WeakSet();
|
|
284
|
+
initializeState_fn = function() {
|
|
285
|
+
this.STATES = {
|
|
286
|
+
UPPER: __privateMethod(this, _CubeEngine_instances, createFace_fn).call(this, "W"),
|
|
287
|
+
LEFT: __privateMethod(this, _CubeEngine_instances, createFace_fn).call(this, "O"),
|
|
288
|
+
FRONT: __privateMethod(this, _CubeEngine_instances, createFace_fn).call(this, "G"),
|
|
289
|
+
RIGHT: __privateMethod(this, _CubeEngine_instances, createFace_fn).call(this, "R"),
|
|
290
|
+
BACK: __privateMethod(this, _CubeEngine_instances, createFace_fn).call(this, "B"),
|
|
291
|
+
DOWN: __privateMethod(this, _CubeEngine_instances, createFace_fn).call(this, "Y")
|
|
292
|
+
};
|
|
293
|
+
};
|
|
294
|
+
// Create a face matrix based on cube size
|
|
295
|
+
createFace_fn = function(color) {
|
|
296
|
+
const face = [];
|
|
297
|
+
for (let i = 0; i < this.size; i++) {
|
|
298
|
+
const row = [];
|
|
299
|
+
for (let j = 0; j < this.size; j++) {
|
|
300
|
+
row.push(color);
|
|
301
|
+
}
|
|
302
|
+
face.push(row);
|
|
303
|
+
}
|
|
304
|
+
return face;
|
|
305
|
+
};
|
|
334
306
|
rotateU_fn = function(clockwise = true) {
|
|
335
307
|
if (clockwise) {
|
|
336
308
|
this.STATES.UPPER = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, this.STATES.UPPER, true);
|
|
@@ -365,6 +337,17 @@ rotateF_fn = function(clockwise = true) {
|
|
|
365
337
|
__privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, false);
|
|
366
338
|
}
|
|
367
339
|
};
|
|
340
|
+
rotateB_fn = function(clockwise = true) {
|
|
341
|
+
__privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, true);
|
|
342
|
+
__privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, true);
|
|
343
|
+
if (clockwise) {
|
|
344
|
+
__privateMethod(this, _CubeEngine_instances, rotateF_fn).call(this, true);
|
|
345
|
+
} else {
|
|
346
|
+
__privateMethod(this, _CubeEngine_instances, rotateF_fn).call(this, false);
|
|
347
|
+
}
|
|
348
|
+
__privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, false);
|
|
349
|
+
__privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, false);
|
|
350
|
+
};
|
|
368
351
|
rotateR_fn = function(clockwise = true) {
|
|
369
352
|
if (clockwise) {
|
|
370
353
|
__privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, true);
|
|
@@ -523,19 +506,37 @@ rotateY_fn = function(clockwise = true) {
|
|
|
523
506
|
*/
|
|
524
507
|
switchMatrix_fn = function(matrix, clockwise = true) {
|
|
525
508
|
const clone = structuredClone(matrix);
|
|
526
|
-
const
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
509
|
+
const size = this.size;
|
|
510
|
+
let tempMatrix = [];
|
|
511
|
+
for (let i = 0; i < size; i++) {
|
|
512
|
+
tempMatrix = [...tempMatrix, ...clone[i]];
|
|
513
|
+
}
|
|
514
|
+
if (size === 2) {
|
|
515
|
+
if (clockwise) {
|
|
516
|
+
return [
|
|
517
|
+
[tempMatrix[2], tempMatrix[0]],
|
|
518
|
+
[tempMatrix[3], tempMatrix[1]]
|
|
519
|
+
];
|
|
520
|
+
} else {
|
|
521
|
+
return [
|
|
522
|
+
[tempMatrix[1], tempMatrix[3]],
|
|
523
|
+
[tempMatrix[0], tempMatrix[2]]
|
|
524
|
+
];
|
|
525
|
+
}
|
|
533
526
|
} else {
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
527
|
+
if (clockwise) {
|
|
528
|
+
return [
|
|
529
|
+
[tempMatrix[6], tempMatrix[3], tempMatrix[0]],
|
|
530
|
+
[tempMatrix[7], tempMatrix[4], tempMatrix[1]],
|
|
531
|
+
[tempMatrix[8], tempMatrix[5], tempMatrix[2]]
|
|
532
|
+
];
|
|
533
|
+
} else {
|
|
534
|
+
return [
|
|
535
|
+
[tempMatrix[2], tempMatrix[5], tempMatrix[8]],
|
|
536
|
+
[tempMatrix[1], tempMatrix[4], tempMatrix[7]],
|
|
537
|
+
[tempMatrix[0], tempMatrix[3], tempMatrix[6]]
|
|
538
|
+
];
|
|
539
|
+
}
|
|
539
540
|
}
|
|
540
541
|
};
|
|
541
542
|
specialFlip_fn = function(matrix) {
|
|
@@ -550,7 +551,6 @@ applyMovesFromString_fn = function(sequence, record = true) {
|
|
|
550
551
|
const rest = token.slice(1);
|
|
551
552
|
const isDouble = rest.includes("2");
|
|
552
553
|
const isPrime = rest.includes("'");
|
|
553
|
-
const times = isDouble ? 2 : 1;
|
|
554
554
|
const exec = (fnClockwise, fnCounter) => {
|
|
555
555
|
if (isDouble) {
|
|
556
556
|
fnClockwise();
|
|
@@ -634,6 +634,12 @@ applyMovesFromString_fn = function(sequence, record = true) {
|
|
|
634
634
|
() => record ? this.rotateF(false) : __privateMethod(this, _CubeEngine_instances, rotateF_fn).call(this, false)
|
|
635
635
|
);
|
|
636
636
|
break;
|
|
637
|
+
case "B":
|
|
638
|
+
exec(
|
|
639
|
+
() => record ? this.rotateB(true) : __privateMethod(this, _CubeEngine_instances, rotateB_fn).call(this, true),
|
|
640
|
+
() => record ? this.rotateB(false) : __privateMethod(this, _CubeEngine_instances, rotateB_fn).call(this, false)
|
|
641
|
+
);
|
|
642
|
+
break;
|
|
637
643
|
case "x":
|
|
638
644
|
exec(
|
|
639
645
|
() => record ? this.rotateX(true) : __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, true),
|