cube-state-engine 1.0.2 → 1.0.4
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/{src/index.js → dist/index.d.mts} +133 -51
- package/dist/index.d.ts +357 -0
- package/dist/index.js +364 -0
- package/dist/index.mjs +341 -0
- package/package.json +10 -4
- package/babel.config.js +0 -3
- package/index.html +0 -107
- package/jest.config.js +0 -198
- package/main.js +0 -62
- package/styles.css +0 -3
- package/test/cube-engine.test.js +0 -649
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
class CubeEngine {
|
|
2
|
+
MOVES = [];
|
|
3
|
+
|
|
2
4
|
// States object for the rotation
|
|
3
5
|
STATES = {
|
|
4
6
|
UPPER: [
|
|
@@ -40,11 +42,20 @@ export class CubeEngine {
|
|
|
40
42
|
};
|
|
41
43
|
|
|
42
44
|
/**
|
|
43
|
-
* Rotates the
|
|
45
|
+
* Rotates the (UPPER) layer clockwise or counterclockwise.
|
|
44
46
|
*/
|
|
45
47
|
rotateU(clockwise = true) {
|
|
46
48
|
if (clockwise) {
|
|
47
|
-
|
|
49
|
+
this.#rotateU(true);
|
|
50
|
+
this.MOVES.push("U");
|
|
51
|
+
} else {
|
|
52
|
+
this.#rotateU(false);
|
|
53
|
+
this.MOVES.push("U'");
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
#rotateU(clockwise = true) {
|
|
58
|
+
if (clockwise) {
|
|
48
59
|
this.STATES.UPPER = this.#switchMatrix(this.STATES.UPPER, true);
|
|
49
60
|
|
|
50
61
|
const tempFront = [...this.STATES.FRONT[0]];
|
|
@@ -57,7 +68,6 @@ export class CubeEngine {
|
|
|
57
68
|
this.STATES.BACK[0] = [...tempLeft];
|
|
58
69
|
this.STATES.RIGHT[0] = [...tempBack];
|
|
59
70
|
} else {
|
|
60
|
-
// Rotate the top layer (UPPER) counterclockwise
|
|
61
71
|
this.STATES.UPPER = this.#switchMatrix(this.STATES.UPPER, false);
|
|
62
72
|
|
|
63
73
|
const tempFront = [...this.STATES.FRONT[0]];
|
|
@@ -73,61 +83,110 @@ export class CubeEngine {
|
|
|
73
83
|
}
|
|
74
84
|
|
|
75
85
|
/**
|
|
76
|
-
* Rotates the
|
|
86
|
+
* Rotates the (FRONT) layer clockwise or counterclockwise.
|
|
77
87
|
*/
|
|
78
88
|
rotateF(clockwise = true) {
|
|
79
89
|
if (clockwise) {
|
|
80
|
-
this
|
|
81
|
-
this.
|
|
82
|
-
this.rotateX(false);
|
|
90
|
+
this.#rotateF(true);
|
|
91
|
+
this.MOVES.push("F");
|
|
83
92
|
} else {
|
|
84
|
-
this
|
|
85
|
-
this.
|
|
86
|
-
this.rotateX(false);
|
|
93
|
+
this.#rotateF(false);
|
|
94
|
+
this.MOVES.push("F'");
|
|
87
95
|
}
|
|
88
96
|
}
|
|
89
97
|
|
|
98
|
+
#rotateF(clockwise = true) {
|
|
99
|
+
if (clockwise) {
|
|
100
|
+
this.#rotateX(true);
|
|
101
|
+
this.#rotateU(true);
|
|
102
|
+
this.#rotateX(false);
|
|
103
|
+
} else {
|
|
104
|
+
this.#rotateX(true);
|
|
105
|
+
this.#rotateU(false);
|
|
106
|
+
this.#rotateX(false);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Rotates the (RIGHT) layer clockwise or counterclockwise.
|
|
112
|
+
*/
|
|
90
113
|
rotateR(clockwise = true) {
|
|
91
114
|
if (clockwise) {
|
|
92
|
-
this
|
|
93
|
-
this.
|
|
94
|
-
|
|
95
|
-
this
|
|
96
|
-
this.
|
|
115
|
+
this.#rotateR(true);
|
|
116
|
+
this.MOVES.push("R");
|
|
117
|
+
} else {
|
|
118
|
+
this.#rotateR(false);
|
|
119
|
+
this.MOVES.push("R'");
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
#rotateR(clockwise = true) {
|
|
124
|
+
if (clockwise) {
|
|
125
|
+
this.#rotateY(true);
|
|
126
|
+
this.#rotateX(true);
|
|
127
|
+
this.#rotateU(true);
|
|
128
|
+
this.#rotateX(false);
|
|
129
|
+
this.#rotateY(false);
|
|
97
130
|
} else {
|
|
98
|
-
this
|
|
99
|
-
this
|
|
100
|
-
this
|
|
101
|
-
this
|
|
102
|
-
this
|
|
131
|
+
this.#rotateY(true);
|
|
132
|
+
this.#rotateX(true);
|
|
133
|
+
this.#rotateU(false);
|
|
134
|
+
this.#rotateX(false);
|
|
135
|
+
this.#rotateY(false);
|
|
103
136
|
}
|
|
104
137
|
}
|
|
105
138
|
|
|
139
|
+
/**
|
|
140
|
+
* Rotates the (LEFT) layer clockwise or counterclockwise.
|
|
141
|
+
*/
|
|
106
142
|
rotateL(clockwise = true) {
|
|
107
143
|
if (clockwise) {
|
|
108
|
-
this
|
|
109
|
-
this.
|
|
110
|
-
|
|
111
|
-
this
|
|
112
|
-
this.
|
|
144
|
+
this.#rotateL(true);
|
|
145
|
+
this.MOVES.push("L");
|
|
146
|
+
} else {
|
|
147
|
+
this.#rotateL(false);
|
|
148
|
+
this.MOVES.push("L'");
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
#rotateL(clockwise = true) {
|
|
153
|
+
if (clockwise) {
|
|
154
|
+
this.#rotateY(false);
|
|
155
|
+
this.#rotateX(true);
|
|
156
|
+
this.#rotateU(true);
|
|
157
|
+
this.#rotateX(false);
|
|
158
|
+
this.#rotateY(true);
|
|
113
159
|
} else {
|
|
114
|
-
this
|
|
115
|
-
this
|
|
116
|
-
this
|
|
117
|
-
this
|
|
118
|
-
this
|
|
160
|
+
this.#rotateY(false);
|
|
161
|
+
this.#rotateX(true);
|
|
162
|
+
this.#rotateU(false);
|
|
163
|
+
this.#rotateX(false);
|
|
164
|
+
this.#rotateY(true);
|
|
119
165
|
}
|
|
120
166
|
}
|
|
121
167
|
|
|
168
|
+
/**
|
|
169
|
+
* Rotates the (DOWN) layer clockwise or counterclockwise.
|
|
170
|
+
*/
|
|
122
171
|
rotateD(clockwise = true) {
|
|
123
172
|
if (clockwise) {
|
|
124
|
-
this
|
|
125
|
-
this.
|
|
126
|
-
|
|
173
|
+
this.#rotateD(true);
|
|
174
|
+
this.MOVES.push("D");
|
|
175
|
+
} else {
|
|
176
|
+
this.#rotateD(false);
|
|
177
|
+
this.MOVES.push("D'");
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
#rotateD(clockwise = true) {
|
|
182
|
+
if (clockwise) {
|
|
183
|
+
this.#rotateX(true);
|
|
184
|
+
this.#rotateF(true);
|
|
185
|
+
this.#rotateX(false);
|
|
127
186
|
} else {
|
|
128
|
-
this
|
|
129
|
-
this
|
|
130
|
-
this
|
|
187
|
+
this.#rotateX(true);
|
|
188
|
+
this.#rotateF(false);
|
|
189
|
+
this.#rotateX(false);
|
|
131
190
|
}
|
|
132
191
|
}
|
|
133
192
|
|
|
@@ -135,6 +194,16 @@ export class CubeEngine {
|
|
|
135
194
|
* Rotates the (x) axis clockwise or counterclockwise.
|
|
136
195
|
*/
|
|
137
196
|
rotateX(clockwise = true) {
|
|
197
|
+
if (clockwise) {
|
|
198
|
+
this.#rotateX(true);
|
|
199
|
+
this.MOVES.push("x");
|
|
200
|
+
} else {
|
|
201
|
+
this.#rotateX(false);
|
|
202
|
+
this.MOVES.push("x'");
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
#rotateX(clockwise = true) {
|
|
138
207
|
const tempFront = structuredClone(this.STATES.FRONT);
|
|
139
208
|
const tempDown = structuredClone(this.STATES.DOWN);
|
|
140
209
|
const tempUpper = structuredClone(this.STATES.UPPER);
|
|
@@ -143,27 +212,24 @@ export class CubeEngine {
|
|
|
143
212
|
const tempRight = structuredClone(this.STATES.RIGHT);
|
|
144
213
|
|
|
145
214
|
if (clockwise) {
|
|
146
|
-
//
|
|
215
|
+
// Balance the rotation
|
|
147
216
|
this.STATES.LEFT = this.#switchMatrix(tempLeft, false);
|
|
148
217
|
this.STATES.RIGHT = this.#switchMatrix(tempRight, true);
|
|
149
218
|
|
|
150
|
-
//
|
|
219
|
+
// Rotate mid X axis
|
|
151
220
|
this.STATES.FRONT = [...tempDown];
|
|
152
221
|
this.STATES.UPPER = [...tempFront];
|
|
153
222
|
|
|
154
|
-
// Special permutation
|
|
223
|
+
// Special permutation (BACK view elements)
|
|
155
224
|
this.STATES.BACK = this.#specialFlip(tempUpper);
|
|
156
225
|
this.STATES.DOWN = this.#specialFlip(tempBack);
|
|
157
226
|
} else {
|
|
158
|
-
// Rotate the RIGHT and LEFT layers
|
|
159
227
|
this.STATES.LEFT = this.#switchMatrix(tempLeft, true);
|
|
160
228
|
this.STATES.RIGHT = this.#switchMatrix(tempRight, false);
|
|
161
229
|
|
|
162
|
-
// Rotation X axis
|
|
163
230
|
this.STATES.FRONT = [...tempUpper];
|
|
164
231
|
this.STATES.DOWN = [...tempFront];
|
|
165
232
|
|
|
166
|
-
// Special permutation
|
|
167
233
|
this.STATES.BACK = this.#specialFlip(tempDown);
|
|
168
234
|
this.STATES.UPPER = this.#specialFlip(tempBack);
|
|
169
235
|
}
|
|
@@ -173,6 +239,16 @@ export class CubeEngine {
|
|
|
173
239
|
* Rotates the (y) axis clockwise or counterclockwise.
|
|
174
240
|
*/
|
|
175
241
|
rotateY(clockwise = true) {
|
|
242
|
+
if (clockwise) {
|
|
243
|
+
this.#rotateY(true);
|
|
244
|
+
this.MOVES.push("y");
|
|
245
|
+
} else {
|
|
246
|
+
this.#rotateY(false);
|
|
247
|
+
this.MOVES.push("y'");
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
#rotateY(clockwise = true) {
|
|
176
252
|
const tempFront = structuredClone(this.STATES.FRONT);
|
|
177
253
|
const tempRight = structuredClone(this.STATES.RIGHT);
|
|
178
254
|
const tempBack = structuredClone(this.STATES.BACK);
|
|
@@ -182,7 +258,6 @@ export class CubeEngine {
|
|
|
182
258
|
this.STATES.UPPER = this.#switchMatrix(this.STATES.UPPER, true);
|
|
183
259
|
this.STATES.DOWN = this.#switchMatrix(this.STATES.DOWN, false);
|
|
184
260
|
|
|
185
|
-
// Rotation X axis
|
|
186
261
|
this.STATES.FRONT = [...tempRight];
|
|
187
262
|
this.STATES.RIGHT = [...tempBack];
|
|
188
263
|
this.STATES.LEFT = [...tempFront];
|
|
@@ -191,7 +266,6 @@ export class CubeEngine {
|
|
|
191
266
|
this.STATES.UPPER = this.#switchMatrix(this.STATES.UPPER, false);
|
|
192
267
|
this.STATES.DOWN = this.#switchMatrix(this.STATES.DOWN, true);
|
|
193
268
|
|
|
194
|
-
// Rotation X axis
|
|
195
269
|
this.STATES.FRONT = [...tempLeft];
|
|
196
270
|
this.STATES.RIGHT = [...tempFront];
|
|
197
271
|
this.STATES.LEFT = [...tempBack];
|
|
@@ -232,10 +306,6 @@ export class CubeEngine {
|
|
|
232
306
|
* Logs the current state of the cube.
|
|
233
307
|
*/
|
|
234
308
|
state() {
|
|
235
|
-
console.clear();
|
|
236
|
-
console.log({
|
|
237
|
-
...this.STATES,
|
|
238
|
-
});
|
|
239
309
|
return {
|
|
240
310
|
...this.STATES,
|
|
241
311
|
};
|
|
@@ -263,13 +333,25 @@ export class CubeEngine {
|
|
|
263
333
|
|
|
264
334
|
return layersSolved.every((isLayerSolved) => isLayerSolved);
|
|
265
335
|
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Returns the history of all movements made.
|
|
339
|
+
*
|
|
340
|
+
* @param {boolean} asString - If true, returns the history as a string; otherwise, returns it as an array.
|
|
341
|
+
* @returns {string|array} The history of movements as an array or string.
|
|
342
|
+
*/
|
|
343
|
+
getMoves(asString = true) {
|
|
344
|
+
return asString ? this.MOVES : this.MOVES.join(" ");
|
|
345
|
+
}
|
|
266
346
|
}
|
|
267
347
|
|
|
268
|
-
|
|
348
|
+
const COLOR = {
|
|
269
349
|
W: ["W", "W", "W", "W", "W", "W", "W", "W", "W"],
|
|
270
350
|
G: ["G", "G", "G", "G", "G", "G", "G", "G", "G"],
|
|
271
351
|
R: ["R", "R", "R", "R", "R", "R", "R", "R", "R"],
|
|
272
352
|
B: ["B", "B", "B", "B", "B", "B", "B", "B", "B"],
|
|
273
353
|
O: ["O", "O", "O", "O", "O", "O", "O", "O", "O"],
|
|
274
354
|
Y: ["Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y"],
|
|
275
|
-
};
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
export { COLOR, CubeEngine };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
class CubeEngine {
|
|
2
|
+
MOVES = [];
|
|
3
|
+
|
|
4
|
+
// States object for the rotation
|
|
5
|
+
STATES = {
|
|
6
|
+
UPPER: [
|
|
7
|
+
// (White)
|
|
8
|
+
[COLOR.W[0], COLOR.W[1], COLOR.W[2]],
|
|
9
|
+
[COLOR.W[3], COLOR.W[4], COLOR.W[5]],
|
|
10
|
+
[COLOR.W[6], COLOR.W[7], COLOR.W[8]],
|
|
11
|
+
],
|
|
12
|
+
LEFT: [
|
|
13
|
+
// (Orange)
|
|
14
|
+
[COLOR.O[0], COLOR.O[1], COLOR.O[2]],
|
|
15
|
+
[COLOR.O[3], COLOR.O[4], COLOR.O[5]],
|
|
16
|
+
[COLOR.O[6], COLOR.O[7], COLOR.O[8]],
|
|
17
|
+
],
|
|
18
|
+
FRONT: [
|
|
19
|
+
// (Green)
|
|
20
|
+
[COLOR.G[0], COLOR.G[1], COLOR.G[2]],
|
|
21
|
+
[COLOR.G[3], COLOR.G[4], COLOR.G[5]],
|
|
22
|
+
[COLOR.G[6], COLOR.G[7], COLOR.G[8]],
|
|
23
|
+
],
|
|
24
|
+
RIGHT: [
|
|
25
|
+
// (Red)
|
|
26
|
+
[COLOR.R[0], COLOR.R[1], COLOR.R[2]],
|
|
27
|
+
[COLOR.R[3], COLOR.R[4], COLOR.R[5]],
|
|
28
|
+
[COLOR.R[6], COLOR.R[7], COLOR.R[8]],
|
|
29
|
+
],
|
|
30
|
+
BACK: [
|
|
31
|
+
// (Blue)
|
|
32
|
+
[COLOR.B[0], COLOR.B[1], COLOR.B[2]],
|
|
33
|
+
[COLOR.B[3], COLOR.B[4], COLOR.B[5]],
|
|
34
|
+
[COLOR.B[6], COLOR.B[7], COLOR.B[8]],
|
|
35
|
+
],
|
|
36
|
+
DOWN: [
|
|
37
|
+
// (Yellow)
|
|
38
|
+
[COLOR.Y[0], COLOR.Y[1], COLOR.Y[2]],
|
|
39
|
+
[COLOR.Y[3], COLOR.Y[4], COLOR.Y[5]],
|
|
40
|
+
[COLOR.Y[6], COLOR.Y[7], COLOR.Y[8]],
|
|
41
|
+
],
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Rotates the (UPPER) layer clockwise or counterclockwise.
|
|
46
|
+
*/
|
|
47
|
+
rotateU(clockwise = true) {
|
|
48
|
+
if (clockwise) {
|
|
49
|
+
this.#rotateU(true);
|
|
50
|
+
this.MOVES.push("U");
|
|
51
|
+
} else {
|
|
52
|
+
this.#rotateU(false);
|
|
53
|
+
this.MOVES.push("U'");
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
#rotateU(clockwise = true) {
|
|
58
|
+
if (clockwise) {
|
|
59
|
+
this.STATES.UPPER = this.#switchMatrix(this.STATES.UPPER, true);
|
|
60
|
+
|
|
61
|
+
const tempFront = [...this.STATES.FRONT[0]];
|
|
62
|
+
const tempRight = [...this.STATES.RIGHT[0]];
|
|
63
|
+
const tempLeft = [...this.STATES.LEFT[0]];
|
|
64
|
+
const tempBack = [...this.STATES.BACK[0]];
|
|
65
|
+
|
|
66
|
+
this.STATES.FRONT[0] = [...tempRight];
|
|
67
|
+
this.STATES.LEFT[0] = [...tempFront];
|
|
68
|
+
this.STATES.BACK[0] = [...tempLeft];
|
|
69
|
+
this.STATES.RIGHT[0] = [...tempBack];
|
|
70
|
+
} else {
|
|
71
|
+
this.STATES.UPPER = this.#switchMatrix(this.STATES.UPPER, false);
|
|
72
|
+
|
|
73
|
+
const tempFront = [...this.STATES.FRONT[0]];
|
|
74
|
+
const tempRight = [...this.STATES.RIGHT[0]];
|
|
75
|
+
const tempLeft = [...this.STATES.LEFT[0]];
|
|
76
|
+
const tempBack = [...this.STATES.BACK[0]];
|
|
77
|
+
|
|
78
|
+
this.STATES.FRONT[0] = [...tempLeft];
|
|
79
|
+
this.STATES.LEFT[0] = [...tempBack];
|
|
80
|
+
this.STATES.BACK[0] = [...tempRight];
|
|
81
|
+
this.STATES.RIGHT[0] = [...tempFront];
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Rotates the (FRONT) layer clockwise or counterclockwise.
|
|
87
|
+
*/
|
|
88
|
+
rotateF(clockwise = true) {
|
|
89
|
+
if (clockwise) {
|
|
90
|
+
this.#rotateF(true);
|
|
91
|
+
this.MOVES.push("F");
|
|
92
|
+
} else {
|
|
93
|
+
this.#rotateF(false);
|
|
94
|
+
this.MOVES.push("F'");
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
#rotateF(clockwise = true) {
|
|
99
|
+
if (clockwise) {
|
|
100
|
+
this.#rotateX(true);
|
|
101
|
+
this.#rotateU(true);
|
|
102
|
+
this.#rotateX(false);
|
|
103
|
+
} else {
|
|
104
|
+
this.#rotateX(true);
|
|
105
|
+
this.#rotateU(false);
|
|
106
|
+
this.#rotateX(false);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Rotates the (RIGHT) layer clockwise or counterclockwise.
|
|
112
|
+
*/
|
|
113
|
+
rotateR(clockwise = true) {
|
|
114
|
+
if (clockwise) {
|
|
115
|
+
this.#rotateR(true);
|
|
116
|
+
this.MOVES.push("R");
|
|
117
|
+
} else {
|
|
118
|
+
this.#rotateR(false);
|
|
119
|
+
this.MOVES.push("R'");
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
#rotateR(clockwise = true) {
|
|
124
|
+
if (clockwise) {
|
|
125
|
+
this.#rotateY(true);
|
|
126
|
+
this.#rotateX(true);
|
|
127
|
+
this.#rotateU(true);
|
|
128
|
+
this.#rotateX(false);
|
|
129
|
+
this.#rotateY(false);
|
|
130
|
+
} else {
|
|
131
|
+
this.#rotateY(true);
|
|
132
|
+
this.#rotateX(true);
|
|
133
|
+
this.#rotateU(false);
|
|
134
|
+
this.#rotateX(false);
|
|
135
|
+
this.#rotateY(false);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Rotates the (LEFT) layer clockwise or counterclockwise.
|
|
141
|
+
*/
|
|
142
|
+
rotateL(clockwise = true) {
|
|
143
|
+
if (clockwise) {
|
|
144
|
+
this.#rotateL(true);
|
|
145
|
+
this.MOVES.push("L");
|
|
146
|
+
} else {
|
|
147
|
+
this.#rotateL(false);
|
|
148
|
+
this.MOVES.push("L'");
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
#rotateL(clockwise = true) {
|
|
153
|
+
if (clockwise) {
|
|
154
|
+
this.#rotateY(false);
|
|
155
|
+
this.#rotateX(true);
|
|
156
|
+
this.#rotateU(true);
|
|
157
|
+
this.#rotateX(false);
|
|
158
|
+
this.#rotateY(true);
|
|
159
|
+
} else {
|
|
160
|
+
this.#rotateY(false);
|
|
161
|
+
this.#rotateX(true);
|
|
162
|
+
this.#rotateU(false);
|
|
163
|
+
this.#rotateX(false);
|
|
164
|
+
this.#rotateY(true);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Rotates the (DOWN) layer clockwise or counterclockwise.
|
|
170
|
+
*/
|
|
171
|
+
rotateD(clockwise = true) {
|
|
172
|
+
if (clockwise) {
|
|
173
|
+
this.#rotateD(true);
|
|
174
|
+
this.MOVES.push("D");
|
|
175
|
+
} else {
|
|
176
|
+
this.#rotateD(false);
|
|
177
|
+
this.MOVES.push("D'");
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
#rotateD(clockwise = true) {
|
|
182
|
+
if (clockwise) {
|
|
183
|
+
this.#rotateX(true);
|
|
184
|
+
this.#rotateF(true);
|
|
185
|
+
this.#rotateX(false);
|
|
186
|
+
} else {
|
|
187
|
+
this.#rotateX(true);
|
|
188
|
+
this.#rotateF(false);
|
|
189
|
+
this.#rotateX(false);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Rotates the (x) axis clockwise or counterclockwise.
|
|
195
|
+
*/
|
|
196
|
+
rotateX(clockwise = true) {
|
|
197
|
+
if (clockwise) {
|
|
198
|
+
this.#rotateX(true);
|
|
199
|
+
this.MOVES.push("x");
|
|
200
|
+
} else {
|
|
201
|
+
this.#rotateX(false);
|
|
202
|
+
this.MOVES.push("x'");
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
#rotateX(clockwise = true) {
|
|
207
|
+
const tempFront = structuredClone(this.STATES.FRONT);
|
|
208
|
+
const tempDown = structuredClone(this.STATES.DOWN);
|
|
209
|
+
const tempUpper = structuredClone(this.STATES.UPPER);
|
|
210
|
+
const tempBack = structuredClone(this.STATES.BACK);
|
|
211
|
+
const tempLeft = structuredClone(this.STATES.LEFT);
|
|
212
|
+
const tempRight = structuredClone(this.STATES.RIGHT);
|
|
213
|
+
|
|
214
|
+
if (clockwise) {
|
|
215
|
+
// Balance the rotation
|
|
216
|
+
this.STATES.LEFT = this.#switchMatrix(tempLeft, false);
|
|
217
|
+
this.STATES.RIGHT = this.#switchMatrix(tempRight, true);
|
|
218
|
+
|
|
219
|
+
// Rotate mid X axis
|
|
220
|
+
this.STATES.FRONT = [...tempDown];
|
|
221
|
+
this.STATES.UPPER = [...tempFront];
|
|
222
|
+
|
|
223
|
+
// Special permutation (BACK view elements)
|
|
224
|
+
this.STATES.BACK = this.#specialFlip(tempUpper);
|
|
225
|
+
this.STATES.DOWN = this.#specialFlip(tempBack);
|
|
226
|
+
} else {
|
|
227
|
+
this.STATES.LEFT = this.#switchMatrix(tempLeft, true);
|
|
228
|
+
this.STATES.RIGHT = this.#switchMatrix(tempRight, false);
|
|
229
|
+
|
|
230
|
+
this.STATES.FRONT = [...tempUpper];
|
|
231
|
+
this.STATES.DOWN = [...tempFront];
|
|
232
|
+
|
|
233
|
+
this.STATES.BACK = this.#specialFlip(tempDown);
|
|
234
|
+
this.STATES.UPPER = this.#specialFlip(tempBack);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Rotates the (y) axis clockwise or counterclockwise.
|
|
240
|
+
*/
|
|
241
|
+
rotateY(clockwise = true) {
|
|
242
|
+
if (clockwise) {
|
|
243
|
+
this.#rotateY(true);
|
|
244
|
+
this.MOVES.push("y");
|
|
245
|
+
} else {
|
|
246
|
+
this.#rotateY(false);
|
|
247
|
+
this.MOVES.push("y'");
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
#rotateY(clockwise = true) {
|
|
252
|
+
const tempFront = structuredClone(this.STATES.FRONT);
|
|
253
|
+
const tempRight = structuredClone(this.STATES.RIGHT);
|
|
254
|
+
const tempBack = structuredClone(this.STATES.BACK);
|
|
255
|
+
const tempLeft = structuredClone(this.STATES.LEFT);
|
|
256
|
+
|
|
257
|
+
if (clockwise) {
|
|
258
|
+
this.STATES.UPPER = this.#switchMatrix(this.STATES.UPPER, true);
|
|
259
|
+
this.STATES.DOWN = this.#switchMatrix(this.STATES.DOWN, false);
|
|
260
|
+
|
|
261
|
+
this.STATES.FRONT = [...tempRight];
|
|
262
|
+
this.STATES.RIGHT = [...tempBack];
|
|
263
|
+
this.STATES.LEFT = [...tempFront];
|
|
264
|
+
this.STATES.BACK = [...tempLeft];
|
|
265
|
+
} else {
|
|
266
|
+
this.STATES.UPPER = this.#switchMatrix(this.STATES.UPPER, false);
|
|
267
|
+
this.STATES.DOWN = this.#switchMatrix(this.STATES.DOWN, true);
|
|
268
|
+
|
|
269
|
+
this.STATES.FRONT = [...tempLeft];
|
|
270
|
+
this.STATES.RIGHT = [...tempFront];
|
|
271
|
+
this.STATES.LEFT = [...tempBack];
|
|
272
|
+
this.STATES.BACK = [...tempRight];
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Rotate the entire face in the direction set
|
|
278
|
+
*/
|
|
279
|
+
#switchMatrix(matrix, clockwise = true) {
|
|
280
|
+
const clone = structuredClone(matrix);
|
|
281
|
+
|
|
282
|
+
const tempMatrix = [...clone[0], ...clone[1], ...clone[2]];
|
|
283
|
+
|
|
284
|
+
if (clockwise) {
|
|
285
|
+
return [
|
|
286
|
+
[tempMatrix[6], tempMatrix[3], tempMatrix[0]],
|
|
287
|
+
[tempMatrix[7], tempMatrix[4], tempMatrix[1]],
|
|
288
|
+
[tempMatrix[8], tempMatrix[5], tempMatrix[2]],
|
|
289
|
+
];
|
|
290
|
+
} else {
|
|
291
|
+
return [
|
|
292
|
+
[tempMatrix[2], tempMatrix[5], tempMatrix[8]],
|
|
293
|
+
[tempMatrix[1], tempMatrix[4], tempMatrix[7]],
|
|
294
|
+
[tempMatrix[0], tempMatrix[3], tempMatrix[6]],
|
|
295
|
+
];
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
#specialFlip(matrix) {
|
|
300
|
+
return structuredClone(matrix)
|
|
301
|
+
.reverse()
|
|
302
|
+
.map((row) => [...row].reverse());
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Logs the current state of the cube.
|
|
307
|
+
*/
|
|
308
|
+
state() {
|
|
309
|
+
return {
|
|
310
|
+
...this.STATES,
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* Indicates if the cube is solve or not in all layers.
|
|
316
|
+
*/
|
|
317
|
+
isSolved() {
|
|
318
|
+
const temp = {
|
|
319
|
+
...this.STATES,
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
const layersSolved = Object.keys(temp).map((layer) => {
|
|
323
|
+
const mixedMatrix = [
|
|
324
|
+
...temp[layer][0],
|
|
325
|
+
...temp[layer][1],
|
|
326
|
+
...temp[layer][2],
|
|
327
|
+
];
|
|
328
|
+
|
|
329
|
+
const centerColor = mixedMatrix[4];
|
|
330
|
+
|
|
331
|
+
return mixedMatrix.every((currentColor) => currentColor === centerColor);
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
return layersSolved.every((isLayerSolved) => isLayerSolved);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Returns the history of all movements made.
|
|
339
|
+
*
|
|
340
|
+
* @param {boolean} asString - If true, returns the history as a string; otherwise, returns it as an array.
|
|
341
|
+
* @returns {string|array} The history of movements as an array or string.
|
|
342
|
+
*/
|
|
343
|
+
getMoves(asString = true) {
|
|
344
|
+
return asString ? this.MOVES : this.MOVES.join(" ");
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const COLOR = {
|
|
349
|
+
W: ["W", "W", "W", "W", "W", "W", "W", "W", "W"],
|
|
350
|
+
G: ["G", "G", "G", "G", "G", "G", "G", "G", "G"],
|
|
351
|
+
R: ["R", "R", "R", "R", "R", "R", "R", "R", "R"],
|
|
352
|
+
B: ["B", "B", "B", "B", "B", "B", "B", "B", "B"],
|
|
353
|
+
O: ["O", "O", "O", "O", "O", "O", "O", "O", "O"],
|
|
354
|
+
Y: ["Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y"],
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
export { COLOR, CubeEngine };
|