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/dist/index.js ADDED
@@ -0,0 +1,364 @@
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, rotateU_fn, rotateF_fn, rotateR_fn, rotateL_fn, rotateD_fn, rotateX_fn, rotateY_fn, switchMatrix_fn, specialFlip_fn;
49
+ var CubeEngine = class {
50
+ constructor() {
51
+ __privateAdd(this, _CubeEngine_instances);
52
+ __publicField(this, "MOVES", []);
53
+ // States object for the rotation
54
+ __publicField(this, "STATES", {
55
+ UPPER: [
56
+ // (White)
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
+ });
92
+ }
93
+ /**
94
+ * Rotates the (UPPER) layer clockwise or counterclockwise.
95
+ */
96
+ rotateU(clockwise = true) {
97
+ if (clockwise) {
98
+ __privateMethod(this, _CubeEngine_instances, rotateU_fn).call(this, true);
99
+ this.MOVES.push("U");
100
+ } else {
101
+ __privateMethod(this, _CubeEngine_instances, rotateU_fn).call(this, false);
102
+ this.MOVES.push("U'");
103
+ }
104
+ }
105
+ /**
106
+ * Rotates the (FRONT) layer clockwise or counterclockwise.
107
+ */
108
+ rotateF(clockwise = true) {
109
+ if (clockwise) {
110
+ __privateMethod(this, _CubeEngine_instances, rotateF_fn).call(this, true);
111
+ this.MOVES.push("F");
112
+ } else {
113
+ __privateMethod(this, _CubeEngine_instances, rotateF_fn).call(this, false);
114
+ this.MOVES.push("F'");
115
+ }
116
+ }
117
+ /**
118
+ * Rotates the (RIGHT) layer clockwise or counterclockwise.
119
+ */
120
+ rotateR(clockwise = true) {
121
+ if (clockwise) {
122
+ __privateMethod(this, _CubeEngine_instances, rotateR_fn).call(this, true);
123
+ this.MOVES.push("R");
124
+ } else {
125
+ __privateMethod(this, _CubeEngine_instances, rotateR_fn).call(this, false);
126
+ this.MOVES.push("R'");
127
+ }
128
+ }
129
+ /**
130
+ * Rotates the (LEFT) layer clockwise or counterclockwise.
131
+ */
132
+ rotateL(clockwise = true) {
133
+ if (clockwise) {
134
+ __privateMethod(this, _CubeEngine_instances, rotateL_fn).call(this, true);
135
+ this.MOVES.push("L");
136
+ } else {
137
+ __privateMethod(this, _CubeEngine_instances, rotateL_fn).call(this, false);
138
+ this.MOVES.push("L'");
139
+ }
140
+ }
141
+ /**
142
+ * Rotates the (DOWN) layer clockwise or counterclockwise.
143
+ */
144
+ rotateD(clockwise = true) {
145
+ if (clockwise) {
146
+ __privateMethod(this, _CubeEngine_instances, rotateD_fn).call(this, true);
147
+ this.MOVES.push("D");
148
+ } else {
149
+ __privateMethod(this, _CubeEngine_instances, rotateD_fn).call(this, false);
150
+ this.MOVES.push("D'");
151
+ }
152
+ }
153
+ /**
154
+ * Rotates the (x) axis clockwise or counterclockwise.
155
+ */
156
+ rotateX(clockwise = true) {
157
+ if (clockwise) {
158
+ __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, true);
159
+ this.MOVES.push("x");
160
+ } else {
161
+ __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, false);
162
+ this.MOVES.push("x'");
163
+ }
164
+ }
165
+ /**
166
+ * Rotates the (y) axis clockwise or counterclockwise.
167
+ */
168
+ rotateY(clockwise = true) {
169
+ if (clockwise) {
170
+ __privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, true);
171
+ this.MOVES.push("y");
172
+ } else {
173
+ __privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, false);
174
+ this.MOVES.push("y'");
175
+ }
176
+ }
177
+ /**
178
+ * Logs the current state of the cube.
179
+ */
180
+ state() {
181
+ return __spreadValues({}, this.STATES);
182
+ }
183
+ /**
184
+ * Indicates if the cube is solve or not in all layers.
185
+ */
186
+ isSolved() {
187
+ const temp = __spreadValues({}, this.STATES);
188
+ const layersSolved = Object.keys(temp).map((layer) => {
189
+ const mixedMatrix = [
190
+ ...temp[layer][0],
191
+ ...temp[layer][1],
192
+ ...temp[layer][2]
193
+ ];
194
+ const centerColor = mixedMatrix[4];
195
+ return mixedMatrix.every((currentColor) => currentColor === centerColor);
196
+ });
197
+ return layersSolved.every((isLayerSolved) => isLayerSolved);
198
+ }
199
+ /**
200
+ * Returns the history of all movements made.
201
+ *
202
+ * @param {boolean} asString - If true, returns the history as a string; otherwise, returns it as an array.
203
+ * @returns {string|array} The history of movements as an array or string.
204
+ */
205
+ getMoves(asString = true) {
206
+ return asString ? this.MOVES : this.MOVES.join(" ");
207
+ }
208
+ };
209
+ _CubeEngine_instances = new WeakSet();
210
+ rotateU_fn = function(clockwise = true) {
211
+ if (clockwise) {
212
+ this.STATES.UPPER = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, this.STATES.UPPER, true);
213
+ const tempFront = [...this.STATES.FRONT[0]];
214
+ const tempRight = [...this.STATES.RIGHT[0]];
215
+ const tempLeft = [...this.STATES.LEFT[0]];
216
+ const tempBack = [...this.STATES.BACK[0]];
217
+ this.STATES.FRONT[0] = [...tempRight];
218
+ this.STATES.LEFT[0] = [...tempFront];
219
+ this.STATES.BACK[0] = [...tempLeft];
220
+ this.STATES.RIGHT[0] = [...tempBack];
221
+ } else {
222
+ this.STATES.UPPER = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, this.STATES.UPPER, false);
223
+ const tempFront = [...this.STATES.FRONT[0]];
224
+ const tempRight = [...this.STATES.RIGHT[0]];
225
+ const tempLeft = [...this.STATES.LEFT[0]];
226
+ const tempBack = [...this.STATES.BACK[0]];
227
+ this.STATES.FRONT[0] = [...tempLeft];
228
+ this.STATES.LEFT[0] = [...tempBack];
229
+ this.STATES.BACK[0] = [...tempRight];
230
+ this.STATES.RIGHT[0] = [...tempFront];
231
+ }
232
+ };
233
+ rotateF_fn = function(clockwise = true) {
234
+ if (clockwise) {
235
+ __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, true);
236
+ __privateMethod(this, _CubeEngine_instances, rotateU_fn).call(this, true);
237
+ __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, false);
238
+ } else {
239
+ __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, true);
240
+ __privateMethod(this, _CubeEngine_instances, rotateU_fn).call(this, false);
241
+ __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, false);
242
+ }
243
+ };
244
+ rotateR_fn = function(clockwise = true) {
245
+ if (clockwise) {
246
+ __privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, true);
247
+ __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, true);
248
+ __privateMethod(this, _CubeEngine_instances, rotateU_fn).call(this, true);
249
+ __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, false);
250
+ __privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, false);
251
+ } else {
252
+ __privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, true);
253
+ __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, true);
254
+ __privateMethod(this, _CubeEngine_instances, rotateU_fn).call(this, false);
255
+ __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, false);
256
+ __privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, false);
257
+ }
258
+ };
259
+ rotateL_fn = function(clockwise = true) {
260
+ if (clockwise) {
261
+ __privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, false);
262
+ __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, true);
263
+ __privateMethod(this, _CubeEngine_instances, rotateU_fn).call(this, true);
264
+ __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, false);
265
+ __privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, true);
266
+ } else {
267
+ __privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, false);
268
+ __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, true);
269
+ __privateMethod(this, _CubeEngine_instances, rotateU_fn).call(this, false);
270
+ __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, false);
271
+ __privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, true);
272
+ }
273
+ };
274
+ rotateD_fn = function(clockwise = true) {
275
+ if (clockwise) {
276
+ __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, true);
277
+ __privateMethod(this, _CubeEngine_instances, rotateF_fn).call(this, true);
278
+ __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, false);
279
+ } else {
280
+ __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, true);
281
+ __privateMethod(this, _CubeEngine_instances, rotateF_fn).call(this, false);
282
+ __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, false);
283
+ }
284
+ };
285
+ rotateX_fn = function(clockwise = true) {
286
+ const tempFront = structuredClone(this.STATES.FRONT);
287
+ const tempDown = structuredClone(this.STATES.DOWN);
288
+ const tempUpper = structuredClone(this.STATES.UPPER);
289
+ const tempBack = structuredClone(this.STATES.BACK);
290
+ const tempLeft = structuredClone(this.STATES.LEFT);
291
+ const tempRight = structuredClone(this.STATES.RIGHT);
292
+ if (clockwise) {
293
+ this.STATES.LEFT = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempLeft, false);
294
+ this.STATES.RIGHT = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempRight, true);
295
+ this.STATES.FRONT = [...tempDown];
296
+ this.STATES.UPPER = [...tempFront];
297
+ this.STATES.BACK = __privateMethod(this, _CubeEngine_instances, specialFlip_fn).call(this, tempUpper);
298
+ this.STATES.DOWN = __privateMethod(this, _CubeEngine_instances, specialFlip_fn).call(this, tempBack);
299
+ } else {
300
+ this.STATES.LEFT = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempLeft, true);
301
+ this.STATES.RIGHT = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempRight, false);
302
+ this.STATES.FRONT = [...tempUpper];
303
+ this.STATES.DOWN = [...tempFront];
304
+ this.STATES.BACK = __privateMethod(this, _CubeEngine_instances, specialFlip_fn).call(this, tempDown);
305
+ this.STATES.UPPER = __privateMethod(this, _CubeEngine_instances, specialFlip_fn).call(this, tempBack);
306
+ }
307
+ };
308
+ rotateY_fn = function(clockwise = true) {
309
+ const tempFront = structuredClone(this.STATES.FRONT);
310
+ const tempRight = structuredClone(this.STATES.RIGHT);
311
+ const tempBack = structuredClone(this.STATES.BACK);
312
+ const tempLeft = structuredClone(this.STATES.LEFT);
313
+ if (clockwise) {
314
+ this.STATES.UPPER = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, this.STATES.UPPER, true);
315
+ this.STATES.DOWN = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, this.STATES.DOWN, false);
316
+ this.STATES.FRONT = [...tempRight];
317
+ this.STATES.RIGHT = [...tempBack];
318
+ this.STATES.LEFT = [...tempFront];
319
+ this.STATES.BACK = [...tempLeft];
320
+ } else {
321
+ this.STATES.UPPER = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, this.STATES.UPPER, false);
322
+ this.STATES.DOWN = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, this.STATES.DOWN, true);
323
+ this.STATES.FRONT = [...tempLeft];
324
+ this.STATES.RIGHT = [...tempFront];
325
+ this.STATES.LEFT = [...tempBack];
326
+ this.STATES.BACK = [...tempRight];
327
+ }
328
+ };
329
+ /**
330
+ * Rotate the entire face in the direction set
331
+ */
332
+ switchMatrix_fn = function(matrix, clockwise = true) {
333
+ const clone = structuredClone(matrix);
334
+ const tempMatrix = [...clone[0], ...clone[1], ...clone[2]];
335
+ if (clockwise) {
336
+ return [
337
+ [tempMatrix[6], tempMatrix[3], tempMatrix[0]],
338
+ [tempMatrix[7], tempMatrix[4], tempMatrix[1]],
339
+ [tempMatrix[8], tempMatrix[5], tempMatrix[2]]
340
+ ];
341
+ } else {
342
+ return [
343
+ [tempMatrix[2], tempMatrix[5], tempMatrix[8]],
344
+ [tempMatrix[1], tempMatrix[4], tempMatrix[7]],
345
+ [tempMatrix[0], tempMatrix[3], tempMatrix[6]]
346
+ ];
347
+ }
348
+ };
349
+ specialFlip_fn = function(matrix) {
350
+ return structuredClone(matrix).reverse().map((row) => [...row].reverse());
351
+ };
352
+ var COLOR = {
353
+ W: ["W", "W", "W", "W", "W", "W", "W", "W", "W"],
354
+ G: ["G", "G", "G", "G", "G", "G", "G", "G", "G"],
355
+ R: ["R", "R", "R", "R", "R", "R", "R", "R", "R"],
356
+ B: ["B", "B", "B", "B", "B", "B", "B", "B", "B"],
357
+ O: ["O", "O", "O", "O", "O", "O", "O", "O", "O"],
358
+ Y: ["Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y"]
359
+ };
360
+ // Annotate the CommonJS export names for ESM import in node:
361
+ 0 && (module.exports = {
362
+ COLOR,
363
+ CubeEngine
364
+ });