cube-state-engine 1.0.5 → 1.1.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,16 @@ var CubeEngine = class {
183
259
  getMoves(asString = true) {
184
260
  return asString ? this.MOVES.join(" ") : this.MOVES;
185
261
  }
262
+ /**
263
+ * Applies a sequence of moves provided as a string.
264
+ * 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.
265
+ * @param {string} sequence - e.g. "R U' F R2 D Dw Uw Rw Rw' Lw Lw2 M M' M2"
266
+ * @param {object} options - { record: boolean } whether to record moves in history (default true)
267
+ */
268
+ applyMoves(sequence, options = { record: false }) {
269
+ const record = (options == null ? void 0 : options.record) !== false;
270
+ __privateMethod(this, _CubeEngine_instances, applyMovesFromString_fn).call(this, sequence, record);
271
+ }
186
272
  };
187
273
  _CubeEngine_instances = new WeakSet();
188
274
  rotateU_fn = function(clockwise = true) {
@@ -260,6 +346,51 @@ rotateD_fn = function(clockwise = true) {
260
346
  __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, false);
261
347
  }
262
348
  };
349
+ rotateDw_fn = function(clockwise = true) {
350
+ if (clockwise) {
351
+ __privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, false);
352
+ __privateMethod(this, _CubeEngine_instances, rotateU_fn).call(this, true);
353
+ } else {
354
+ __privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, true);
355
+ __privateMethod(this, _CubeEngine_instances, rotateU_fn).call(this, false);
356
+ }
357
+ };
358
+ rotateUw_fn = function(clockwise = true) {
359
+ if (clockwise) {
360
+ __privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, true);
361
+ __privateMethod(this, _CubeEngine_instances, rotateD_fn).call(this, true);
362
+ } else {
363
+ __privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, false);
364
+ __privateMethod(this, _CubeEngine_instances, rotateD_fn).call(this, false);
365
+ }
366
+ };
367
+ rotateRw_fn = function(clockwise = true) {
368
+ if (clockwise) {
369
+ __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, true);
370
+ __privateMethod(this, _CubeEngine_instances, rotateL_fn).call(this, true);
371
+ } else {
372
+ __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, false);
373
+ __privateMethod(this, _CubeEngine_instances, rotateL_fn).call(this, false);
374
+ }
375
+ };
376
+ rotateLw_fn = function(clockwise = true) {
377
+ if (clockwise) {
378
+ __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, false);
379
+ __privateMethod(this, _CubeEngine_instances, rotateR_fn).call(this, true);
380
+ } else {
381
+ __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, true);
382
+ __privateMethod(this, _CubeEngine_instances, rotateR_fn).call(this, false);
383
+ }
384
+ };
385
+ rotateM_fn = function(clockwise = true) {
386
+ if (clockwise) {
387
+ __privateMethod(this, _CubeEngine_instances, rotateLw_fn).call(this, true);
388
+ __privateMethod(this, _CubeEngine_instances, rotateL_fn).call(this, false);
389
+ } else {
390
+ __privateMethod(this, _CubeEngine_instances, rotateLw_fn).call(this, false);
391
+ __privateMethod(this, _CubeEngine_instances, rotateL_fn).call(this, true);
392
+ }
393
+ };
263
394
  rotateX_fn = function(clockwise = true) {
264
395
  const tempFront = structuredClone(this.STATES.FRONT);
265
396
  const tempDown = structuredClone(this.STATES.DOWN);
@@ -283,6 +414,29 @@ rotateX_fn = function(clockwise = true) {
283
414
  this.STATES.UPPER = __privateMethod(this, _CubeEngine_instances, specialFlip_fn).call(this, tempBack);
284
415
  }
285
416
  };
417
+ rotateZ_fn = function(clockwise = true) {
418
+ const tempUpper = structuredClone(this.STATES.UPPER);
419
+ const tempRight = structuredClone(this.STATES.RIGHT);
420
+ const tempDown = structuredClone(this.STATES.DOWN);
421
+ const tempLeft = structuredClone(this.STATES.LEFT);
422
+ const tempFront = structuredClone(this.STATES.FRONT);
423
+ const tempBack = structuredClone(this.STATES.BACK);
424
+ if (clockwise) {
425
+ this.STATES.FRONT = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempFront, true);
426
+ this.STATES.BACK = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempBack, false);
427
+ this.STATES.RIGHT = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempUpper, true);
428
+ this.STATES.DOWN = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempRight, true);
429
+ this.STATES.LEFT = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempDown, true);
430
+ this.STATES.UPPER = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempLeft, true);
431
+ } else {
432
+ this.STATES.FRONT = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempFront, false);
433
+ this.STATES.BACK = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempBack, true);
434
+ this.STATES.RIGHT = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempDown, false);
435
+ this.STATES.DOWN = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempLeft, false);
436
+ this.STATES.LEFT = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempUpper, false);
437
+ this.STATES.UPPER = __privateMethod(this, _CubeEngine_instances, switchMatrix_fn).call(this, tempRight, false);
438
+ }
439
+ };
286
440
  rotateY_fn = function(clockwise = true) {
287
441
  const tempFront = structuredClone(this.STATES.FRONT);
288
442
  const tempRight = structuredClone(this.STATES.RIGHT);
@@ -327,6 +481,128 @@ switchMatrix_fn = function(matrix, clockwise = true) {
327
481
  specialFlip_fn = function(matrix) {
328
482
  return structuredClone(matrix).reverse().map((row) => [...row].reverse());
329
483
  };
484
+ // Internal: parses and applies moves. If record=false, uses private methods to avoid logging.
485
+ applyMovesFromString_fn = function(sequence, record = true) {
486
+ if (typeof sequence !== "string") return;
487
+ const tokens = sequence.split(/\s+/).map((t) => t.trim()).filter((t) => t.length > 0);
488
+ for (const token of tokens) {
489
+ const base = token[0];
490
+ const rest = token.slice(1);
491
+ const isDouble = rest.includes("2");
492
+ const isPrime = rest.includes("'");
493
+ const times = isDouble ? 2 : 1;
494
+ const exec = (fnClockwise, fnCounter) => {
495
+ if (isDouble) {
496
+ fnClockwise();
497
+ fnClockwise();
498
+ } else {
499
+ if (isPrime) {
500
+ fnCounter();
501
+ } else {
502
+ fnClockwise();
503
+ }
504
+ }
505
+ };
506
+ switch (base) {
507
+ case "U":
508
+ {
509
+ const isWide = /w/i.test(rest);
510
+ if (isWide) {
511
+ exec(
512
+ () => record ? this.rotateUw(true) : __privateMethod(this, _CubeEngine_instances, rotateUw_fn).call(this, true),
513
+ () => record ? this.rotateUw(false) : __privateMethod(this, _CubeEngine_instances, rotateUw_fn).call(this, false)
514
+ );
515
+ } else {
516
+ exec(
517
+ () => record ? this.rotateU(true) : __privateMethod(this, _CubeEngine_instances, rotateU_fn).call(this, true),
518
+ () => record ? this.rotateU(false) : __privateMethod(this, _CubeEngine_instances, rotateU_fn).call(this, false)
519
+ );
520
+ }
521
+ }
522
+ break;
523
+ case "D":
524
+ {
525
+ const isWide = /w/i.test(rest);
526
+ if (isWide) {
527
+ exec(
528
+ () => record ? this.rotateDw(true) : __privateMethod(this, _CubeEngine_instances, rotateDw_fn).call(this, true),
529
+ () => record ? this.rotateDw(false) : __privateMethod(this, _CubeEngine_instances, rotateDw_fn).call(this, false)
530
+ );
531
+ } else {
532
+ exec(
533
+ () => record ? this.rotateD(true) : __privateMethod(this, _CubeEngine_instances, rotateD_fn).call(this, true),
534
+ () => record ? this.rotateD(false) : __privateMethod(this, _CubeEngine_instances, rotateD_fn).call(this, false)
535
+ );
536
+ }
537
+ }
538
+ break;
539
+ case "L":
540
+ {
541
+ const isWide = /w/i.test(rest);
542
+ if (isWide) {
543
+ exec(
544
+ () => record ? this.rotateLw(true) : __privateMethod(this, _CubeEngine_instances, rotateLw_fn).call(this, true),
545
+ () => record ? this.rotateLw(false) : __privateMethod(this, _CubeEngine_instances, rotateLw_fn).call(this, false)
546
+ );
547
+ } else {
548
+ exec(
549
+ () => record ? this.rotateL(true) : __privateMethod(this, _CubeEngine_instances, rotateL_fn).call(this, true),
550
+ () => record ? this.rotateL(false) : __privateMethod(this, _CubeEngine_instances, rotateL_fn).call(this, false)
551
+ );
552
+ }
553
+ }
554
+ break;
555
+ case "R":
556
+ {
557
+ const isWide = /w/i.test(rest);
558
+ if (isWide) {
559
+ exec(
560
+ () => record ? this.rotateRw(true) : __privateMethod(this, _CubeEngine_instances, rotateRw_fn).call(this, true),
561
+ () => record ? this.rotateRw(false) : __privateMethod(this, _CubeEngine_instances, rotateRw_fn).call(this, false)
562
+ );
563
+ } else {
564
+ exec(
565
+ () => record ? this.rotateR(true) : __privateMethod(this, _CubeEngine_instances, rotateR_fn).call(this, true),
566
+ () => record ? this.rotateR(false) : __privateMethod(this, _CubeEngine_instances, rotateR_fn).call(this, false)
567
+ );
568
+ }
569
+ }
570
+ break;
571
+ case "F":
572
+ exec(
573
+ () => record ? this.rotateF(true) : __privateMethod(this, _CubeEngine_instances, rotateF_fn).call(this, true),
574
+ () => record ? this.rotateF(false) : __privateMethod(this, _CubeEngine_instances, rotateF_fn).call(this, false)
575
+ );
576
+ break;
577
+ case "x":
578
+ exec(
579
+ () => record ? this.rotateX(true) : __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, true),
580
+ () => record ? this.rotateX(false) : __privateMethod(this, _CubeEngine_instances, rotateX_fn).call(this, false)
581
+ );
582
+ break;
583
+ case "y":
584
+ exec(
585
+ () => record ? this.rotateY(true) : __privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, true),
586
+ () => record ? this.rotateY(false) : __privateMethod(this, _CubeEngine_instances, rotateY_fn).call(this, false)
587
+ );
588
+ break;
589
+ case "z":
590
+ exec(
591
+ () => record ? this.rotateZ(true) : __privateMethod(this, _CubeEngine_instances, rotateZ_fn).call(this, true),
592
+ () => record ? this.rotateZ(false) : __privateMethod(this, _CubeEngine_instances, rotateZ_fn).call(this, false)
593
+ );
594
+ break;
595
+ case "M":
596
+ exec(
597
+ () => record ? this.rotateM(true) : __privateMethod(this, _CubeEngine_instances, rotateM_fn).call(this, true),
598
+ () => record ? this.rotateM(false) : __privateMethod(this, _CubeEngine_instances, rotateM_fn).call(this, false)
599
+ );
600
+ break;
601
+ default:
602
+ break;
603
+ }
604
+ }
605
+ };
330
606
  var COLOR = {
331
607
  W: ["W", "W", "W", "W", "W", "W", "W", "W", "W"],
332
608
  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.1.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",