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/dist/index.mjs CHANGED
@@ -23,50 +23,15 @@ var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot
23
23
  var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
24
24
 
25
25
  // src/index.js
26
- 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;
26
+ 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;
27
27
  var CubeEngine = class {
28
- constructor(initialScramble = "") {
28
+ constructor(initialScramble = "", options = { size: 3 }) {
29
29
  __privateAdd(this, _CubeEngine_instances);
30
30
  __publicField(this, "MOVES", []);
31
- // States object for the rotation
32
- __publicField(this, "STATES", {
33
- UPPER: [
34
- // (White)
35
- [COLOR.W[0], COLOR.W[1], COLOR.W[2]],
36
- [COLOR.W[3], COLOR.W[4], COLOR.W[5]],
37
- [COLOR.W[6], COLOR.W[7], COLOR.W[8]]
38
- ],
39
- LEFT: [
40
- // (Orange)
41
- [COLOR.O[0], COLOR.O[1], COLOR.O[2]],
42
- [COLOR.O[3], COLOR.O[4], COLOR.O[5]],
43
- [COLOR.O[6], COLOR.O[7], COLOR.O[8]]
44
- ],
45
- FRONT: [
46
- // (Green)
47
- [COLOR.G[0], COLOR.G[1], COLOR.G[2]],
48
- [COLOR.G[3], COLOR.G[4], COLOR.G[5]],
49
- [COLOR.G[6], COLOR.G[7], COLOR.G[8]]
50
- ],
51
- RIGHT: [
52
- // (Red)
53
- [COLOR.R[0], COLOR.R[1], COLOR.R[2]],
54
- [COLOR.R[3], COLOR.R[4], COLOR.R[5]],
55
- [COLOR.R[6], COLOR.R[7], COLOR.R[8]]
56
- ],
57
- BACK: [
58
- // (Blue)
59
- [COLOR.B[0], COLOR.B[1], COLOR.B[2]],
60
- [COLOR.B[3], COLOR.B[4], COLOR.B[5]],
61
- [COLOR.B[6], COLOR.B[7], COLOR.B[8]]
62
- ],
63
- DOWN: [
64
- // (Yellow)
65
- [COLOR.Y[0], COLOR.Y[1], COLOR.Y[2]],
66
- [COLOR.Y[3], COLOR.Y[4], COLOR.Y[5]],
67
- [COLOR.Y[6], COLOR.Y[7], COLOR.Y[8]]
68
- ]
69
- });
31
+ __publicField(this, "size", 3);
32
+ const allowedSizes = [2, 3];
33
+ this.size = allowedSizes.includes(options.size) ? options.size : 3;
34
+ __privateMethod(this, _CubeEngine_instances, initializeState_fn).call(this);
70
35
  if (typeof initialScramble === "string" && initialScramble.trim().length > 0) {
71
36
  __privateMethod(this, _CubeEngine_instances, applyMovesFromString_fn).call(this, initialScramble, false);
72
37
  this.MOVES = [];
@@ -96,6 +61,18 @@ var CubeEngine = class {
96
61
  this.MOVES.push("F'");
97
62
  }
98
63
  }
64
+ /**
65
+ * Rotates the (BACK) layer clockwise or counterclockwise.
66
+ */
67
+ rotateB(clockwise = true) {
68
+ if (clockwise) {
69
+ __privateMethod(this, _CubeEngine_instances, rotateB_fn).call(this, true);
70
+ this.MOVES.push("B");
71
+ } else {
72
+ __privateMethod(this, _CubeEngine_instances, rotateB_fn).call(this, false);
73
+ this.MOVES.push("B'");
74
+ }
75
+ }
99
76
  /**
100
77
  * Rotates the (RIGHT) layer clockwise or counterclockwise.
101
78
  */
@@ -136,6 +113,7 @@ var CubeEngine = class {
136
113
  * Rotates the wide (DOWN two layers) clockwise or counterclockwise.
137
114
  */
138
115
  rotateDw(clockwise = true) {
116
+ if (this.size === 2) return;
139
117
  if (clockwise) {
140
118
  __privateMethod(this, _CubeEngine_instances, rotateDw_fn).call(this, true);
141
119
  this.MOVES.push("Dw");
@@ -148,6 +126,7 @@ var CubeEngine = class {
148
126
  * Rotates the wide (UPPER two layers) clockwise or counterclockwise.
149
127
  */
150
128
  rotateUw(clockwise = true) {
129
+ if (this.size === 2) return;
151
130
  if (clockwise) {
152
131
  __privateMethod(this, _CubeEngine_instances, rotateUw_fn).call(this, true);
153
132
  this.MOVES.push("Uw");
@@ -160,6 +139,7 @@ var CubeEngine = class {
160
139
  * Rotates the wide (RIGHT two layers) clockwise or counterclockwise.
161
140
  */
162
141
  rotateRw(clockwise = true) {
142
+ if (this.size === 2) return;
163
143
  if (clockwise) {
164
144
  __privateMethod(this, _CubeEngine_instances, rotateRw_fn).call(this, true);
165
145
  this.MOVES.push("Rw");
@@ -172,6 +152,7 @@ var CubeEngine = class {
172
152
  * Rotates the wide (LEFT two layers) clockwise or counterclockwise.
173
153
  */
174
154
  rotateLw(clockwise = true) {
155
+ if (this.size === 2) return;
175
156
  if (clockwise) {
176
157
  __privateMethod(this, _CubeEngine_instances, rotateLw_fn).call(this, true);
177
158
  this.MOVES.push("Lw");
@@ -184,6 +165,7 @@ var CubeEngine = class {
184
165
  * Rotates the middle slice (M) parallel to L/R. Clockwise corresponds to Lw followed by L'.
185
166
  */
186
167
  rotateM(clockwise = true) {
168
+ if (this.size === 2) return;
187
169
  if (clockwise) {
188
170
  __privateMethod(this, _CubeEngine_instances, rotateM_fn).call(this, true);
189
171
  this.MOVES.push("M");
@@ -240,12 +222,11 @@ var CubeEngine = class {
240
222
  isSolved() {
241
223
  const temp = __spreadValues({}, this.STATES);
242
224
  const layersSolved = Object.keys(temp).map((layer) => {
243
- const mixedMatrix = [
244
- ...temp[layer][0],
245
- ...temp[layer][1],
246
- ...temp[layer][2]
247
- ];
248
- const centerColor = mixedMatrix[4];
225
+ let mixedMatrix = [];
226
+ for (let i = 0; i < this.size; i++) {
227
+ mixedMatrix = [...mixedMatrix, ...temp[layer][i]];
228
+ }
229
+ const centerColor = this.size === 2 ? mixedMatrix[0] : mixedMatrix[4];
249
230
  return mixedMatrix.every((currentColor) => currentColor === centerColor);
250
231
  });
251
232
  return layersSolved.every((isLayerSolved) => isLayerSolved);
@@ -263,38 +244,7 @@ var CubeEngine = class {
263
244
  * Resets the cube to the solved state and clears the move history.
264
245
  */
265
246
  reset() {
266
- this.STATES = {
267
- UPPER: [
268
- [COLOR.W[0], COLOR.W[1], COLOR.W[2]],
269
- [COLOR.W[3], COLOR.W[4], COLOR.W[5]],
270
- [COLOR.W[6], COLOR.W[7], COLOR.W[8]]
271
- ],
272
- LEFT: [
273
- [COLOR.O[0], COLOR.O[1], COLOR.O[2]],
274
- [COLOR.O[3], COLOR.O[4], COLOR.O[5]],
275
- [COLOR.O[6], COLOR.O[7], COLOR.O[8]]
276
- ],
277
- FRONT: [
278
- [COLOR.G[0], COLOR.G[1], COLOR.G[2]],
279
- [COLOR.G[3], COLOR.G[4], COLOR.G[5]],
280
- [COLOR.G[6], COLOR.G[7], COLOR.G[8]]
281
- ],
282
- RIGHT: [
283
- [COLOR.R[0], COLOR.R[1], COLOR.R[2]],
284
- [COLOR.R[3], COLOR.R[4], COLOR.R[5]],
285
- [COLOR.R[6], COLOR.R[7], COLOR.R[8]]
286
- ],
287
- BACK: [
288
- [COLOR.B[0], COLOR.B[1], COLOR.B[2]],
289
- [COLOR.B[3], COLOR.B[4], COLOR.B[5]],
290
- [COLOR.B[6], COLOR.B[7], COLOR.B[8]]
291
- ],
292
- DOWN: [
293
- [COLOR.Y[0], COLOR.Y[1], COLOR.Y[2]],
294
- [COLOR.Y[3], COLOR.Y[4], COLOR.Y[5]],
295
- [COLOR.Y[6], COLOR.Y[7], COLOR.Y[8]]
296
- ]
297
- };
247
+ __privateMethod(this, _CubeEngine_instances, initializeState_fn).call(this);
298
248
  this.MOVES = [];
299
249
  }
300
250
  /**
@@ -309,6 +259,28 @@ var CubeEngine = class {
309
259
  }
310
260
  };
311
261
  _CubeEngine_instances = new WeakSet();
262
+ initializeState_fn = function() {
263
+ this.STATES = {
264
+ UPPER: __privateMethod(this, _CubeEngine_instances, createFace_fn).call(this, "W"),
265
+ LEFT: __privateMethod(this, _CubeEngine_instances, createFace_fn).call(this, "O"),
266
+ FRONT: __privateMethod(this, _CubeEngine_instances, createFace_fn).call(this, "G"),
267
+ RIGHT: __privateMethod(this, _CubeEngine_instances, createFace_fn).call(this, "R"),
268
+ BACK: __privateMethod(this, _CubeEngine_instances, createFace_fn).call(this, "B"),
269
+ DOWN: __privateMethod(this, _CubeEngine_instances, createFace_fn).call(this, "Y")
270
+ };
271
+ };
272
+ // Create a face matrix based on cube size
273
+ createFace_fn = function(color) {
274
+ const face = [];
275
+ for (let i = 0; i < this.size; i++) {
276
+ const row = [];
277
+ for (let j = 0; j < this.size; j++) {
278
+ row.push(color);
279
+ }
280
+ face.push(row);
281
+ }
282
+ return face;
283
+ };
312
284
  rotateU_fn = function(clockwise = true) {
313
285
  if (clockwise) {
314
286
  this.STATES.UPPER = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, this.STATES.UPPER, true);
@@ -343,6 +315,17 @@ rotateF_fn = function(clockwise = true) {
343
315
  __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, false);
344
316
  }
345
317
  };
318
+ rotateB_fn = function(clockwise = true) {
319
+ __privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, true);
320
+ __privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, true);
321
+ if (clockwise) {
322
+ __privateMethod(this, _CubeEngine_instances, rotateF_fn).call(this, true);
323
+ } else {
324
+ __privateMethod(this, _CubeEngine_instances, rotateF_fn).call(this, false);
325
+ }
326
+ __privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, false);
327
+ __privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, false);
328
+ };
346
329
  rotateR_fn = function(clockwise = true) {
347
330
  if (clockwise) {
348
331
  __privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, true);
@@ -501,19 +484,37 @@ rotateY_fn = function(clockwise = true) {
501
484
  */
502
485
  switchMatrix_fn = function(matrix, clockwise = true) {
503
486
  const clone = structuredClone(matrix);
504
- const tempMatrix = [...clone[0], ...clone[1], ...clone[2]];
505
- if (clockwise) {
506
- return [
507
- [tempMatrix[6], tempMatrix[3], tempMatrix[0]],
508
- [tempMatrix[7], tempMatrix[4], tempMatrix[1]],
509
- [tempMatrix[8], tempMatrix[5], tempMatrix[2]]
510
- ];
487
+ const size = this.size;
488
+ let tempMatrix = [];
489
+ for (let i = 0; i < size; i++) {
490
+ tempMatrix = [...tempMatrix, ...clone[i]];
491
+ }
492
+ if (size === 2) {
493
+ if (clockwise) {
494
+ return [
495
+ [tempMatrix[2], tempMatrix[0]],
496
+ [tempMatrix[3], tempMatrix[1]]
497
+ ];
498
+ } else {
499
+ return [
500
+ [tempMatrix[1], tempMatrix[3]],
501
+ [tempMatrix[0], tempMatrix[2]]
502
+ ];
503
+ }
511
504
  } else {
512
- return [
513
- [tempMatrix[2], tempMatrix[5], tempMatrix[8]],
514
- [tempMatrix[1], tempMatrix[4], tempMatrix[7]],
515
- [tempMatrix[0], tempMatrix[3], tempMatrix[6]]
516
- ];
505
+ if (clockwise) {
506
+ return [
507
+ [tempMatrix[6], tempMatrix[3], tempMatrix[0]],
508
+ [tempMatrix[7], tempMatrix[4], tempMatrix[1]],
509
+ [tempMatrix[8], tempMatrix[5], tempMatrix[2]]
510
+ ];
511
+ } else {
512
+ return [
513
+ [tempMatrix[2], tempMatrix[5], tempMatrix[8]],
514
+ [tempMatrix[1], tempMatrix[4], tempMatrix[7]],
515
+ [tempMatrix[0], tempMatrix[3], tempMatrix[6]]
516
+ ];
517
+ }
517
518
  }
518
519
  };
519
520
  specialFlip_fn = function(matrix) {
@@ -528,7 +529,6 @@ applyMovesFromString_fn = function(sequence, record = true) {
528
529
  const rest = token.slice(1);
529
530
  const isDouble = rest.includes("2");
530
531
  const isPrime = rest.includes("'");
531
- const times = isDouble ? 2 : 1;
532
532
  const exec = (fnClockwise, fnCounter) => {
533
533
  if (isDouble) {
534
534
  fnClockwise();
@@ -612,6 +612,12 @@ applyMovesFromString_fn = function(sequence, record = true) {
612
612
  () => record ? this.rotateF(false) : __privateMethod(this, _CubeEngine_instances, rotateF_fn).call(this, false)
613
613
  );
614
614
  break;
615
+ case "B":
616
+ exec(
617
+ () => record ? this.rotateB(true) : __privateMethod(this, _CubeEngine_instances, rotateB_fn).call(this, true),
618
+ () => record ? this.rotateB(false) : __privateMethod(this, _CubeEngine_instances, rotateB_fn).call(this, false)
619
+ );
620
+ break;
615
621
  case "x":
616
622
  exec(
617
623
  () => record ? this.rotateX(true) : __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, true),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cube-state-engine",
3
- "version": "1.2.0",
3
+ "version": "1.4.0",
4
4
  "description": "An efficient representation in memory for tracking the Rubik's cube state on each movement.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -1,12 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <module type="WEB_MODULE" version="4">
3
- <component name="NewModuleRootManager">
4
- <content url="file://$MODULE_DIR$">
5
- <excludeFolder url="file://$MODULE_DIR$/.tmp" />
6
- <excludeFolder url="file://$MODULE_DIR$/temp" />
7
- <excludeFolder url="file://$MODULE_DIR$/tmp" />
8
- </content>
9
- <orderEntry type="inheritedJdk" />
10
- <orderEntry type="sourceFolder" forTests="false" />
11
- </component>
12
- </module>
package/.idea/modules.xml DELETED
@@ -1,8 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="ProjectModuleManager">
4
- <modules>
5
- <module fileurl="file://$PROJECT_DIR$/.idea/cube-state-engine.iml" filepath="$PROJECT_DIR$/.idea/cube-state-engine.iml" />
6
- </modules>
7
- </component>
8
- </project>
package/.idea/vcs.xml DELETED
@@ -1,6 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="VcsDirectoryMappings">
4
- <mapping directory="" vcs="Git" />
5
- </component>
6
- </project>