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.mjs CHANGED
@@ -23,9 +23,9 @@ 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, rotateX_fn, rotateY_fn, switchMatrix_fn, specialFlip_fn;
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;
27
27
  var CubeEngine = class {
28
- constructor() {
28
+ constructor(initialScramble = "") {
29
29
  __privateAdd(this, _CubeEngine_instances);
30
30
  __publicField(this, "MOVES", []);
31
31
  // States object for the rotation
@@ -67,6 +67,10 @@ var CubeEngine = class {
67
67
  [COLOR.Y[6], COLOR.Y[7], COLOR.Y[8]]
68
68
  ]
69
69
  });
70
+ if (typeof initialScramble === "string" && initialScramble.trim().length > 0) {
71
+ __privateMethod(this, _CubeEngine_instances, applyMovesFromString_fn).call(this, initialScramble, false);
72
+ this.MOVES = [];
73
+ }
70
74
  }
71
75
  /**
72
76
  * Rotates the (UPPER) layer clockwise or counterclockwise.
@@ -128,6 +132,66 @@ var CubeEngine = class {
128
132
  this.MOVES.push("D'");
129
133
  }
130
134
  }
135
+ /**
136
+ * Rotates the wide (DOWN two layers) clockwise or counterclockwise.
137
+ */
138
+ rotateDw(clockwise = true) {
139
+ if (clockwise) {
140
+ __privateMethod(this, _CubeEngine_instances, rotateDw_fn).call(this, true);
141
+ this.MOVES.push("Dw");
142
+ } else {
143
+ __privateMethod(this, _CubeEngine_instances, rotateDw_fn).call(this, false);
144
+ this.MOVES.push("Dw'");
145
+ }
146
+ }
147
+ /**
148
+ * Rotates the wide (UPPER two layers) clockwise or counterclockwise.
149
+ */
150
+ rotateUw(clockwise = true) {
151
+ if (clockwise) {
152
+ __privateMethod(this, _CubeEngine_instances, rotateUw_fn).call(this, true);
153
+ this.MOVES.push("Uw");
154
+ } else {
155
+ __privateMethod(this, _CubeEngine_instances, rotateUw_fn).call(this, false);
156
+ this.MOVES.push("Uw'");
157
+ }
158
+ }
159
+ /**
160
+ * Rotates the wide (RIGHT two layers) clockwise or counterclockwise.
161
+ */
162
+ rotateRw(clockwise = true) {
163
+ if (clockwise) {
164
+ __privateMethod(this, _CubeEngine_instances, rotateRw_fn).call(this, true);
165
+ this.MOVES.push("Rw");
166
+ } else {
167
+ __privateMethod(this, _CubeEngine_instances, rotateRw_fn).call(this, false);
168
+ this.MOVES.push("Rw'");
169
+ }
170
+ }
171
+ /**
172
+ * Rotates the wide (LEFT two layers) clockwise or counterclockwise.
173
+ */
174
+ rotateLw(clockwise = true) {
175
+ if (clockwise) {
176
+ __privateMethod(this, _CubeEngine_instances, rotateLw_fn).call(this, true);
177
+ this.MOVES.push("Lw");
178
+ } else {
179
+ __privateMethod(this, _CubeEngine_instances, rotateLw_fn).call(this, false);
180
+ this.MOVES.push("Lw'");
181
+ }
182
+ }
183
+ /**
184
+ * Rotates the middle slice (M) parallel to L/R. Clockwise corresponds to Lw followed by L'.
185
+ */
186
+ rotateM(clockwise = true) {
187
+ if (clockwise) {
188
+ __privateMethod(this, _CubeEngine_instances, rotateM_fn).call(this, true);
189
+ this.MOVES.push("M");
190
+ } else {
191
+ __privateMethod(this, _CubeEngine_instances, rotateM_fn).call(this, false);
192
+ this.MOVES.push("M'");
193
+ }
194
+ }
131
195
  /**
132
196
  * Rotates the (x) axis clockwise or counterclockwise.
133
197
  */
@@ -140,6 +204,18 @@ var CubeEngine = class {
140
204
  this.MOVES.push("x'");
141
205
  }
142
206
  }
207
+ /**
208
+ * Rotates the (z) axis clockwise or counterclockwise.
209
+ */
210
+ rotateZ(clockwise = true) {
211
+ if (clockwise) {
212
+ __privateMethod(this, _CubeEngine_instances, rotateZ_fn).call(this, true);
213
+ this.MOVES.push("z");
214
+ } else {
215
+ __privateMethod(this, _CubeEngine_instances, rotateZ_fn).call(this, false);
216
+ this.MOVES.push("z'");
217
+ }
218
+ }
143
219
  /**
144
220
  * Rotates the (y) axis clockwise or counterclockwise.
145
221
  */
@@ -183,6 +259,54 @@ var CubeEngine = class {
183
259
  getMoves(asString = true) {
184
260
  return asString ? this.MOVES.join(" ") : this.MOVES;
185
261
  }
262
+ /**
263
+ * Resets the cube to the solved state and clears the move history.
264
+ */
265
+ 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
+ };
298
+ this.MOVES = [];
299
+ }
300
+ /**
301
+ * Applies a sequence of moves provided as a string.
302
+ * 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.
303
+ * @param {string} sequence - e.g. "R U' F R2 D Dw Uw Rw Rw' Lw Lw2 M M' M2"
304
+ * @param {object} options - { record: boolean } whether to record moves in history (default true)
305
+ */
306
+ applyMoves(sequence, options = { record: false }) {
307
+ const record = (options == null ? void 0 : options.record) !== false;
308
+ __privateMethod(this, _CubeEngine_instances, applyMovesFromString_fn).call(this, sequence, record);
309
+ }
186
310
  };
187
311
  _CubeEngine_instances = new WeakSet();
188
312
  rotateU_fn = function(clockwise = true) {
@@ -260,6 +384,51 @@ rotateD_fn = function(clockwise = true) {
260
384
  __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, false);
261
385
  }
262
386
  };
387
+ rotateDw_fn = function(clockwise = true) {
388
+ if (clockwise) {
389
+ __privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, false);
390
+ __privateMethod(this, _CubeEngine_instances, rotateU_fn).call(this, true);
391
+ } else {
392
+ __privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, true);
393
+ __privateMethod(this, _CubeEngine_instances, rotateU_fn).call(this, false);
394
+ }
395
+ };
396
+ rotateUw_fn = function(clockwise = true) {
397
+ if (clockwise) {
398
+ __privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, true);
399
+ __privateMethod(this, _CubeEngine_instances, rotateD_fn).call(this, true);
400
+ } else {
401
+ __privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, false);
402
+ __privateMethod(this, _CubeEngine_instances, rotateD_fn).call(this, false);
403
+ }
404
+ };
405
+ rotateRw_fn = function(clockwise = true) {
406
+ if (clockwise) {
407
+ __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, true);
408
+ __privateMethod(this, _CubeEngine_instances, rotateL_fn).call(this, true);
409
+ } else {
410
+ __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, false);
411
+ __privateMethod(this, _CubeEngine_instances, rotateL_fn).call(this, false);
412
+ }
413
+ };
414
+ rotateLw_fn = function(clockwise = true) {
415
+ if (clockwise) {
416
+ __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, false);
417
+ __privateMethod(this, _CubeEngine_instances, rotateR_fn).call(this, true);
418
+ } else {
419
+ __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, true);
420
+ __privateMethod(this, _CubeEngine_instances, rotateR_fn).call(this, false);
421
+ }
422
+ };
423
+ rotateM_fn = function(clockwise = true) {
424
+ if (clockwise) {
425
+ __privateMethod(this, _CubeEngine_instances, rotateLw_fn).call(this, true);
426
+ __privateMethod(this, _CubeEngine_instances, rotateL_fn).call(this, false);
427
+ } else {
428
+ __privateMethod(this, _CubeEngine_instances, rotateLw_fn).call(this, false);
429
+ __privateMethod(this, _CubeEngine_instances, rotateL_fn).call(this, true);
430
+ }
431
+ };
263
432
  rotateX_fn = function(clockwise = true) {
264
433
  const tempFront = structuredClone(this.STATES.FRONT);
265
434
  const tempDown = structuredClone(this.STATES.DOWN);
@@ -283,6 +452,29 @@ rotateX_fn = function(clockwise = true) {
283
452
  this.STATES.UPPER = __privateMethod(this, _CubeEngine_instances, specialFlip_fn).call(this, tempBack);
284
453
  }
285
454
  };
455
+ rotateZ_fn = function(clockwise = true) {
456
+ const tempUpper = structuredClone(this.STATES.UPPER);
457
+ const tempRight = structuredClone(this.STATES.RIGHT);
458
+ const tempDown = structuredClone(this.STATES.DOWN);
459
+ const tempLeft = structuredClone(this.STATES.LEFT);
460
+ const tempFront = structuredClone(this.STATES.FRONT);
461
+ const tempBack = structuredClone(this.STATES.BACK);
462
+ if (clockwise) {
463
+ this.STATES.FRONT = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempFront, true);
464
+ this.STATES.BACK = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempBack, false);
465
+ this.STATES.RIGHT = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempUpper, true);
466
+ this.STATES.DOWN = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempRight, true);
467
+ this.STATES.LEFT = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempDown, true);
468
+ this.STATES.UPPER = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempLeft, true);
469
+ } else {
470
+ this.STATES.FRONT = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempFront, false);
471
+ this.STATES.BACK = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempBack, true);
472
+ this.STATES.RIGHT = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempDown, false);
473
+ this.STATES.DOWN = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempLeft, false);
474
+ this.STATES.LEFT = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempUpper, false);
475
+ this.STATES.UPPER = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempRight, false);
476
+ }
477
+ };
286
478
  rotateY_fn = function(clockwise = true) {
287
479
  const tempFront = structuredClone(this.STATES.FRONT);
288
480
  const tempRight = structuredClone(this.STATES.RIGHT);
@@ -327,6 +519,128 @@ switchMatrix_fn = function(matrix, clockwise = true) {
327
519
  specialFlip_fn = function(matrix) {
328
520
  return structuredClone(matrix).reverse().map((row) => [...row].reverse());
329
521
  };
522
+ // Internal: parses and applies moves. If record=false, uses private methods to avoid logging.
523
+ applyMovesFromString_fn = function(sequence, record = true) {
524
+ if (typeof sequence !== "string") return;
525
+ const tokens = sequence.split(/\s+/).map((t) => t.trim()).filter((t) => t.length > 0);
526
+ for (const token of tokens) {
527
+ const base = token[0];
528
+ const rest = token.slice(1);
529
+ const isDouble = rest.includes("2");
530
+ const isPrime = rest.includes("'");
531
+ const times = isDouble ? 2 : 1;
532
+ const exec = (fnClockwise, fnCounter) => {
533
+ if (isDouble) {
534
+ fnClockwise();
535
+ fnClockwise();
536
+ } else {
537
+ if (isPrime) {
538
+ fnCounter();
539
+ } else {
540
+ fnClockwise();
541
+ }
542
+ }
543
+ };
544
+ switch (base) {
545
+ case "U":
546
+ {
547
+ const isWide = /w/i.test(rest);
548
+ if (isWide) {
549
+ exec(
550
+ () => record ? this.rotateUw(true) : __privateMethod(this, _CubeEngine_instances, rotateUw_fn).call(this, true),
551
+ () => record ? this.rotateUw(false) : __privateMethod(this, _CubeEngine_instances, rotateUw_fn).call(this, false)
552
+ );
553
+ } else {
554
+ exec(
555
+ () => record ? this.rotateU(true) : __privateMethod(this, _CubeEngine_instances, rotateU_fn).call(this, true),
556
+ () => record ? this.rotateU(false) : __privateMethod(this, _CubeEngine_instances, rotateU_fn).call(this, false)
557
+ );
558
+ }
559
+ }
560
+ break;
561
+ case "D":
562
+ {
563
+ const isWide = /w/i.test(rest);
564
+ if (isWide) {
565
+ exec(
566
+ () => record ? this.rotateDw(true) : __privateMethod(this, _CubeEngine_instances, rotateDw_fn).call(this, true),
567
+ () => record ? this.rotateDw(false) : __privateMethod(this, _CubeEngine_instances, rotateDw_fn).call(this, false)
568
+ );
569
+ } else {
570
+ exec(
571
+ () => record ? this.rotateD(true) : __privateMethod(this, _CubeEngine_instances, rotateD_fn).call(this, true),
572
+ () => record ? this.rotateD(false) : __privateMethod(this, _CubeEngine_instances, rotateD_fn).call(this, false)
573
+ );
574
+ }
575
+ }
576
+ break;
577
+ case "L":
578
+ {
579
+ const isWide = /w/i.test(rest);
580
+ if (isWide) {
581
+ exec(
582
+ () => record ? this.rotateLw(true) : __privateMethod(this, _CubeEngine_instances, rotateLw_fn).call(this, true),
583
+ () => record ? this.rotateLw(false) : __privateMethod(this, _CubeEngine_instances, rotateLw_fn).call(this, false)
584
+ );
585
+ } else {
586
+ exec(
587
+ () => record ? this.rotateL(true) : __privateMethod(this, _CubeEngine_instances, rotateL_fn).call(this, true),
588
+ () => record ? this.rotateL(false) : __privateMethod(this, _CubeEngine_instances, rotateL_fn).call(this, false)
589
+ );
590
+ }
591
+ }
592
+ break;
593
+ case "R":
594
+ {
595
+ const isWide = /w/i.test(rest);
596
+ if (isWide) {
597
+ exec(
598
+ () => record ? this.rotateRw(true) : __privateMethod(this, _CubeEngine_instances, rotateRw_fn).call(this, true),
599
+ () => record ? this.rotateRw(false) : __privateMethod(this, _CubeEngine_instances, rotateRw_fn).call(this, false)
600
+ );
601
+ } else {
602
+ exec(
603
+ () => record ? this.rotateR(true) : __privateMethod(this, _CubeEngine_instances, rotateR_fn).call(this, true),
604
+ () => record ? this.rotateR(false) : __privateMethod(this, _CubeEngine_instances, rotateR_fn).call(this, false)
605
+ );
606
+ }
607
+ }
608
+ break;
609
+ case "F":
610
+ exec(
611
+ () => record ? this.rotateF(true) : __privateMethod(this, _CubeEngine_instances, rotateF_fn).call(this, true),
612
+ () => record ? this.rotateF(false) : __privateMethod(this, _CubeEngine_instances, rotateF_fn).call(this, false)
613
+ );
614
+ break;
615
+ case "x":
616
+ exec(
617
+ () => record ? this.rotateX(true) : __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, true),
618
+ () => record ? this.rotateX(false) : __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, false)
619
+ );
620
+ break;
621
+ case "y":
622
+ exec(
623
+ () => record ? this.rotateY(true) : __privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, true),
624
+ () => record ? this.rotateY(false) : __privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, false)
625
+ );
626
+ break;
627
+ case "z":
628
+ exec(
629
+ () => record ? this.rotateZ(true) : __privateMethod(this, _CubeEngine_instances, rotateZ_fn).call(this, true),
630
+ () => record ? this.rotateZ(false) : __privateMethod(this, _CubeEngine_instances, rotateZ_fn).call(this, false)
631
+ );
632
+ break;
633
+ case "M":
634
+ exec(
635
+ () => record ? this.rotateM(true) : __privateMethod(this, _CubeEngine_instances, rotateM_fn).call(this, true),
636
+ () => record ? this.rotateM(false) : __privateMethod(this, _CubeEngine_instances, rotateM_fn).call(this, false)
637
+ );
638
+ break;
639
+ default:
640
+ break;
641
+ }
642
+ }
643
+ };
330
644
  var COLOR = {
331
645
  W: ["W", "W", "W", "W", "W", "W", "W", "W", "W"],
332
646
  G: ["G", "G", "G", "G", "G", "G", "G", "G", "G"],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cube-state-engine",
3
- "version": "1.0.5",
3
+ "version": "1.2.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",