cube-state-engine 1.0.5 → 1.2.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.js CHANGED
@@ -45,9 +45,9 @@ __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, rotateX_fn, rotateY_fn, switchMatrix_fn, specialFlip_fn;
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;
49
49
  var CubeEngine = class {
50
- constructor() {
50
+ constructor(initialScramble = "") {
51
51
  __privateAdd(this, _CubeEngine_instances);
52
52
  __publicField(this, "MOVES", []);
53
53
  // States object for the rotation
@@ -89,6 +89,10 @@ var CubeEngine = class {
89
89
  [COLOR.Y[6], COLOR.Y[7], COLOR.Y[8]]
90
90
  ]
91
91
  });
92
+ if (typeof initialScramble === "string" && initialScramble.trim().length > 0) {
93
+ __privateMethod(this, _CubeEngine_instances, applyMovesFromString_fn).call(this, initialScramble, false);
94
+ this.MOVES = [];
95
+ }
92
96
  }
93
97
  /**
94
98
  * Rotates the (UPPER) layer clockwise or counterclockwise.
@@ -150,6 +154,66 @@ var CubeEngine = class {
150
154
  this.MOVES.push("D'");
151
155
  }
152
156
  }
157
+ /**
158
+ * Rotates the wide (DOWN two layers) clockwise or counterclockwise.
159
+ */
160
+ rotateDw(clockwise = true) {
161
+ if (clockwise) {
162
+ __privateMethod(this, _CubeEngine_instances, rotateDw_fn).call(this, true);
163
+ this.MOVES.push("Dw");
164
+ } else {
165
+ __privateMethod(this, _CubeEngine_instances, rotateDw_fn).call(this, false);
166
+ this.MOVES.push("Dw'");
167
+ }
168
+ }
169
+ /**
170
+ * Rotates the wide (UPPER two layers) clockwise or counterclockwise.
171
+ */
172
+ rotateUw(clockwise = true) {
173
+ if (clockwise) {
174
+ __privateMethod(this, _CubeEngine_instances, rotateUw_fn).call(this, true);
175
+ this.MOVES.push("Uw");
176
+ } else {
177
+ __privateMethod(this, _CubeEngine_instances, rotateUw_fn).call(this, false);
178
+ this.MOVES.push("Uw'");
179
+ }
180
+ }
181
+ /**
182
+ * Rotates the wide (RIGHT two layers) clockwise or counterclockwise.
183
+ */
184
+ rotateRw(clockwise = true) {
185
+ if (clockwise) {
186
+ __privateMethod(this, _CubeEngine_instances, rotateRw_fn).call(this, true);
187
+ this.MOVES.push("Rw");
188
+ } else {
189
+ __privateMethod(this, _CubeEngine_instances, rotateRw_fn).call(this, false);
190
+ this.MOVES.push("Rw'");
191
+ }
192
+ }
193
+ /**
194
+ * Rotates the wide (LEFT two layers) clockwise or counterclockwise.
195
+ */
196
+ rotateLw(clockwise = true) {
197
+ if (clockwise) {
198
+ __privateMethod(this, _CubeEngine_instances, rotateLw_fn).call(this, true);
199
+ this.MOVES.push("Lw");
200
+ } else {
201
+ __privateMethod(this, _CubeEngine_instances, rotateLw_fn).call(this, false);
202
+ this.MOVES.push("Lw'");
203
+ }
204
+ }
205
+ /**
206
+ * Rotates the middle slice (M) parallel to L/R. Clockwise corresponds to Lw followed by L'.
207
+ */
208
+ rotateM(clockwise = true) {
209
+ if (clockwise) {
210
+ __privateMethod(this, _CubeEngine_instances, rotateM_fn).call(this, true);
211
+ this.MOVES.push("M");
212
+ } else {
213
+ __privateMethod(this, _CubeEngine_instances, rotateM_fn).call(this, false);
214
+ this.MOVES.push("M'");
215
+ }
216
+ }
153
217
  /**
154
218
  * Rotates the (x) axis clockwise or counterclockwise.
155
219
  */
@@ -162,6 +226,18 @@ var CubeEngine = class {
162
226
  this.MOVES.push("x'");
163
227
  }
164
228
  }
229
+ /**
230
+ * Rotates the (z) axis clockwise or counterclockwise.
231
+ */
232
+ rotateZ(clockwise = true) {
233
+ if (clockwise) {
234
+ __privateMethod(this, _CubeEngine_instances, rotateZ_fn).call(this, true);
235
+ this.MOVES.push("z");
236
+ } else {
237
+ __privateMethod(this, _CubeEngine_instances, rotateZ_fn).call(this, false);
238
+ this.MOVES.push("z'");
239
+ }
240
+ }
165
241
  /**
166
242
  * Rotates the (y) axis clockwise or counterclockwise.
167
243
  */
@@ -205,6 +281,54 @@ var CubeEngine = class {
205
281
  getMoves(asString = true) {
206
282
  return asString ? this.MOVES.join(" ") : this.MOVES;
207
283
  }
284
+ /**
285
+ * Resets the cube to the solved state and clears the move history.
286
+ */
287
+ reset() {
288
+ this.STATES = {
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
+ };
320
+ this.MOVES = [];
321
+ }
322
+ /**
323
+ * Applies a sequence of moves provided as a string.
324
+ * Supports: U, D, L, R, F, x, y, z; slice moves: M; and wide moves: Dw, Uw, Rw, Lw with optional ' for counterclockwise and 2 for double turns.
325
+ * @param {string} sequence - e.g. "R U' F R2 D Dw Uw Rw Rw' Lw Lw2 M M' M2"
326
+ * @param {object} options - { record: boolean } whether to record moves in history (default true)
327
+ */
328
+ applyMoves(sequence, options = { record: false }) {
329
+ const record = (options == null ? void 0 : options.record) !== false;
330
+ __privateMethod(this, _CubeEngine_instances, applyMovesFromString_fn).call(this, sequence, record);
331
+ }
208
332
  };
209
333
  _CubeEngine_instances = new WeakSet();
210
334
  rotateU_fn = function(clockwise = true) {
@@ -282,6 +406,51 @@ rotateD_fn = function(clockwise = true) {
282
406
  __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, false);
283
407
  }
284
408
  };
409
+ rotateDw_fn = function(clockwise = true) {
410
+ if (clockwise) {
411
+ __privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, false);
412
+ __privateMethod(this, _CubeEngine_instances, rotateU_fn).call(this, true);
413
+ } else {
414
+ __privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, true);
415
+ __privateMethod(this, _CubeEngine_instances, rotateU_fn).call(this, false);
416
+ }
417
+ };
418
+ rotateUw_fn = function(clockwise = true) {
419
+ if (clockwise) {
420
+ __privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, true);
421
+ __privateMethod(this, _CubeEngine_instances, rotateD_fn).call(this, true);
422
+ } else {
423
+ __privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, false);
424
+ __privateMethod(this, _CubeEngine_instances, rotateD_fn).call(this, false);
425
+ }
426
+ };
427
+ rotateRw_fn = function(clockwise = true) {
428
+ if (clockwise) {
429
+ __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, true);
430
+ __privateMethod(this, _CubeEngine_instances, rotateL_fn).call(this, true);
431
+ } else {
432
+ __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, false);
433
+ __privateMethod(this, _CubeEngine_instances, rotateL_fn).call(this, false);
434
+ }
435
+ };
436
+ rotateLw_fn = function(clockwise = true) {
437
+ if (clockwise) {
438
+ __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, false);
439
+ __privateMethod(this, _CubeEngine_instances, rotateR_fn).call(this, true);
440
+ } else {
441
+ __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, true);
442
+ __privateMethod(this, _CubeEngine_instances, rotateR_fn).call(this, false);
443
+ }
444
+ };
445
+ rotateM_fn = function(clockwise = true) {
446
+ if (clockwise) {
447
+ __privateMethod(this, _CubeEngine_instances, rotateLw_fn).call(this, true);
448
+ __privateMethod(this, _CubeEngine_instances, rotateL_fn).call(this, false);
449
+ } else {
450
+ __privateMethod(this, _CubeEngine_instances, rotateLw_fn).call(this, false);
451
+ __privateMethod(this, _CubeEngine_instances, rotateL_fn).call(this, true);
452
+ }
453
+ };
285
454
  rotateX_fn = function(clockwise = true) {
286
455
  const tempFront = structuredClone(this.STATES.FRONT);
287
456
  const tempDown = structuredClone(this.STATES.DOWN);
@@ -305,6 +474,29 @@ rotateX_fn = function(clockwise = true) {
305
474
  this.STATES.UPPER = __privateMethod(this, _CubeEngine_instances, specialFlip_fn).call(this, tempBack);
306
475
  }
307
476
  };
477
+ rotateZ_fn = function(clockwise = true) {
478
+ const tempUpper = structuredClone(this.STATES.UPPER);
479
+ const tempRight = structuredClone(this.STATES.RIGHT);
480
+ const tempDown = structuredClone(this.STATES.DOWN);
481
+ const tempLeft = structuredClone(this.STATES.LEFT);
482
+ const tempFront = structuredClone(this.STATES.FRONT);
483
+ const tempBack = structuredClone(this.STATES.BACK);
484
+ if (clockwise) {
485
+ this.STATES.FRONT = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempFront, true);
486
+ this.STATES.BACK = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempBack, false);
487
+ this.STATES.RIGHT = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempUpper, true);
488
+ this.STATES.DOWN = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempRight, true);
489
+ this.STATES.LEFT = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempDown, true);
490
+ this.STATES.UPPER = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempLeft, true);
491
+ } else {
492
+ this.STATES.FRONT = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempFront, false);
493
+ this.STATES.BACK = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempBack, true);
494
+ this.STATES.RIGHT = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempDown, false);
495
+ this.STATES.DOWN = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempLeft, false);
496
+ this.STATES.LEFT = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempUpper, false);
497
+ this.STATES.UPPER = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempRight, false);
498
+ }
499
+ };
308
500
  rotateY_fn = function(clockwise = true) {
309
501
  const tempFront = structuredClone(this.STATES.FRONT);
310
502
  const tempRight = structuredClone(this.STATES.RIGHT);
@@ -349,6 +541,128 @@ switchMatrix_fn = function(matrix, clockwise = true) {
349
541
  specialFlip_fn = function(matrix) {
350
542
  return structuredClone(matrix).reverse().map((row) => [...row].reverse());
351
543
  };
544
+ // Internal: parses and applies moves. If record=false, uses private methods to avoid logging.
545
+ applyMovesFromString_fn = function(sequence, record = true) {
546
+ if (typeof sequence !== "string") return;
547
+ const tokens = sequence.split(/\s+/).map((t) => t.trim()).filter((t) => t.length > 0);
548
+ for (const token of tokens) {
549
+ const base = token[0];
550
+ const rest = token.slice(1);
551
+ const isDouble = rest.includes("2");
552
+ const isPrime = rest.includes("'");
553
+ const times = isDouble ? 2 : 1;
554
+ const exec = (fnClockwise, fnCounter) => {
555
+ if (isDouble) {
556
+ fnClockwise();
557
+ fnClockwise();
558
+ } else {
559
+ if (isPrime) {
560
+ fnCounter();
561
+ } else {
562
+ fnClockwise();
563
+ }
564
+ }
565
+ };
566
+ switch (base) {
567
+ case "U":
568
+ {
569
+ const isWide = /w/i.test(rest);
570
+ if (isWide) {
571
+ exec(
572
+ () => record ? this.rotateUw(true) : __privateMethod(this, _CubeEngine_instances, rotateUw_fn).call(this, true),
573
+ () => record ? this.rotateUw(false) : __privateMethod(this, _CubeEngine_instances, rotateUw_fn).call(this, false)
574
+ );
575
+ } else {
576
+ exec(
577
+ () => record ? this.rotateU(true) : __privateMethod(this, _CubeEngine_instances, rotateU_fn).call(this, true),
578
+ () => record ? this.rotateU(false) : __privateMethod(this, _CubeEngine_instances, rotateU_fn).call(this, false)
579
+ );
580
+ }
581
+ }
582
+ break;
583
+ case "D":
584
+ {
585
+ const isWide = /w/i.test(rest);
586
+ if (isWide) {
587
+ exec(
588
+ () => record ? this.rotateDw(true) : __privateMethod(this, _CubeEngine_instances, rotateDw_fn).call(this, true),
589
+ () => record ? this.rotateDw(false) : __privateMethod(this, _CubeEngine_instances, rotateDw_fn).call(this, false)
590
+ );
591
+ } else {
592
+ exec(
593
+ () => record ? this.rotateD(true) : __privateMethod(this, _CubeEngine_instances, rotateD_fn).call(this, true),
594
+ () => record ? this.rotateD(false) : __privateMethod(this, _CubeEngine_instances, rotateD_fn).call(this, false)
595
+ );
596
+ }
597
+ }
598
+ break;
599
+ case "L":
600
+ {
601
+ const isWide = /w/i.test(rest);
602
+ if (isWide) {
603
+ exec(
604
+ () => record ? this.rotateLw(true) : __privateMethod(this, _CubeEngine_instances, rotateLw_fn).call(this, true),
605
+ () => record ? this.rotateLw(false) : __privateMethod(this, _CubeEngine_instances, rotateLw_fn).call(this, false)
606
+ );
607
+ } else {
608
+ exec(
609
+ () => record ? this.rotateL(true) : __privateMethod(this, _CubeEngine_instances, rotateL_fn).call(this, true),
610
+ () => record ? this.rotateL(false) : __privateMethod(this, _CubeEngine_instances, rotateL_fn).call(this, false)
611
+ );
612
+ }
613
+ }
614
+ break;
615
+ case "R":
616
+ {
617
+ const isWide = /w/i.test(rest);
618
+ if (isWide) {
619
+ exec(
620
+ () => record ? this.rotateRw(true) : __privateMethod(this, _CubeEngine_instances, rotateRw_fn).call(this, true),
621
+ () => record ? this.rotateRw(false) : __privateMethod(this, _CubeEngine_instances, rotateRw_fn).call(this, false)
622
+ );
623
+ } else {
624
+ exec(
625
+ () => record ? this.rotateR(true) : __privateMethod(this, _CubeEngine_instances, rotateR_fn).call(this, true),
626
+ () => record ? this.rotateR(false) : __privateMethod(this, _CubeEngine_instances, rotateR_fn).call(this, false)
627
+ );
628
+ }
629
+ }
630
+ break;
631
+ case "F":
632
+ exec(
633
+ () => record ? this.rotateF(true) : __privateMethod(this, _CubeEngine_instances, rotateF_fn).call(this, true),
634
+ () => record ? this.rotateF(false) : __privateMethod(this, _CubeEngine_instances, rotateF_fn).call(this, false)
635
+ );
636
+ break;
637
+ case "x":
638
+ exec(
639
+ () => record ? this.rotateX(true) : __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, true),
640
+ () => record ? this.rotateX(false) : __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, false)
641
+ );
642
+ break;
643
+ case "y":
644
+ exec(
645
+ () => record ? this.rotateY(true) : __privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, true),
646
+ () => record ? this.rotateY(false) : __privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, false)
647
+ );
648
+ break;
649
+ case "z":
650
+ exec(
651
+ () => record ? this.rotateZ(true) : __privateMethod(this, _CubeEngine_instances, rotateZ_fn).call(this, true),
652
+ () => record ? this.rotateZ(false) : __privateMethod(this, _CubeEngine_instances, rotateZ_fn).call(this, false)
653
+ );
654
+ break;
655
+ case "M":
656
+ exec(
657
+ () => record ? this.rotateM(true) : __privateMethod(this, _CubeEngine_instances, rotateM_fn).call(this, true),
658
+ () => record ? this.rotateM(false) : __privateMethod(this, _CubeEngine_instances, rotateM_fn).call(this, false)
659
+ );
660
+ break;
661
+ default:
662
+ break;
663
+ }
664
+ }
665
+ };
352
666
  var COLOR = {
353
667
  W: ["W", "W", "W", "W", "W", "W", "W", "W", "W"],
354
668
  G: ["G", "G", "G", "G", "G", "G", "G", "G", "G"],