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.
@@ -0,0 +1,12 @@
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>
@@ -0,0 +1,8 @@
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 ADDED
@@ -0,0 +1,6 @@
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>
package/README.md CHANGED
@@ -12,18 +12,26 @@ A core state manager designed to integrate with custom 3D cube models. This engi
12
12
 
13
13
  ## Methods
14
14
 
15
- | Method | Description | Return Type |
16
- | ----------------------------- | -------------------------------------------------------------------------- | ----------- |
17
- | `isSolved()` | Checks if the cube is solved. | `boolean` |
18
- | `state()` | Returns the current state of the cube as an object representing each face. | `object` |
19
- | `getMoves(asString: boolean)` | Returns the history of all movements made. | `string` |
20
- | `rotateU(clockwise: boolean)` | Rotates the **Upper** face clockwise or counterclockwise. | `void` |
21
- | `rotateF(clockwise: boolean)` | Rotates the **Front** face clockwise or counterclockwise. | `void` |
22
- | `rotateD(clockwise: boolean)` | Rotates the **Down** face clockwise or counterclockwise. | `void` |
23
- | `rotateX(clockwise: boolean)` | Rotates the cube along the **X-axis** clockwise or counterclockwise. | `void` |
24
- | `rotateY(clockwise: boolean)` | Rotates the cube along the **Y-axis** clockwise or counterclockwise. | `void` |
25
- | `rotateL(clockwise: boolean)` | Rotates the **Left** face clockwise or counterclockwise. | `void` |
26
- | `rotateR(clockwise: boolean)` | Rotates the **Right** face clockwise or counterclockwise. | `void` |
15
+ | Method | Description | Return Type |
16
+ | ------------------------------------------------ | -------------------------------------------------------------------------------------------------------- |---------------------|
17
+ | `constructor(initialScramble?: string)` | Optionally initialize the cube with a scramble string (not recorded in history). | `CubeEngine` |
18
+ | `isSolved()` | Checks if the cube is solved. | `boolean` |
19
+ | `state()` | Returns the current state of the cube as an object representing each face. | `object` |
20
+ | `getMoves(asString?: boolean)` | Returns the history of all movements; as string if `true` (default), or as array if `false`. | `string / string[]` |
21
+ | `applyMoves(sequence: string, options?: {record?: boolean})` | Applies a sequence of moves (supports U, D, L, R, F, x, y, z, M, Dw, Uw, Rw, Lw with ', 2). | `void` |
22
+ | `rotateU(clockwise?: boolean)` | Rotates the **Upper** layer clockwise or counterclockwise. | `void` |
23
+ | `rotateD(clockwise?: boolean)` | Rotates the **Down** layer clockwise or counterclockwise. | `void` |
24
+ | `rotateL(clockwise?: boolean)` | Rotates the **Left** layer clockwise or counterclockwise. | `void` |
25
+ | `rotateR(clockwise?: boolean)` | Rotates the **Right** layer clockwise or counterclockwise. | `void` |
26
+ | `rotateF(clockwise?: boolean)` | Rotates the **Front** layer clockwise or counterclockwise. | `void` |
27
+ | `rotateX(clockwise?: boolean)` | Rotates the cube along the **X-axis** clockwise or counterclockwise. | `void` |
28
+ | `rotateY(clockwise?: boolean)` | Rotates the cube along the **Y-axis** clockwise or counterclockwise. | `void` |
29
+ | `rotateZ(clockwise?: boolean)` | Rotates the cube along the **Z-axis** clockwise or counterclockwise. | `void` |
30
+ | `rotateDw(clockwise?: boolean)` | Rotates the wide **Down** two layers. | `void` |
31
+ | `rotateUw(clockwise?: boolean)` | Rotates the wide **Upper** two layers. | `void` |
32
+ | `rotateRw(clockwise?: boolean)` | Rotates the wide **Right** two layers. | `void` |
33
+ | `rotateLw(clockwise?: boolean)` | Rotates the wide **Left** two layers. | `void` |
34
+ | `rotateM(clockwise?: boolean)` | Rotates the middle slice parallel to L/R. | `void` |
27
35
 
28
36
  ---
29
37
 
@@ -47,18 +55,25 @@ The `state()` method returns the current state of the cube as an object with six
47
55
  ```javascript
48
56
  import { CubeEngine } from "cube-state-engine";
49
57
 
50
- // Initialize the engine
51
- const cube = new CubeEngine();
58
+ // Initialize the engine (optionally with a scramble)
59
+ const cube = new CubeEngine("R U' F R2 D");
52
60
 
53
61
  // Check if the cube is solved
54
- console.log(cube.isSolved()); // true
62
+ console.log(cube.isSolved());
55
63
 
56
64
  // Rotate the upper face clockwise
57
65
  cube.rotateU(true);
58
66
 
67
+ // Apply an algorithm and record moves
68
+ cube.applyMoves("Dw Dw' M2", { record: true });
69
+
59
70
  // Get the current cube state
60
71
  console.log(cube.state());
61
72
 
73
+ // Get history as string or array
74
+ console.log(cube.getMoves(true)); // "U Dw Dw' M2 ..."
75
+ console.log(cube.getMoves(false)); // ["U", "Dw", "Dw'", "M", "M"]
76
+
62
77
  // Rotate the cube along the Y-axis
63
78
  cube.rotateY(false);
64
79
  ```
package/dist/index.d.mts CHANGED
@@ -1,6 +1,15 @@
1
1
  class CubeEngine {
2
2
  MOVES = [];
3
3
 
4
+ constructor(initialScramble) {
5
+ // If an initial scramble string is provided, apply it without recording moves
6
+ if (typeof initialScramble === "string" && initialScramble.trim().length > 0) {
7
+ this.#applyMovesFromString(initialScramble, false);
8
+ // Ensure history is empty for initial position
9
+ this.MOVES = [];
10
+ }
11
+ }
12
+
4
13
  // States object for the rotation
5
14
  STATES = {
6
15
  UPPER: [
@@ -190,6 +199,122 @@ class CubeEngine {
190
199
  }
191
200
  }
192
201
 
202
+ /**
203
+ * Rotates the wide (DOWN two layers) clockwise or counterclockwise.
204
+ */
205
+ rotateDw(clockwise = true) {
206
+ if (clockwise) {
207
+ this.#rotateDw(true);
208
+ this.MOVES.push("Dw");
209
+ } else {
210
+ this.#rotateDw(false);
211
+ this.MOVES.push("Dw'");
212
+ }
213
+ }
214
+
215
+ #rotateDw(clockwise = true) {
216
+ if (clockwise) {
217
+ this.#rotateY(false);
218
+ this.#rotateU(true);
219
+ } else {
220
+ this.#rotateY(true);
221
+ this.#rotateU(false);
222
+ }
223
+ }
224
+
225
+ /**
226
+ * Rotates the wide (UPPER two layers) clockwise or counterclockwise.
227
+ */
228
+ rotateUw(clockwise = true) {
229
+ if (clockwise) {
230
+ this.#rotateUw(true);
231
+ this.MOVES.push("Uw");
232
+ } else {
233
+ this.#rotateUw(false);
234
+ this.MOVES.push("Uw'");
235
+ }
236
+ }
237
+
238
+ #rotateUw(clockwise = true) {
239
+ if (clockwise) {
240
+ this.#rotateY(true);
241
+ this.#rotateD(true);
242
+ } else {
243
+ this.#rotateY(false);
244
+ this.#rotateD(false);
245
+ }
246
+ }
247
+
248
+ /**
249
+ * Rotates the wide (RIGHT two layers) clockwise or counterclockwise.
250
+ */
251
+ rotateRw(clockwise = true) {
252
+ if (clockwise) {
253
+ this.#rotateRw(true);
254
+ this.MOVES.push("Rw");
255
+ } else {
256
+ this.#rotateRw(false);
257
+ this.MOVES.push("Rw'");
258
+ }
259
+ }
260
+
261
+ #rotateRw(clockwise = true) {
262
+ if (clockwise) {
263
+ this.#rotateX(true);
264
+ this.#rotateL(true);
265
+ } else {
266
+ this.#rotateX(false);
267
+ this.#rotateL(false);
268
+ }
269
+ }
270
+
271
+ /**
272
+ * Rotates the wide (LEFT two layers) clockwise or counterclockwise.
273
+ */
274
+ rotateLw(clockwise = true) {
275
+ if (clockwise) {
276
+ this.#rotateLw(true);
277
+ this.MOVES.push("Lw");
278
+ } else {
279
+ this.#rotateLw(false);
280
+ this.MOVES.push("Lw'");
281
+ }
282
+ }
283
+
284
+ #rotateLw(clockwise = true) {
285
+ if (clockwise) {
286
+ // Lw equals x' R
287
+ this.#rotateX(false);
288
+ this.#rotateR(true);
289
+ } else {
290
+ this.#rotateX(true);
291
+ this.#rotateR(false);
292
+ }
293
+ }
294
+
295
+ /**
296
+ * Rotates the middle slice (M) parallel to L/R. Clockwise corresponds to Lw followed by L'.
297
+ */
298
+ rotateM(clockwise = true) {
299
+ if (clockwise) {
300
+ this.#rotateM(true);
301
+ this.MOVES.push("M");
302
+ } else {
303
+ this.#rotateM(false);
304
+ this.MOVES.push("M'");
305
+ }
306
+ }
307
+
308
+ #rotateM(clockwise = true) {
309
+ if (clockwise) {
310
+ this.#rotateLw(true);
311
+ this.#rotateL(false);
312
+ } else {
313
+ this.#rotateLw(false);
314
+ this.#rotateL(true);
315
+ }
316
+ }
317
+
193
318
  /**
194
319
  * Rotates the (x) axis clockwise or counterclockwise.
195
320
  */
@@ -235,6 +360,50 @@ class CubeEngine {
235
360
  }
236
361
  }
237
362
 
363
+ /**
364
+ * Rotates the (z) axis clockwise or counterclockwise.
365
+ */
366
+ rotateZ(clockwise = true) {
367
+ if (clockwise) {
368
+ this.#rotateZ(true);
369
+ this.MOVES.push("z");
370
+ } else {
371
+ this.#rotateZ(false);
372
+ this.MOVES.push("z'");
373
+ }
374
+ }
375
+
376
+ #rotateZ(clockwise = true) {
377
+ const tempUpper = structuredClone(this.STATES.UPPER);
378
+ const tempRight = structuredClone(this.STATES.RIGHT);
379
+ const tempDown = structuredClone(this.STATES.DOWN);
380
+ const tempLeft = structuredClone(this.STATES.LEFT);
381
+ const tempFront = structuredClone(this.STATES.FRONT);
382
+ const tempBack = structuredClone(this.STATES.BACK);
383
+
384
+ if (clockwise) {
385
+ // Rotate faces on the rotation axis
386
+ this.STATES.FRONT = this.#switchMatrix(tempFront, true);
387
+ this.STATES.BACK = this.#switchMatrix(tempBack, false);
388
+
389
+ // Cycle U -> R -> D -> L -> U with proper orientation
390
+ this.STATES.RIGHT = this.#switchMatrix(tempUpper, true);
391
+ this.STATES.DOWN = this.#switchMatrix(tempRight, true);
392
+ this.STATES.LEFT = this.#switchMatrix(tempDown, true);
393
+ this.STATES.UPPER = this.#switchMatrix(tempLeft, true);
394
+ } else {
395
+ // Counterclockwise
396
+ this.STATES.FRONT = this.#switchMatrix(tempFront, false);
397
+ this.STATES.BACK = this.#switchMatrix(tempBack, true);
398
+
399
+ // Cycle U -> L -> D -> R -> U (inverse of clockwise), rotate CCW
400
+ this.STATES.RIGHT = this.#switchMatrix(tempDown, false);
401
+ this.STATES.DOWN = this.#switchMatrix(tempLeft, false);
402
+ this.STATES.LEFT = this.#switchMatrix(tempUpper, false);
403
+ this.STATES.UPPER = this.#switchMatrix(tempRight, false);
404
+ }
405
+ }
406
+
238
407
  /**
239
408
  * Rotates the (y) axis clockwise or counterclockwise.
240
409
  */
@@ -343,6 +512,144 @@ class CubeEngine {
343
512
  getMoves(asString = true) {
344
513
  return asString ? this.MOVES.join(" ") : this.MOVES;
345
514
  }
515
+
516
+ /**
517
+ * Applies a sequence of moves provided as a string.
518
+ * 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.
519
+ * @param {string} sequence - e.g. "R U' F R2 D Dw Uw Rw Rw' Lw Lw2 M M' M2"
520
+ * @param {object} options - { record: boolean } whether to record moves in history (default true)
521
+ */
522
+ applyMoves(sequence, options = { record: false }) {
523
+ const record = options?.record !== false;
524
+ this.#applyMovesFromString(sequence, record);
525
+ }
526
+
527
+ // Internal: parses and applies moves. If record=false, uses private methods to avoid logging.
528
+ #applyMovesFromString(sequence, record = true) {
529
+ if (typeof sequence !== "string") return;
530
+ const tokens = sequence
531
+ .split(/\s+/)
532
+ .map((t) => t.trim())
533
+ .filter((t) => t.length > 0);
534
+
535
+ for (const token of tokens) {
536
+ const base = token[0];
537
+ const rest = token.slice(1);
538
+ const isDouble = rest.includes("2");
539
+ const isPrime = rest.includes("'");
540
+
541
+ const exec = (fnClockwise, fnCounter) => {
542
+ if (isDouble) {
543
+ // Double turns ignore prime; do two clockwise quarter-turns
544
+ fnClockwise();
545
+ fnClockwise();
546
+ } else {
547
+ if (isPrime) {
548
+ fnCounter();
549
+ } else {
550
+ fnClockwise();
551
+ }
552
+ }
553
+ };
554
+
555
+ switch (base) {
556
+ case 'U':
557
+ {
558
+ const isWide = /w/i.test(rest);
559
+ if (isWide) {
560
+ exec(
561
+ () => (record ? this.rotateUw(true) : this.#rotateUw(true)),
562
+ () => (record ? this.rotateUw(false) : this.#rotateUw(false))
563
+ );
564
+ } else {
565
+ exec(
566
+ () => (record ? this.rotateU(true) : this.#rotateU(true)),
567
+ () => (record ? this.rotateU(false) : this.#rotateU(false))
568
+ );
569
+ }
570
+ }
571
+ break;
572
+ case 'D':
573
+ {
574
+ const isWide = /w/i.test(rest);
575
+ if (isWide) {
576
+ exec(
577
+ () => (record ? this.rotateDw(true) : this.#rotateDw(true)),
578
+ () => (record ? this.rotateDw(false) : this.#rotateDw(false))
579
+ );
580
+ } else {
581
+ exec(
582
+ () => (record ? this.rotateD(true) : this.#rotateD(true)),
583
+ () => (record ? this.rotateD(false) : this.#rotateD(false))
584
+ );
585
+ }
586
+ }
587
+ break;
588
+ case 'L':
589
+ {
590
+ const isWide = /w/i.test(rest);
591
+ if (isWide) {
592
+ exec(
593
+ () => (record ? this.rotateLw(true) : this.#rotateLw(true)),
594
+ () => (record ? this.rotateLw(false) : this.#rotateLw(false))
595
+ );
596
+ } else {
597
+ exec(
598
+ () => (record ? this.rotateL(true) : this.#rotateL(true)),
599
+ () => (record ? this.rotateL(false) : this.#rotateL(false))
600
+ );
601
+ }
602
+ }
603
+ break;
604
+ case 'R':
605
+ {
606
+ const isWide = /w/i.test(rest);
607
+ if (isWide) {
608
+ exec(
609
+ () => (record ? this.rotateRw(true) : this.#rotateRw(true)),
610
+ () => (record ? this.rotateRw(false) : this.#rotateRw(false))
611
+ );
612
+ } else {
613
+ exec(
614
+ () => (record ? this.rotateR(true) : this.#rotateR(true)),
615
+ () => (record ? this.rotateR(false) : this.#rotateR(false))
616
+ );
617
+ }
618
+ }
619
+ break;
620
+ case 'F':
621
+ exec(
622
+ () => (record ? this.rotateF(true) : this.#rotateF(true)),
623
+ () => (record ? this.rotateF(false) : this.#rotateF(false))
624
+ );
625
+ break;
626
+ case 'x':
627
+ exec(
628
+ () => (record ? this.rotateX(true) : this.#rotateX(true)),
629
+ () => (record ? this.rotateX(false) : this.#rotateX(false))
630
+ );
631
+ break;
632
+ case 'y':
633
+ exec(
634
+ () => (record ? this.rotateY(true) : this.#rotateY(true)),
635
+ () => (record ? this.rotateY(false) : this.#rotateY(false))
636
+ );
637
+ break;
638
+ case 'z':
639
+ exec(
640
+ () => (record ? this.rotateZ(true) : this.#rotateZ(true)),
641
+ () => (record ? this.rotateZ(false) : this.#rotateZ(false))
642
+ );
643
+ break;
644
+ case 'M':
645
+ exec(
646
+ () => (record ? this.rotateM(true) : this.#rotateM(true)),
647
+ () => (record ? this.rotateM(false) : this.#rotateM(false))
648
+ );
649
+ break;
650
+ }
651
+ }
652
+ }
346
653
  }
347
654
 
348
655
  const COLOR = {