cube-state-engine 1.0.1 → 1.0.3
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/README.md +56 -2
- package/{src/index.js → dist/index.d.mts} +5 -3
- package/dist/index.d.ts +277 -0
- package/dist/index.js +284 -0
- package/dist/index.mjs +261 -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
package/README.md
CHANGED
|
@@ -1,3 +1,57 @@
|
|
|
1
|
-
#
|
|
1
|
+
# CUBE STATE ENGINE
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A core state manager designed to integrate with custom 3D cube models. This engine makes it easy to track and update the cube's state after every move, providing contextual information such as whether the cube is solved.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Methods
|
|
8
|
+
|
|
9
|
+
| Method | Description | Return Type |
|
|
10
|
+
| ----------------------------- | -------------------------------------------------------------------------- | ----------- |
|
|
11
|
+
| `isSolved()` | Checks if the cube is solved. | `boolean` |
|
|
12
|
+
| `state()` | Returns the current state of the cube as an object representing each face. | `object` |
|
|
13
|
+
| `rotateU(clockwise: boolean)` | Rotates the **Upper** face clockwise or counterclockwise. | `void` |
|
|
14
|
+
| `rotateF(clockwise: boolean)` | Rotates the **Front** face clockwise or counterclockwise. | `void` |
|
|
15
|
+
| `rotateD(clockwise: boolean)` | Rotates the **Down** face clockwise or counterclockwise. | `void` |
|
|
16
|
+
| `rotateX(clockwise: boolean)` | Rotates the cube along the **X-axis** clockwise or counterclockwise. | `void` |
|
|
17
|
+
| `rotateY(clockwise: boolean)` | Rotates the cube along the **Y-axis** clockwise or counterclockwise. | `void` |
|
|
18
|
+
| `rotateL(clockwise: boolean)` | Rotates the **Left** face clockwise or counterclockwise. | `void` |
|
|
19
|
+
| `rotateR(clockwise: boolean)` | Rotates the **Right** face clockwise or counterclockwise. | `void` |
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Cube State Structure
|
|
24
|
+
|
|
25
|
+
The `state()` method returns the current state of the cube as an object with six properties, each representing a face:
|
|
26
|
+
|
|
27
|
+
| Face | Description | Structure |
|
|
28
|
+
| ------- | ---------------------------- | ------------ |
|
|
29
|
+
| `UPPER` | The top face of the cube. | `string[][]` |
|
|
30
|
+
| `LEFT` | The left face of the cube. | `string[][]` |
|
|
31
|
+
| `FRONT` | The front face of the cube. | `string[][]` |
|
|
32
|
+
| `RIGHT` | The right face of the cube. | `string[][]` |
|
|
33
|
+
| `BACK` | The back face of the cube. | `string[][]` |
|
|
34
|
+
| `DOWN` | The bottom face of the cube. | `string[][]` |
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
## Example Usage
|
|
39
|
+
|
|
40
|
+
```javascript
|
|
41
|
+
import { CubeEngine } from "cube-state-engine";
|
|
42
|
+
|
|
43
|
+
// Initialize the engine
|
|
44
|
+
const cube = new CubeEngine();
|
|
45
|
+
|
|
46
|
+
// Check if the cube is solved
|
|
47
|
+
console.log(cube.isSolved()); // true
|
|
48
|
+
|
|
49
|
+
// Rotate the upper face clockwise
|
|
50
|
+
cube.rotateU(true);
|
|
51
|
+
|
|
52
|
+
// Get the current cube state
|
|
53
|
+
console.log(cube.state());
|
|
54
|
+
|
|
55
|
+
// Rotate the cube along the Y-axis
|
|
56
|
+
cube.rotateY(false);
|
|
57
|
+
```
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
class CubeEngine {
|
|
2
2
|
// States object for the rotation
|
|
3
3
|
STATES = {
|
|
4
4
|
UPPER: [
|
|
@@ -265,11 +265,13 @@ export class CubeEngine {
|
|
|
265
265
|
}
|
|
266
266
|
}
|
|
267
267
|
|
|
268
|
-
|
|
268
|
+
const COLOR = {
|
|
269
269
|
W: ["W", "W", "W", "W", "W", "W", "W", "W", "W"],
|
|
270
270
|
G: ["G", "G", "G", "G", "G", "G", "G", "G", "G"],
|
|
271
271
|
R: ["R", "R", "R", "R", "R", "R", "R", "R", "R"],
|
|
272
272
|
B: ["B", "B", "B", "B", "B", "B", "B", "B", "B"],
|
|
273
273
|
O: ["O", "O", "O", "O", "O", "O", "O", "O", "O"],
|
|
274
274
|
Y: ["Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y"],
|
|
275
|
-
};
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
export { COLOR, CubeEngine };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
class CubeEngine {
|
|
2
|
+
// States object for the rotation
|
|
3
|
+
STATES = {
|
|
4
|
+
UPPER: [
|
|
5
|
+
// (White)
|
|
6
|
+
[COLOR.W[0], COLOR.W[1], COLOR.W[2]],
|
|
7
|
+
[COLOR.W[3], COLOR.W[4], COLOR.W[5]],
|
|
8
|
+
[COLOR.W[6], COLOR.W[7], COLOR.W[8]],
|
|
9
|
+
],
|
|
10
|
+
LEFT: [
|
|
11
|
+
// (Orange)
|
|
12
|
+
[COLOR.O[0], COLOR.O[1], COLOR.O[2]],
|
|
13
|
+
[COLOR.O[3], COLOR.O[4], COLOR.O[5]],
|
|
14
|
+
[COLOR.O[6], COLOR.O[7], COLOR.O[8]],
|
|
15
|
+
],
|
|
16
|
+
FRONT: [
|
|
17
|
+
// (Green)
|
|
18
|
+
[COLOR.G[0], COLOR.G[1], COLOR.G[2]],
|
|
19
|
+
[COLOR.G[3], COLOR.G[4], COLOR.G[5]],
|
|
20
|
+
[COLOR.G[6], COLOR.G[7], COLOR.G[8]],
|
|
21
|
+
],
|
|
22
|
+
RIGHT: [
|
|
23
|
+
// (Red)
|
|
24
|
+
[COLOR.R[0], COLOR.R[1], COLOR.R[2]],
|
|
25
|
+
[COLOR.R[3], COLOR.R[4], COLOR.R[5]],
|
|
26
|
+
[COLOR.R[6], COLOR.R[7], COLOR.R[8]],
|
|
27
|
+
],
|
|
28
|
+
BACK: [
|
|
29
|
+
// (Blue)
|
|
30
|
+
[COLOR.B[0], COLOR.B[1], COLOR.B[2]],
|
|
31
|
+
[COLOR.B[3], COLOR.B[4], COLOR.B[5]],
|
|
32
|
+
[COLOR.B[6], COLOR.B[7], COLOR.B[8]],
|
|
33
|
+
],
|
|
34
|
+
DOWN: [
|
|
35
|
+
// (Yellow)
|
|
36
|
+
[COLOR.Y[0], COLOR.Y[1], COLOR.Y[2]],
|
|
37
|
+
[COLOR.Y[3], COLOR.Y[4], COLOR.Y[5]],
|
|
38
|
+
[COLOR.Y[6], COLOR.Y[7], COLOR.Y[8]],
|
|
39
|
+
],
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Rotates the top (U) layer clockwise or counterclockwise.
|
|
44
|
+
*/
|
|
45
|
+
rotateU(clockwise = true) {
|
|
46
|
+
if (clockwise) {
|
|
47
|
+
// Rotate the top layer (UPPER) clockwise
|
|
48
|
+
this.STATES.UPPER = this.#switchMatrix(this.STATES.UPPER, true);
|
|
49
|
+
|
|
50
|
+
const tempFront = [...this.STATES.FRONT[0]];
|
|
51
|
+
const tempRight = [...this.STATES.RIGHT[0]];
|
|
52
|
+
const tempLeft = [...this.STATES.LEFT[0]];
|
|
53
|
+
const tempBack = [...this.STATES.BACK[0]];
|
|
54
|
+
|
|
55
|
+
this.STATES.FRONT[0] = [...tempRight];
|
|
56
|
+
this.STATES.LEFT[0] = [...tempFront];
|
|
57
|
+
this.STATES.BACK[0] = [...tempLeft];
|
|
58
|
+
this.STATES.RIGHT[0] = [...tempBack];
|
|
59
|
+
} else {
|
|
60
|
+
// Rotate the top layer (UPPER) counterclockwise
|
|
61
|
+
this.STATES.UPPER = this.#switchMatrix(this.STATES.UPPER, false);
|
|
62
|
+
|
|
63
|
+
const tempFront = [...this.STATES.FRONT[0]];
|
|
64
|
+
const tempRight = [...this.STATES.RIGHT[0]];
|
|
65
|
+
const tempLeft = [...this.STATES.LEFT[0]];
|
|
66
|
+
const tempBack = [...this.STATES.BACK[0]];
|
|
67
|
+
|
|
68
|
+
this.STATES.FRONT[0] = [...tempLeft];
|
|
69
|
+
this.STATES.LEFT[0] = [...tempBack];
|
|
70
|
+
this.STATES.BACK[0] = [...tempRight];
|
|
71
|
+
this.STATES.RIGHT[0] = [...tempFront];
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Rotates the front (F) layer clockwise or counterclockwise.
|
|
77
|
+
*/
|
|
78
|
+
rotateF(clockwise = true) {
|
|
79
|
+
if (clockwise) {
|
|
80
|
+
this.rotateX(true);
|
|
81
|
+
this.rotateU(true);
|
|
82
|
+
this.rotateX(false);
|
|
83
|
+
} else {
|
|
84
|
+
this.rotateX(true);
|
|
85
|
+
this.rotateU(false);
|
|
86
|
+
this.rotateX(false);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
rotateR(clockwise = true) {
|
|
91
|
+
if (clockwise) {
|
|
92
|
+
this.rotateY(true);
|
|
93
|
+
this.rotateX(true);
|
|
94
|
+
this.rotateU(true);
|
|
95
|
+
this.rotateX(false);
|
|
96
|
+
this.rotateY(false);
|
|
97
|
+
} else {
|
|
98
|
+
this.rotateY(true);
|
|
99
|
+
this.rotateX(true);
|
|
100
|
+
this.rotateU(false);
|
|
101
|
+
this.rotateX(false);
|
|
102
|
+
this.rotateY(false);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
rotateL(clockwise = true) {
|
|
107
|
+
if (clockwise) {
|
|
108
|
+
this.rotateY(false);
|
|
109
|
+
this.rotateX(true);
|
|
110
|
+
this.rotateU(true);
|
|
111
|
+
this.rotateX(false);
|
|
112
|
+
this.rotateY(true);
|
|
113
|
+
} else {
|
|
114
|
+
this.rotateY(false);
|
|
115
|
+
this.rotateX(true);
|
|
116
|
+
this.rotateU(false);
|
|
117
|
+
this.rotateX(false);
|
|
118
|
+
this.rotateY(true);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
rotateD(clockwise = true) {
|
|
123
|
+
if (clockwise) {
|
|
124
|
+
this.rotateX(true);
|
|
125
|
+
this.rotateF(true);
|
|
126
|
+
this.rotateX(false);
|
|
127
|
+
} else {
|
|
128
|
+
this.rotateX(true);
|
|
129
|
+
this.rotateF(false);
|
|
130
|
+
this.rotateX(false);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Rotates the (x) axis clockwise or counterclockwise.
|
|
136
|
+
*/
|
|
137
|
+
rotateX(clockwise = true) {
|
|
138
|
+
const tempFront = structuredClone(this.STATES.FRONT);
|
|
139
|
+
const tempDown = structuredClone(this.STATES.DOWN);
|
|
140
|
+
const tempUpper = structuredClone(this.STATES.UPPER);
|
|
141
|
+
const tempBack = structuredClone(this.STATES.BACK);
|
|
142
|
+
const tempLeft = structuredClone(this.STATES.LEFT);
|
|
143
|
+
const tempRight = structuredClone(this.STATES.RIGHT);
|
|
144
|
+
|
|
145
|
+
if (clockwise) {
|
|
146
|
+
// Rotate the RIGHT and LEFT layers
|
|
147
|
+
this.STATES.LEFT = this.#switchMatrix(tempLeft, false);
|
|
148
|
+
this.STATES.RIGHT = this.#switchMatrix(tempRight, true);
|
|
149
|
+
|
|
150
|
+
// Rotation X axis
|
|
151
|
+
this.STATES.FRONT = [...tempDown];
|
|
152
|
+
this.STATES.UPPER = [...tempFront];
|
|
153
|
+
|
|
154
|
+
// Special permutation
|
|
155
|
+
this.STATES.BACK = this.#specialFlip(tempUpper);
|
|
156
|
+
this.STATES.DOWN = this.#specialFlip(tempBack);
|
|
157
|
+
} else {
|
|
158
|
+
// Rotate the RIGHT and LEFT layers
|
|
159
|
+
this.STATES.LEFT = this.#switchMatrix(tempLeft, true);
|
|
160
|
+
this.STATES.RIGHT = this.#switchMatrix(tempRight, false);
|
|
161
|
+
|
|
162
|
+
// Rotation X axis
|
|
163
|
+
this.STATES.FRONT = [...tempUpper];
|
|
164
|
+
this.STATES.DOWN = [...tempFront];
|
|
165
|
+
|
|
166
|
+
// Special permutation
|
|
167
|
+
this.STATES.BACK = this.#specialFlip(tempDown);
|
|
168
|
+
this.STATES.UPPER = this.#specialFlip(tempBack);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Rotates the (y) axis clockwise or counterclockwise.
|
|
174
|
+
*/
|
|
175
|
+
rotateY(clockwise = true) {
|
|
176
|
+
const tempFront = structuredClone(this.STATES.FRONT);
|
|
177
|
+
const tempRight = structuredClone(this.STATES.RIGHT);
|
|
178
|
+
const tempBack = structuredClone(this.STATES.BACK);
|
|
179
|
+
const tempLeft = structuredClone(this.STATES.LEFT);
|
|
180
|
+
|
|
181
|
+
if (clockwise) {
|
|
182
|
+
this.STATES.UPPER = this.#switchMatrix(this.STATES.UPPER, true);
|
|
183
|
+
this.STATES.DOWN = this.#switchMatrix(this.STATES.DOWN, false);
|
|
184
|
+
|
|
185
|
+
// Rotation X axis
|
|
186
|
+
this.STATES.FRONT = [...tempRight];
|
|
187
|
+
this.STATES.RIGHT = [...tempBack];
|
|
188
|
+
this.STATES.LEFT = [...tempFront];
|
|
189
|
+
this.STATES.BACK = [...tempLeft];
|
|
190
|
+
} else {
|
|
191
|
+
this.STATES.UPPER = this.#switchMatrix(this.STATES.UPPER, false);
|
|
192
|
+
this.STATES.DOWN = this.#switchMatrix(this.STATES.DOWN, true);
|
|
193
|
+
|
|
194
|
+
// Rotation X axis
|
|
195
|
+
this.STATES.FRONT = [...tempLeft];
|
|
196
|
+
this.STATES.RIGHT = [...tempFront];
|
|
197
|
+
this.STATES.LEFT = [...tempBack];
|
|
198
|
+
this.STATES.BACK = [...tempRight];
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Rotate the entire face in the direction set
|
|
204
|
+
*/
|
|
205
|
+
#switchMatrix(matrix, clockwise = true) {
|
|
206
|
+
const clone = structuredClone(matrix);
|
|
207
|
+
|
|
208
|
+
const tempMatrix = [...clone[0], ...clone[1], ...clone[2]];
|
|
209
|
+
|
|
210
|
+
if (clockwise) {
|
|
211
|
+
return [
|
|
212
|
+
[tempMatrix[6], tempMatrix[3], tempMatrix[0]],
|
|
213
|
+
[tempMatrix[7], tempMatrix[4], tempMatrix[1]],
|
|
214
|
+
[tempMatrix[8], tempMatrix[5], tempMatrix[2]],
|
|
215
|
+
];
|
|
216
|
+
} else {
|
|
217
|
+
return [
|
|
218
|
+
[tempMatrix[2], tempMatrix[5], tempMatrix[8]],
|
|
219
|
+
[tempMatrix[1], tempMatrix[4], tempMatrix[7]],
|
|
220
|
+
[tempMatrix[0], tempMatrix[3], tempMatrix[6]],
|
|
221
|
+
];
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
#specialFlip(matrix) {
|
|
226
|
+
return structuredClone(matrix)
|
|
227
|
+
.reverse()
|
|
228
|
+
.map((row) => [...row].reverse());
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Logs the current state of the cube.
|
|
233
|
+
*/
|
|
234
|
+
state() {
|
|
235
|
+
console.clear();
|
|
236
|
+
console.log({
|
|
237
|
+
...this.STATES,
|
|
238
|
+
});
|
|
239
|
+
return {
|
|
240
|
+
...this.STATES,
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Indicates if the cube is solve or not in all layers.
|
|
246
|
+
*/
|
|
247
|
+
isSolved() {
|
|
248
|
+
const temp = {
|
|
249
|
+
...this.STATES,
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
const layersSolved = Object.keys(temp).map((layer) => {
|
|
253
|
+
const mixedMatrix = [
|
|
254
|
+
...temp[layer][0],
|
|
255
|
+
...temp[layer][1],
|
|
256
|
+
...temp[layer][2],
|
|
257
|
+
];
|
|
258
|
+
|
|
259
|
+
const centerColor = mixedMatrix[4];
|
|
260
|
+
|
|
261
|
+
return mixedMatrix.every((currentColor) => currentColor === centerColor);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
return layersSolved.every((isLayerSolved) => isLayerSolved);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const COLOR = {
|
|
269
|
+
W: ["W", "W", "W", "W", "W", "W", "W", "W", "W"],
|
|
270
|
+
G: ["G", "G", "G", "G", "G", "G", "G", "G", "G"],
|
|
271
|
+
R: ["R", "R", "R", "R", "R", "R", "R", "R", "R"],
|
|
272
|
+
B: ["B", "B", "B", "B", "B", "B", "B", "B", "B"],
|
|
273
|
+
O: ["O", "O", "O", "O", "O", "O", "O", "O", "O"],
|
|
274
|
+
Y: ["Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y"],
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
export { COLOR, CubeEngine };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
8
|
+
var __typeError = (msg) => {
|
|
9
|
+
throw TypeError(msg);
|
|
10
|
+
};
|
|
11
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
12
|
+
var __spreadValues = (a, b) => {
|
|
13
|
+
for (var prop in b || (b = {}))
|
|
14
|
+
if (__hasOwnProp.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
if (__getOwnPropSymbols)
|
|
17
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
18
|
+
if (__propIsEnum.call(b, prop))
|
|
19
|
+
__defNormalProp(a, prop, b[prop]);
|
|
20
|
+
}
|
|
21
|
+
return a;
|
|
22
|
+
};
|
|
23
|
+
var __export = (target, all) => {
|
|
24
|
+
for (var name in all)
|
|
25
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
26
|
+
};
|
|
27
|
+
var __copyProps = (to, from, except, desc) => {
|
|
28
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
29
|
+
for (let key of __getOwnPropNames(from))
|
|
30
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
31
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
32
|
+
}
|
|
33
|
+
return to;
|
|
34
|
+
};
|
|
35
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
36
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
37
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
38
|
+
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
39
|
+
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
40
|
+
|
|
41
|
+
// src/index.js
|
|
42
|
+
var src_exports = {};
|
|
43
|
+
__export(src_exports, {
|
|
44
|
+
COLOR: () => COLOR,
|
|
45
|
+
CubeEngine: () => CubeEngine
|
|
46
|
+
});
|
|
47
|
+
module.exports = __toCommonJS(src_exports);
|
|
48
|
+
var _CubeEngine_instances, switchMatrix_fn, specialFlip_fn;
|
|
49
|
+
var CubeEngine = class {
|
|
50
|
+
constructor() {
|
|
51
|
+
__privateAdd(this, _CubeEngine_instances);
|
|
52
|
+
// States object for the rotation
|
|
53
|
+
__publicField(this, "STATES", {
|
|
54
|
+
UPPER: [
|
|
55
|
+
// (White)
|
|
56
|
+
[COLOR.W[0], COLOR.W[1], COLOR.W[2]],
|
|
57
|
+
[COLOR.W[3], COLOR.W[4], COLOR.W[5]],
|
|
58
|
+
[COLOR.W[6], COLOR.W[7], COLOR.W[8]]
|
|
59
|
+
],
|
|
60
|
+
LEFT: [
|
|
61
|
+
// (Orange)
|
|
62
|
+
[COLOR.O[0], COLOR.O[1], COLOR.O[2]],
|
|
63
|
+
[COLOR.O[3], COLOR.O[4], COLOR.O[5]],
|
|
64
|
+
[COLOR.O[6], COLOR.O[7], COLOR.O[8]]
|
|
65
|
+
],
|
|
66
|
+
FRONT: [
|
|
67
|
+
// (Green)
|
|
68
|
+
[COLOR.G[0], COLOR.G[1], COLOR.G[2]],
|
|
69
|
+
[COLOR.G[3], COLOR.G[4], COLOR.G[5]],
|
|
70
|
+
[COLOR.G[6], COLOR.G[7], COLOR.G[8]]
|
|
71
|
+
],
|
|
72
|
+
RIGHT: [
|
|
73
|
+
// (Red)
|
|
74
|
+
[COLOR.R[0], COLOR.R[1], COLOR.R[2]],
|
|
75
|
+
[COLOR.R[3], COLOR.R[4], COLOR.R[5]],
|
|
76
|
+
[COLOR.R[6], COLOR.R[7], COLOR.R[8]]
|
|
77
|
+
],
|
|
78
|
+
BACK: [
|
|
79
|
+
// (Blue)
|
|
80
|
+
[COLOR.B[0], COLOR.B[1], COLOR.B[2]],
|
|
81
|
+
[COLOR.B[3], COLOR.B[4], COLOR.B[5]],
|
|
82
|
+
[COLOR.B[6], COLOR.B[7], COLOR.B[8]]
|
|
83
|
+
],
|
|
84
|
+
DOWN: [
|
|
85
|
+
// (Yellow)
|
|
86
|
+
[COLOR.Y[0], COLOR.Y[1], COLOR.Y[2]],
|
|
87
|
+
[COLOR.Y[3], COLOR.Y[4], COLOR.Y[5]],
|
|
88
|
+
[COLOR.Y[6], COLOR.Y[7], COLOR.Y[8]]
|
|
89
|
+
]
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Rotates the top (U) layer clockwise or counterclockwise.
|
|
94
|
+
*/
|
|
95
|
+
rotateU(clockwise = true) {
|
|
96
|
+
if (clockwise) {
|
|
97
|
+
this.STATES.UPPER = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, this.STATES.UPPER, true);
|
|
98
|
+
const tempFront = [...this.STATES.FRONT[0]];
|
|
99
|
+
const tempRight = [...this.STATES.RIGHT[0]];
|
|
100
|
+
const tempLeft = [...this.STATES.LEFT[0]];
|
|
101
|
+
const tempBack = [...this.STATES.BACK[0]];
|
|
102
|
+
this.STATES.FRONT[0] = [...tempRight];
|
|
103
|
+
this.STATES.LEFT[0] = [...tempFront];
|
|
104
|
+
this.STATES.BACK[0] = [...tempLeft];
|
|
105
|
+
this.STATES.RIGHT[0] = [...tempBack];
|
|
106
|
+
} else {
|
|
107
|
+
this.STATES.UPPER = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, this.STATES.UPPER, false);
|
|
108
|
+
const tempFront = [...this.STATES.FRONT[0]];
|
|
109
|
+
const tempRight = [...this.STATES.RIGHT[0]];
|
|
110
|
+
const tempLeft = [...this.STATES.LEFT[0]];
|
|
111
|
+
const tempBack = [...this.STATES.BACK[0]];
|
|
112
|
+
this.STATES.FRONT[0] = [...tempLeft];
|
|
113
|
+
this.STATES.LEFT[0] = [...tempBack];
|
|
114
|
+
this.STATES.BACK[0] = [...tempRight];
|
|
115
|
+
this.STATES.RIGHT[0] = [...tempFront];
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Rotates the front (F) layer clockwise or counterclockwise.
|
|
120
|
+
*/
|
|
121
|
+
rotateF(clockwise = true) {
|
|
122
|
+
if (clockwise) {
|
|
123
|
+
this.rotateX(true);
|
|
124
|
+
this.rotateU(true);
|
|
125
|
+
this.rotateX(false);
|
|
126
|
+
} else {
|
|
127
|
+
this.rotateX(true);
|
|
128
|
+
this.rotateU(false);
|
|
129
|
+
this.rotateX(false);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
rotateR(clockwise = true) {
|
|
133
|
+
if (clockwise) {
|
|
134
|
+
this.rotateY(true);
|
|
135
|
+
this.rotateX(true);
|
|
136
|
+
this.rotateU(true);
|
|
137
|
+
this.rotateX(false);
|
|
138
|
+
this.rotateY(false);
|
|
139
|
+
} else {
|
|
140
|
+
this.rotateY(true);
|
|
141
|
+
this.rotateX(true);
|
|
142
|
+
this.rotateU(false);
|
|
143
|
+
this.rotateX(false);
|
|
144
|
+
this.rotateY(false);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
rotateL(clockwise = true) {
|
|
148
|
+
if (clockwise) {
|
|
149
|
+
this.rotateY(false);
|
|
150
|
+
this.rotateX(true);
|
|
151
|
+
this.rotateU(true);
|
|
152
|
+
this.rotateX(false);
|
|
153
|
+
this.rotateY(true);
|
|
154
|
+
} else {
|
|
155
|
+
this.rotateY(false);
|
|
156
|
+
this.rotateX(true);
|
|
157
|
+
this.rotateU(false);
|
|
158
|
+
this.rotateX(false);
|
|
159
|
+
this.rotateY(true);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
rotateD(clockwise = true) {
|
|
163
|
+
if (clockwise) {
|
|
164
|
+
this.rotateX(true);
|
|
165
|
+
this.rotateF(true);
|
|
166
|
+
this.rotateX(false);
|
|
167
|
+
} else {
|
|
168
|
+
this.rotateX(true);
|
|
169
|
+
this.rotateF(false);
|
|
170
|
+
this.rotateX(false);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Rotates the (x) axis clockwise or counterclockwise.
|
|
175
|
+
*/
|
|
176
|
+
rotateX(clockwise = true) {
|
|
177
|
+
const tempFront = structuredClone(this.STATES.FRONT);
|
|
178
|
+
const tempDown = structuredClone(this.STATES.DOWN);
|
|
179
|
+
const tempUpper = structuredClone(this.STATES.UPPER);
|
|
180
|
+
const tempBack = structuredClone(this.STATES.BACK);
|
|
181
|
+
const tempLeft = structuredClone(this.STATES.LEFT);
|
|
182
|
+
const tempRight = structuredClone(this.STATES.RIGHT);
|
|
183
|
+
if (clockwise) {
|
|
184
|
+
this.STATES.LEFT = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempLeft, false);
|
|
185
|
+
this.STATES.RIGHT = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempRight, true);
|
|
186
|
+
this.STATES.FRONT = [...tempDown];
|
|
187
|
+
this.STATES.UPPER = [...tempFront];
|
|
188
|
+
this.STATES.BACK = __privateMethod(this, _CubeEngine_instances, specialFlip_fn).call(this, tempUpper);
|
|
189
|
+
this.STATES.DOWN = __privateMethod(this, _CubeEngine_instances, specialFlip_fn).call(this, tempBack);
|
|
190
|
+
} else {
|
|
191
|
+
this.STATES.LEFT = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempLeft, true);
|
|
192
|
+
this.STATES.RIGHT = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempRight, false);
|
|
193
|
+
this.STATES.FRONT = [...tempUpper];
|
|
194
|
+
this.STATES.DOWN = [...tempFront];
|
|
195
|
+
this.STATES.BACK = __privateMethod(this, _CubeEngine_instances, specialFlip_fn).call(this, tempDown);
|
|
196
|
+
this.STATES.UPPER = __privateMethod(this, _CubeEngine_instances, specialFlip_fn).call(this, tempBack);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Rotates the (y) axis clockwise or counterclockwise.
|
|
201
|
+
*/
|
|
202
|
+
rotateY(clockwise = true) {
|
|
203
|
+
const tempFront = structuredClone(this.STATES.FRONT);
|
|
204
|
+
const tempRight = structuredClone(this.STATES.RIGHT);
|
|
205
|
+
const tempBack = structuredClone(this.STATES.BACK);
|
|
206
|
+
const tempLeft = structuredClone(this.STATES.LEFT);
|
|
207
|
+
if (clockwise) {
|
|
208
|
+
this.STATES.UPPER = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, this.STATES.UPPER, true);
|
|
209
|
+
this.STATES.DOWN = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, this.STATES.DOWN, false);
|
|
210
|
+
this.STATES.FRONT = [...tempRight];
|
|
211
|
+
this.STATES.RIGHT = [...tempBack];
|
|
212
|
+
this.STATES.LEFT = [...tempFront];
|
|
213
|
+
this.STATES.BACK = [...tempLeft];
|
|
214
|
+
} else {
|
|
215
|
+
this.STATES.UPPER = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, this.STATES.UPPER, false);
|
|
216
|
+
this.STATES.DOWN = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, this.STATES.DOWN, true);
|
|
217
|
+
this.STATES.FRONT = [...tempLeft];
|
|
218
|
+
this.STATES.RIGHT = [...tempFront];
|
|
219
|
+
this.STATES.LEFT = [...tempBack];
|
|
220
|
+
this.STATES.BACK = [...tempRight];
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Logs the current state of the cube.
|
|
225
|
+
*/
|
|
226
|
+
state() {
|
|
227
|
+
console.clear();
|
|
228
|
+
console.log(__spreadValues({}, this.STATES));
|
|
229
|
+
return __spreadValues({}, this.STATES);
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Indicates if the cube is solve or not in all layers.
|
|
233
|
+
*/
|
|
234
|
+
isSolved() {
|
|
235
|
+
const temp = __spreadValues({}, this.STATES);
|
|
236
|
+
const layersSolved = Object.keys(temp).map((layer) => {
|
|
237
|
+
const mixedMatrix = [
|
|
238
|
+
...temp[layer][0],
|
|
239
|
+
...temp[layer][1],
|
|
240
|
+
...temp[layer][2]
|
|
241
|
+
];
|
|
242
|
+
const centerColor = mixedMatrix[4];
|
|
243
|
+
return mixedMatrix.every((currentColor) => currentColor === centerColor);
|
|
244
|
+
});
|
|
245
|
+
return layersSolved.every((isLayerSolved) => isLayerSolved);
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
_CubeEngine_instances = new WeakSet();
|
|
249
|
+
/**
|
|
250
|
+
* Rotate the entire face in the direction set
|
|
251
|
+
*/
|
|
252
|
+
switchMatrix_fn = function(matrix, clockwise = true) {
|
|
253
|
+
const clone = structuredClone(matrix);
|
|
254
|
+
const tempMatrix = [...clone[0], ...clone[1], ...clone[2]];
|
|
255
|
+
if (clockwise) {
|
|
256
|
+
return [
|
|
257
|
+
[tempMatrix[6], tempMatrix[3], tempMatrix[0]],
|
|
258
|
+
[tempMatrix[7], tempMatrix[4], tempMatrix[1]],
|
|
259
|
+
[tempMatrix[8], tempMatrix[5], tempMatrix[2]]
|
|
260
|
+
];
|
|
261
|
+
} else {
|
|
262
|
+
return [
|
|
263
|
+
[tempMatrix[2], tempMatrix[5], tempMatrix[8]],
|
|
264
|
+
[tempMatrix[1], tempMatrix[4], tempMatrix[7]],
|
|
265
|
+
[tempMatrix[0], tempMatrix[3], tempMatrix[6]]
|
|
266
|
+
];
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
specialFlip_fn = function(matrix) {
|
|
270
|
+
return structuredClone(matrix).reverse().map((row) => [...row].reverse());
|
|
271
|
+
};
|
|
272
|
+
var COLOR = {
|
|
273
|
+
W: ["W", "W", "W", "W", "W", "W", "W", "W", "W"],
|
|
274
|
+
G: ["G", "G", "G", "G", "G", "G", "G", "G", "G"],
|
|
275
|
+
R: ["R", "R", "R", "R", "R", "R", "R", "R", "R"],
|
|
276
|
+
B: ["B", "B", "B", "B", "B", "B", "B", "B", "B"],
|
|
277
|
+
O: ["O", "O", "O", "O", "O", "O", "O", "O", "O"],
|
|
278
|
+
Y: ["Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y"]
|
|
279
|
+
};
|
|
280
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
281
|
+
0 && (module.exports = {
|
|
282
|
+
COLOR,
|
|
283
|
+
CubeEngine
|
|
284
|
+
});
|