cube-state-engine 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Bryan Lundberg
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # cube-state-engine
2
+
3
+ An efficient representation in memory for tracking the Rubik's cube state on each movement.
@@ -0,0 +1,3 @@
1
+ module.exports = {
2
+ presets: [["@babel/preset-env", { targets: { node: "current" } }]],
3
+ };
package/index.html ADDED
@@ -0,0 +1,107 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
+ <script src="https://cdn.tailwindcss.com"></script>
7
+ <link href="styles.css" rel="stylesheet" />
8
+ <script
9
+ src="https://cdn.cubing.net/v0/js/cubing/twisty"
10
+ type="module"
11
+ ></script>
12
+ <title>Cube Engine Sample</title>
13
+ </head>
14
+ <body class="max-w-xl p-10 mx-auto font-serif">
15
+ <h1 class="mt-5 mb-5 text-2xl text-center font-bold">CUBE STATE ENGINE</h1>
16
+ <p class="mb-10">
17
+ It's a core state manager that should be able to integrate with custom 3D
18
+ cube models, making it easy to track and update the cube's state after
19
+ every move, providing context information, such as when it's solved.
20
+ </p>
21
+
22
+ <h3 class="font-bold">Docs</h3>
23
+
24
+ <pre class="mb-5">
25
+ class CubeEngine {
26
+ isSolved():boolean,
27
+ state(): {
28
+ UPPER: string[][];
29
+ LEFT: string[][];
30
+ FRONT: string[][];
31
+ RIGHT: string[][];
32
+ BACK: string[][];
33
+ DOWN: string[][];
34
+ }
35
+ rotateU(clockwise: boolean): void,
36
+ rotateF(clockwise: boolean): void,
37
+ rotateD(clockwise: boolean): void,
38
+ rotateX(clockwise: boolean): void,
39
+ rotateY(clockwise: boolean): void,
40
+ rotateL(clockwise: boolean): void,
41
+ rotateR(clockwise: boolean): void,
42
+ }
43
+ </pre
44
+ >
45
+
46
+ <h3 class="font-bold mb-5">Sample usage</h3>
47
+ <p>
48
+ In this example, I am demonstrating 3D visualization of a Rubik's Cube
49
+ using the Cubing.js library. You can replace it with any other
50
+ configuration if desired. I have set up the controls to work with the
51
+ keyboard, allowing you to interact with the cube. Specially, you can track
52
+ when the cube is solved.
53
+ </p>
54
+
55
+ <pre class="mt-5 animate-pulse">
56
+ CONTROLS_KEYBOARD: {
57
+ LEFT_HAND: A,S,D,F,G,E,T,
58
+ RIGHT_HAND: H,J,K,L,Ñ/;,I,B,Y
59
+ }
60
+ </pre
61
+ >
62
+ <section
63
+ class="flex flex-col items-center justify-center gap-10 sm:flex-row"
64
+ >
65
+ <div class="flex-col">
66
+ <h2 class="mb-3 font-bold">3D</h2>
67
+ <twisty-player
68
+ style="width: 200px; height: 200px"
69
+ controlPanel="none"
70
+ back-view="top-right"
71
+ ></twisty-player>
72
+ </div>
73
+
74
+ <div class="flex-col">
75
+ <h2 class="mb-3 font-bold">Memory</h2>
76
+ <div
77
+ class="w-[200px] h-[200px] flex justify-center items-center relative text-xs"
78
+ >
79
+ <div class="absolute top-16 left-1" id="LEFT"></div>
80
+
81
+ <div class="absolute top-16 left-12" id="FRONT"></div>
82
+
83
+ <div class="absolute top-16 left-24" id="RIGHT"></div>
84
+
85
+ <div class="absolute top-16 left-36" id="BACK"></div>
86
+
87
+ <div class="absolute top-0 left-12" id="UPPER"></div>
88
+
89
+ <div class="absolute top-32 left-12" id="DOWN"></div>
90
+ </div>
91
+ </div>
92
+ </section>
93
+
94
+ <section class="w-full mt-5 text-start">
95
+ <div class="font-black">
96
+ isSolved?: <span class="font-normal" id="is-solve">true</span>
97
+ </div>
98
+ <div class="font-black">
99
+ Moves: <span class="font-normal" id="total">0</span>
100
+ </div>
101
+ <div>
102
+ <span class="font-black">Historial: </span><span id="moves"></span>
103
+ </div>
104
+ </section>
105
+ <script src="main.js" type="module"></script>
106
+ </body>
107
+ </html>
package/jest.config.js ADDED
@@ -0,0 +1,198 @@
1
+ /**
2
+ * For a detailed explanation regarding each configuration property, visit:
3
+ * https://jestjs.io/docs/configuration
4
+ */
5
+
6
+ /** @type {import('jest').Config} */
7
+ const config = {
8
+ // All imported modules in your tests should be mocked automatically
9
+ // automock: false,
10
+
11
+ // Stop running tests after `n` failures
12
+ // bail: 0,
13
+
14
+ // The directory where Jest should store its cached dependency information
15
+ // cacheDirectory: "C:\\Users\\bryan\\AppData\\Local\\Temp\\jest",
16
+
17
+ // Automatically clear mock calls, instances, contexts and results before every test
18
+ clearMocks: true,
19
+
20
+ // Indicates whether the coverage information should be collected while executing the test
21
+ collectCoverage: true,
22
+
23
+ // An array of glob patterns indicating a set of files for which coverage information should be collected
24
+ // collectCoverageFrom: undefined,
25
+
26
+ // The directory where Jest should output its coverage files
27
+ coverageDirectory: "coverage",
28
+
29
+ // An array of regexp pattern strings used to skip coverage collection
30
+ // coveragePathIgnorePatterns: [
31
+ // "\\\\node_modules\\\\"
32
+ // ],
33
+
34
+ // Indicates which provider should be used to instrument code for coverage
35
+ coverageProvider: "v8",
36
+
37
+ // A list of reporter names that Jest uses when writing coverage reports
38
+ // coverageReporters: [
39
+ // "json",
40
+ // "text",
41
+ // "lcov",
42
+ // "clover"
43
+ // ],
44
+
45
+ // An object that configures minimum threshold enforcement for coverage results
46
+ // coverageThreshold: undefined,
47
+
48
+ // A path to a custom dependency extractor
49
+ // dependencyExtractor: undefined,
50
+
51
+ // Make calling deprecated APIs throw helpful error messages
52
+ // errorOnDeprecated: false,
53
+
54
+ // The default configuration for fake timers
55
+ // fakeTimers: {
56
+ // "enableGlobally": false
57
+ // },
58
+
59
+ // Force coverage collection from ignored files using an array of glob patterns
60
+ // forceCoverageMatch: [],
61
+
62
+ // A path to a module which exports an async function that is triggered once before all test suites
63
+ // globalSetup: undefined,
64
+
65
+ // A path to a module which exports an async function that is triggered once after all test suites
66
+ // globalTeardown: undefined,
67
+
68
+ // A set of global variables that need to be available in all test environments
69
+ // globals: {},
70
+
71
+ // The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
72
+ // maxWorkers: "50%",
73
+
74
+ // An array of directory names to be searched recursively up from the requiring module's location
75
+ // moduleDirectories: [
76
+ // "node_modules"
77
+ // ],
78
+
79
+ // An array of file extensions your modules use
80
+ // moduleFileExtensions: [
81
+ // "js",
82
+ // "mjs",
83
+ // "cjs",
84
+ // "jsx",
85
+ // "ts",
86
+ // "tsx",
87
+ // "json",
88
+ // "node"
89
+ // ],
90
+
91
+ // A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
92
+ // moduleNameMapper: {},
93
+
94
+ // An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
95
+ // modulePathIgnorePatterns: [],
96
+
97
+ // Activates notifications for test results
98
+ // notify: false,
99
+
100
+ // An enum that specifies notification mode. Requires { notify: true }
101
+ // notifyMode: "failure-change",
102
+
103
+ // A preset that is used as a base for Jest's configuration
104
+ // preset: undefined,
105
+
106
+ // Run tests from one or more projects
107
+ // projects: undefined,
108
+
109
+ // Use this configuration option to add custom reporters to Jest
110
+ // reporters: undefined,
111
+
112
+ // Automatically reset mock state before every test
113
+ // resetMocks: false,
114
+
115
+ // Reset the module registry before running each individual test
116
+ // resetModules: false,
117
+
118
+ // A path to a custom resolver
119
+ // resolver: undefined,
120
+
121
+ // Automatically restore mock state and implementation before every test
122
+ // restoreMocks: false,
123
+
124
+ // The root directory that Jest should scan for tests and modules within
125
+ // rootDir: undefined,
126
+
127
+ // A list of paths to directories that Jest should use to search for files in
128
+ // roots: [
129
+ // "<rootDir>"
130
+ // ],
131
+
132
+ // Allows you to use a custom runner instead of Jest's default test runner
133
+ // runner: "jest-runner",
134
+
135
+ // The paths to modules that run some code to configure or set up the testing environment before each test
136
+ // setupFiles: [],
137
+
138
+ // A list of paths to modules that run some code to configure or set up the testing framework before each test
139
+ // setupFilesAfterEnv: [],
140
+
141
+ // The number of seconds after which a test is considered as slow and reported as such in the results.
142
+ // slowTestThreshold: 5,
143
+
144
+ // A list of paths to snapshot serializer modules Jest should use for snapshot testing
145
+ // snapshotSerializers: [],
146
+
147
+ // The test environment that will be used for testing
148
+ testEnvironment: "node",
149
+
150
+ // Options that will be passed to the testEnvironment
151
+ // testEnvironmentOptions: {},
152
+
153
+ // Adds a location field to test results
154
+ // testLocationInResults: false,
155
+
156
+ // The glob patterns Jest uses to detect test files
157
+ // testMatch: [
158
+ // "**/__tests__/**/*.[jt]s?(x)",
159
+ // "**/?(*.)+(spec|test).[tj]s?(x)"
160
+ // ],
161
+
162
+ // An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
163
+ // testPathIgnorePatterns: [
164
+ // "\\\\node_modules\\\\"
165
+ // ],
166
+
167
+ // The regexp pattern or array of patterns that Jest uses to detect test files
168
+ // testRegex: [],
169
+
170
+ // This option allows the use of a custom results processor
171
+ // testResultsProcessor: undefined,
172
+
173
+ // This option allows use of a custom test runner
174
+ // testRunner: "jest-circus/runner",
175
+
176
+ // A map from regular expressions to paths to transformers
177
+ // transform: undefined,
178
+
179
+ // An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
180
+ // transformIgnorePatterns: [
181
+ // "\\\\node_modules\\\\",
182
+ // "\\.pnp\\.[^\\\\]+$"
183
+ // ],
184
+
185
+ // An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
186
+ // unmockedModulePathPatterns: undefined,
187
+
188
+ // Indicates whether each individual test should be reported during the run
189
+ // verbose: undefined,
190
+
191
+ // An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
192
+ // watchPathIgnorePatterns: [],
193
+
194
+ // Whether to use watchman for file crawling
195
+ // watchman: true,
196
+ };
197
+
198
+ module.exports = config;
package/main.js ADDED
@@ -0,0 +1,62 @@
1
+ import { CubeEngine } from "./src/index.js";
2
+
3
+ window.CubeEngine = new CubeEngine();
4
+ render();
5
+
6
+ const player = document.querySelector("twisty-player");
7
+ const moves = [];
8
+
9
+ /**
10
+ * Key listener for cube movements.
11
+ */
12
+ document.addEventListener("keyup", (e) => {
13
+ const movesMap = {
14
+ f: { move: () => window.CubeEngine.rotateU(false), notation: "U'" },
15
+ j: { move: () => window.CubeEngine.rotateU(true), notation: "U" },
16
+ g: { move: () => window.CubeEngine.rotateF(false), notation: "F'" },
17
+ h: { move: () => window.CubeEngine.rotateF(true), notation: "F" },
18
+ i: { move: () => window.CubeEngine.rotateR(true), notation: "R" },
19
+ k: { move: () => window.CubeEngine.rotateR(false), notation: "R'" },
20
+ d: { move: () => window.CubeEngine.rotateL(true), notation: "L" },
21
+ e: { move: () => window.CubeEngine.rotateL(false), notation: "L'" },
22
+ t: { move: () => window.CubeEngine.rotateX(true), notation: "x" },
23
+ y: { move: () => window.CubeEngine.rotateX(true), notation: "x" },
24
+ b: { move: () => window.CubeEngine.rotateX(false), notation: "x'" },
25
+ n: { move: () => window.CubeEngine.rotateX(false), notation: "x'" },
26
+ ñ: { move: () => window.CubeEngine.rotateY(true), notation: "y" },
27
+ a: { move: () => window.CubeEngine.rotateY(false), notation: "y'" },
28
+ s: { move: () => window.CubeEngine.rotateD(true), notation: "D" },
29
+ l: { move: () => window.CubeEngine.rotateD(false), notation: "D'" },
30
+ };
31
+
32
+ const key = e.key.toLowerCase();
33
+ const moveObj = movesMap[key];
34
+
35
+ if (moveObj) {
36
+ moveObj.move();
37
+ moves.push(moveObj.notation);
38
+
39
+ // sync UI
40
+ document.querySelector("#total").textContent = moves.length;
41
+ document.querySelector("#moves").textContent = moves.join(" ");
42
+ document.querySelector(
43
+ "#is-solve"
44
+ ).textContent = `${window.CubeEngine.isSolved()}`;
45
+ player.alg = moves.join(" ");
46
+ render();
47
+ }
48
+ });
49
+
50
+ function render() {
51
+ const state = window.CubeEngine.state();
52
+ Object.keys(state).forEach((layer) => {
53
+ const layerContent = state[layer]
54
+ .map(
55
+ (row) =>
56
+ `<div>${row.map((cell) => `<span>${cell}</span>`).join("")}</div>`
57
+ )
58
+ .join("");
59
+
60
+ document.querySelector(`#${layer}`).innerHTML = layerContent;
61
+ });
62
+ }
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "cube-state-engine",
3
+ "version": "1.0.0",
4
+ "description": "An efficient representation in memory for tracking the Rubik's cube state on each movement.",
5
+ "main": "./src/index.js",
6
+ "scripts": {
7
+ "test": "jest --watch"
8
+ },
9
+ "keywords": [],
10
+ "author": "Bryan Lundberg",
11
+ "license": "MIT",
12
+ "devDependencies": {
13
+ "@babel/core": "7.26.0",
14
+ "@babel/preset-env": "7.26.0",
15
+ "babel-jest": "29.7.0",
16
+ "jest": "29.7.0"
17
+ }
18
+ }
package/src/index.js ADDED
@@ -0,0 +1,275 @@
1
+ export class CubeEngine {
2
+ // States object for the rotation
3
+ STATES = {
4
+ UPPER: [
5
+ // (White)
6
+ [COLOR.W[0], COLOR.W[1], COLOR.W[2]],
7
+ [COLOR.W[3], COLOR.W[4], COLOR.W[5]],
8
+ [COLOR.W[6], COLOR.W[7], COLOR.W[8]],
9
+ ],
10
+ LEFT: [
11
+ // (Orange)
12
+ [COLOR.O[0], COLOR.O[1], COLOR.O[2]],
13
+ [COLOR.O[3], COLOR.O[4], COLOR.O[5]],
14
+ [COLOR.O[6], COLOR.O[7], COLOR.O[8]],
15
+ ],
16
+ FRONT: [
17
+ // (Green)
18
+ [COLOR.G[0], COLOR.G[1], COLOR.G[2]],
19
+ [COLOR.G[3], COLOR.G[4], COLOR.G[5]],
20
+ [COLOR.G[6], COLOR.G[7], COLOR.G[8]],
21
+ ],
22
+ RIGHT: [
23
+ // (Red)
24
+ [COLOR.R[0], COLOR.R[1], COLOR.R[2]],
25
+ [COLOR.R[3], COLOR.R[4], COLOR.R[5]],
26
+ [COLOR.R[6], COLOR.R[7], COLOR.R[8]],
27
+ ],
28
+ BACK: [
29
+ // (Blue)
30
+ [COLOR.B[0], COLOR.B[1], COLOR.B[2]],
31
+ [COLOR.B[3], COLOR.B[4], COLOR.B[5]],
32
+ [COLOR.B[6], COLOR.B[7], COLOR.B[8]],
33
+ ],
34
+ DOWN: [
35
+ // (Yellow)
36
+ [COLOR.Y[0], COLOR.Y[1], COLOR.Y[2]],
37
+ [COLOR.Y[3], COLOR.Y[4], COLOR.Y[5]],
38
+ [COLOR.Y[6], COLOR.Y[7], COLOR.Y[8]],
39
+ ],
40
+ };
41
+
42
+ /**
43
+ * Rotates the top (U) layer clockwise or counterclockwise.
44
+ */
45
+ rotateU(clockwise = true) {
46
+ if (clockwise) {
47
+ // Rotate the top layer (UPPER) clockwise
48
+ this.STATES.UPPER = this.#switchMatrix(this.STATES.UPPER, true);
49
+
50
+ const tempFront = [...this.STATES.FRONT[0]];
51
+ const tempRight = [...this.STATES.RIGHT[0]];
52
+ const tempLeft = [...this.STATES.LEFT[0]];
53
+ const tempBack = [...this.STATES.BACK[0]];
54
+
55
+ this.STATES.FRONT[0] = [...tempRight];
56
+ this.STATES.LEFT[0] = [...tempFront];
57
+ this.STATES.BACK[0] = [...tempLeft];
58
+ this.STATES.RIGHT[0] = [...tempBack];
59
+ } else {
60
+ // Rotate the top layer (UPPER) counterclockwise
61
+ this.STATES.UPPER = this.#switchMatrix(this.STATES.UPPER, false);
62
+
63
+ const tempFront = [...this.STATES.FRONT[0]];
64
+ const tempRight = [...this.STATES.RIGHT[0]];
65
+ const tempLeft = [...this.STATES.LEFT[0]];
66
+ const tempBack = [...this.STATES.BACK[0]];
67
+
68
+ this.STATES.FRONT[0] = [...tempLeft];
69
+ this.STATES.LEFT[0] = [...tempBack];
70
+ this.STATES.BACK[0] = [...tempRight];
71
+ this.STATES.RIGHT[0] = [...tempFront];
72
+ }
73
+ }
74
+
75
+ /**
76
+ * Rotates the front (F) layer clockwise or counterclockwise.
77
+ */
78
+ rotateF(clockwise = true) {
79
+ if (clockwise) {
80
+ this.rotateX(true);
81
+ this.rotateU(true);
82
+ this.rotateX(false);
83
+ } else {
84
+ this.rotateX(true);
85
+ this.rotateU(false);
86
+ this.rotateX(false);
87
+ }
88
+ }
89
+
90
+ rotateR(clockwise = true) {
91
+ if (clockwise) {
92
+ this.rotateY(true);
93
+ this.rotateX(true);
94
+ this.rotateU(true);
95
+ this.rotateX(false);
96
+ this.rotateY(false);
97
+ } else {
98
+ this.rotateY(true);
99
+ this.rotateX(true);
100
+ this.rotateU(false);
101
+ this.rotateX(false);
102
+ this.rotateY(false);
103
+ }
104
+ }
105
+
106
+ rotateL(clockwise = true) {
107
+ if (clockwise) {
108
+ this.rotateY(false);
109
+ this.rotateX(true);
110
+ this.rotateU(true);
111
+ this.rotateX(false);
112
+ this.rotateY(true);
113
+ } else {
114
+ this.rotateY(false);
115
+ this.rotateX(true);
116
+ this.rotateU(false);
117
+ this.rotateX(false);
118
+ this.rotateY(true);
119
+ }
120
+ }
121
+
122
+ rotateD(clockwise = true) {
123
+ if (clockwise) {
124
+ this.rotateX(true);
125
+ this.rotateF(true);
126
+ this.rotateX(false);
127
+ } else {
128
+ this.rotateX(true);
129
+ this.rotateF(false);
130
+ this.rotateX(false);
131
+ }
132
+ }
133
+
134
+ /**
135
+ * Rotates the (x) axis clockwise or counterclockwise.
136
+ */
137
+ rotateX(clockwise = true) {
138
+ const tempFront = structuredClone(this.STATES.FRONT);
139
+ const tempDown = structuredClone(this.STATES.DOWN);
140
+ const tempUpper = structuredClone(this.STATES.UPPER);
141
+ const tempBack = structuredClone(this.STATES.BACK);
142
+ const tempLeft = structuredClone(this.STATES.LEFT);
143
+ const tempRight = structuredClone(this.STATES.RIGHT);
144
+
145
+ if (clockwise) {
146
+ // Rotate the RIGHT and LEFT layers
147
+ this.STATES.LEFT = this.#switchMatrix(tempLeft, false);
148
+ this.STATES.RIGHT = this.#switchMatrix(tempRight, true);
149
+
150
+ // Rotation X axis
151
+ this.STATES.FRONT = [...tempDown];
152
+ this.STATES.UPPER = [...tempFront];
153
+
154
+ // Special permutation
155
+ this.STATES.BACK = this.#specialFlip(tempUpper);
156
+ this.STATES.DOWN = this.#specialFlip(tempBack);
157
+ } else {
158
+ // Rotate the RIGHT and LEFT layers
159
+ this.STATES.LEFT = this.#switchMatrix(tempLeft, true);
160
+ this.STATES.RIGHT = this.#switchMatrix(tempRight, false);
161
+
162
+ // Rotation X axis
163
+ this.STATES.FRONT = [...tempUpper];
164
+ this.STATES.DOWN = [...tempFront];
165
+
166
+ // Special permutation
167
+ this.STATES.BACK = this.#specialFlip(tempDown);
168
+ this.STATES.UPPER = this.#specialFlip(tempBack);
169
+ }
170
+ }
171
+
172
+ /**
173
+ * Rotates the (y) axis clockwise or counterclockwise.
174
+ */
175
+ rotateY(clockwise = true) {
176
+ const tempFront = structuredClone(this.STATES.FRONT);
177
+ const tempRight = structuredClone(this.STATES.RIGHT);
178
+ const tempBack = structuredClone(this.STATES.BACK);
179
+ const tempLeft = structuredClone(this.STATES.LEFT);
180
+
181
+ if (clockwise) {
182
+ this.STATES.UPPER = this.#switchMatrix(this.STATES.UPPER, true);
183
+ this.STATES.DOWN = this.#switchMatrix(this.STATES.DOWN, false);
184
+
185
+ // Rotation X axis
186
+ this.STATES.FRONT = [...tempRight];
187
+ this.STATES.RIGHT = [...tempBack];
188
+ this.STATES.LEFT = [...tempFront];
189
+ this.STATES.BACK = [...tempLeft];
190
+ } else {
191
+ this.STATES.UPPER = this.#switchMatrix(this.STATES.UPPER, false);
192
+ this.STATES.DOWN = this.#switchMatrix(this.STATES.DOWN, true);
193
+
194
+ // Rotation X axis
195
+ this.STATES.FRONT = [...tempLeft];
196
+ this.STATES.RIGHT = [...tempFront];
197
+ this.STATES.LEFT = [...tempBack];
198
+ this.STATES.BACK = [...tempRight];
199
+ }
200
+ }
201
+
202
+ /**
203
+ * Rotate the entire face in the direction set
204
+ */
205
+ #switchMatrix(matrix, clockwise = true) {
206
+ const clone = structuredClone(matrix);
207
+
208
+ const tempMatrix = [...clone[0], ...clone[1], ...clone[2]];
209
+
210
+ if (clockwise) {
211
+ return [
212
+ [tempMatrix[6], tempMatrix[3], tempMatrix[0]],
213
+ [tempMatrix[7], tempMatrix[4], tempMatrix[1]],
214
+ [tempMatrix[8], tempMatrix[5], tempMatrix[2]],
215
+ ];
216
+ } else {
217
+ return [
218
+ [tempMatrix[2], tempMatrix[5], tempMatrix[8]],
219
+ [tempMatrix[1], tempMatrix[4], tempMatrix[7]],
220
+ [tempMatrix[0], tempMatrix[3], tempMatrix[6]],
221
+ ];
222
+ }
223
+ }
224
+
225
+ #specialFlip(matrix) {
226
+ return structuredClone(matrix)
227
+ .reverse()
228
+ .map((row) => [...row].reverse());
229
+ }
230
+
231
+ /**
232
+ * Logs the current state of the cube.
233
+ */
234
+ state() {
235
+ console.clear();
236
+ console.log({
237
+ ...this.STATES,
238
+ });
239
+ return {
240
+ ...this.STATES,
241
+ };
242
+ }
243
+
244
+ /**
245
+ * Indicates if the cube is solve or not in all layers.
246
+ */
247
+ isSolved() {
248
+ const temp = {
249
+ ...this.STATES,
250
+ };
251
+
252
+ const layersSolved = Object.keys(temp).map((layer) => {
253
+ const mixedMatrix = [
254
+ ...temp[layer][0],
255
+ ...temp[layer][1],
256
+ ...temp[layer][2],
257
+ ];
258
+
259
+ const centerColor = mixedMatrix[4];
260
+
261
+ return mixedMatrix.every((currentColor) => currentColor === centerColor);
262
+ });
263
+
264
+ return layersSolved.every((isLayerSolved) => isLayerSolved);
265
+ }
266
+ }
267
+
268
+ export const COLOR = {
269
+ W: ["W", "W", "W", "W", "W", "W", "W", "W", "W"],
270
+ G: ["G", "G", "G", "G", "G", "G", "G", "G", "G"],
271
+ R: ["R", "R", "R", "R", "R", "R", "R", "R", "R"],
272
+ B: ["B", "B", "B", "B", "B", "B", "B", "B", "B"],
273
+ O: ["O", "O", "O", "O", "O", "O", "O", "O", "O"],
274
+ Y: ["Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y"],
275
+ };
package/styles.css ADDED
@@ -0,0 +1,3 @@
1
+ body {
2
+ background-color: beige;
3
+ }
@@ -0,0 +1,649 @@
1
+ import { CubeEngine, COLOR } from "../src/index.js";
2
+
3
+ test("spawn a cube", () => {
4
+ const virtualization = new CubeEngine();
5
+ const state = virtualization.state();
6
+ const result = {
7
+ UPPER: [
8
+ [COLOR.W[0], COLOR.W[1], COLOR.W[2]],
9
+ [COLOR.W[3], COLOR.W[4], COLOR.W[5]],
10
+ [COLOR.W[6], COLOR.W[7], COLOR.W[8]],
11
+ ],
12
+ LEFT: [
13
+ [COLOR.O[0], COLOR.O[1], COLOR.O[2]],
14
+ [COLOR.O[3], COLOR.O[4], COLOR.O[5]],
15
+ [COLOR.O[6], COLOR.O[7], COLOR.O[8]],
16
+ ],
17
+ FRONT: [
18
+ [COLOR.G[0], COLOR.G[1], COLOR.G[2]],
19
+ [COLOR.G[3], COLOR.G[4], COLOR.G[5]],
20
+ [COLOR.G[6], COLOR.G[7], COLOR.G[8]],
21
+ ],
22
+ RIGHT: [
23
+ [COLOR.R[0], COLOR.R[1], COLOR.R[2]],
24
+ [COLOR.R[3], COLOR.R[4], COLOR.R[5]],
25
+ [COLOR.R[6], COLOR.R[7], COLOR.R[8]],
26
+ ],
27
+ BACK: [
28
+ [COLOR.B[0], COLOR.B[1], COLOR.B[2]],
29
+ [COLOR.B[3], COLOR.B[4], COLOR.B[5]],
30
+ [COLOR.B[6], COLOR.B[7], COLOR.B[8]],
31
+ ],
32
+ DOWN: [
33
+ [COLOR.Y[0], COLOR.Y[1], COLOR.Y[2]],
34
+ [COLOR.Y[3], COLOR.Y[4], COLOR.Y[5]],
35
+ [COLOR.Y[6], COLOR.Y[7], COLOR.Y[8]],
36
+ ],
37
+ };
38
+ expect(JSON.stringify(state)).toBe(JSON.stringify(result));
39
+ });
40
+
41
+ test("ROTATE U", () => {
42
+ const virtualization = new CubeEngine();
43
+ virtualization.rotateU();
44
+ const state = virtualization.state();
45
+ const result = {
46
+ UPPER: [
47
+ [COLOR.W[6], COLOR.W[3], COLOR.W[0]],
48
+ [COLOR.W[7], COLOR.W[4], COLOR.W[1]],
49
+ [COLOR.W[8], COLOR.W[5], COLOR.W[2]],
50
+ ],
51
+ LEFT: [
52
+ [COLOR.G[0], COLOR.G[1], COLOR.G[2]],
53
+ [COLOR.O[3], COLOR.O[4], COLOR.O[5]],
54
+ [COLOR.O[6], COLOR.O[7], COLOR.O[8]],
55
+ ],
56
+ FRONT: [
57
+ [COLOR.R[0], COLOR.R[1], COLOR.R[2]],
58
+ [COLOR.G[3], COLOR.G[4], COLOR.G[5]],
59
+ [COLOR.G[6], COLOR.G[7], COLOR.G[8]],
60
+ ],
61
+ RIGHT: [
62
+ [COLOR.B[0], COLOR.B[1], COLOR.B[2]],
63
+ [COLOR.R[3], COLOR.R[4], COLOR.R[5]],
64
+ [COLOR.R[6], COLOR.R[7], COLOR.R[8]],
65
+ ],
66
+ BACK: [
67
+ [COLOR.O[0], COLOR.O[1], COLOR.O[2]],
68
+ [COLOR.B[3], COLOR.B[4], COLOR.B[5]],
69
+ [COLOR.B[6], COLOR.B[7], COLOR.B[8]],
70
+ ],
71
+ DOWN: [
72
+ [COLOR.Y[0], COLOR.Y[1], COLOR.Y[2]],
73
+ [COLOR.Y[3], COLOR.Y[4], COLOR.Y[5]],
74
+ [COLOR.Y[6], COLOR.Y[7], COLOR.Y[8]],
75
+ ],
76
+ };
77
+ expect(JSON.stringify(state)).toBe(JSON.stringify(result));
78
+ });
79
+
80
+ test("ROTATE U'", () => {
81
+ const virtualization = new CubeEngine();
82
+ virtualization.rotateU(false);
83
+ const state = virtualization.state();
84
+ const result = {
85
+ UPPER: [
86
+ [COLOR.W[2], COLOR.W[5], COLOR.W[8]],
87
+ [COLOR.W[1], COLOR.W[4], COLOR.W[7]],
88
+ [COLOR.W[0], COLOR.W[3], COLOR.W[6]],
89
+ ],
90
+ LEFT: [
91
+ [COLOR.B[0], COLOR.B[1], COLOR.B[2]],
92
+ [COLOR.O[3], COLOR.O[4], COLOR.O[5]],
93
+ [COLOR.O[6], COLOR.O[7], COLOR.O[8]],
94
+ ],
95
+ FRONT: [
96
+ [COLOR.O[0], COLOR.O[1], COLOR.O[2]],
97
+ [COLOR.G[3], COLOR.G[4], COLOR.G[5]],
98
+ [COLOR.G[6], COLOR.G[7], COLOR.G[8]],
99
+ ],
100
+ RIGHT: [
101
+ [COLOR.G[0], COLOR.G[1], COLOR.G[2]],
102
+ [COLOR.R[3], COLOR.R[4], COLOR.R[5]],
103
+ [COLOR.R[6], COLOR.R[7], COLOR.R[8]],
104
+ ],
105
+ BACK: [
106
+ [COLOR.R[0], COLOR.R[1], COLOR.R[2]],
107
+ [COLOR.B[3], COLOR.B[4], COLOR.B[5]],
108
+ [COLOR.B[6], COLOR.B[7], COLOR.B[8]],
109
+ ],
110
+ DOWN: [
111
+ [COLOR.Y[0], COLOR.Y[1], COLOR.Y[2]],
112
+ [COLOR.Y[3], COLOR.Y[4], COLOR.Y[5]],
113
+ [COLOR.Y[6], COLOR.Y[7], COLOR.Y[8]],
114
+ ],
115
+ };
116
+ expect(JSON.stringify(state)).toBe(JSON.stringify(result));
117
+ });
118
+
119
+ test("ROTATE F", () => {
120
+ const virtualization = new CubeEngine();
121
+ virtualization.rotateF(true);
122
+ const state = virtualization.state();
123
+ const result = {
124
+ UPPER: [
125
+ [COLOR.W[0], COLOR.W[1], COLOR.W[2]],
126
+ [COLOR.W[3], COLOR.W[4], COLOR.W[5]],
127
+ [COLOR.O[8], COLOR.O[5], COLOR.O[2]],
128
+ ],
129
+ LEFT: [
130
+ [COLOR.O[0], COLOR.O[1], COLOR.Y[0]],
131
+ [COLOR.O[3], COLOR.O[4], COLOR.Y[1]],
132
+ [COLOR.O[6], COLOR.O[7], COLOR.Y[2]],
133
+ ],
134
+ FRONT: [
135
+ [COLOR.G[6], COLOR.G[3], COLOR.G[0]],
136
+ [COLOR.G[7], COLOR.G[4], COLOR.G[1]],
137
+ [COLOR.G[8], COLOR.G[5], COLOR.G[2]],
138
+ ],
139
+ RIGHT: [
140
+ [COLOR.W[6], COLOR.R[1], COLOR.R[2]],
141
+ [COLOR.W[7], COLOR.R[4], COLOR.R[5]],
142
+ [COLOR.W[8], COLOR.R[7], COLOR.R[8]],
143
+ ],
144
+ BACK: [
145
+ [COLOR.B[0], COLOR.B[1], COLOR.B[2]],
146
+ [COLOR.B[3], COLOR.B[4], COLOR.B[5]],
147
+ [COLOR.B[6], COLOR.B[7], COLOR.B[8]],
148
+ ],
149
+ DOWN: [
150
+ [COLOR.R[2], COLOR.R[1], COLOR.R[0]],
151
+ [COLOR.Y[3], COLOR.Y[4], COLOR.Y[5]],
152
+ [COLOR.Y[6], COLOR.Y[7], COLOR.Y[8]],
153
+ ],
154
+ };
155
+ expect(JSON.stringify(state)).toBe(JSON.stringify(result));
156
+ });
157
+
158
+ test("ROTATE F'", () => {
159
+ const virtualization = new CubeEngine();
160
+ virtualization.rotateF(false);
161
+ const state = virtualization.state();
162
+ const result = {
163
+ UPPER: [
164
+ [COLOR.W[0], COLOR.W[1], COLOR.W[2]],
165
+ [COLOR.W[3], COLOR.W[4], COLOR.W[5]],
166
+ [COLOR.R[8], COLOR.R[5], COLOR.R[2]],
167
+ ],
168
+ LEFT: [
169
+ [COLOR.O[0], COLOR.O[1], COLOR.W[0]],
170
+ [COLOR.O[3], COLOR.O[4], COLOR.W[1]],
171
+ [COLOR.O[6], COLOR.O[7], COLOR.W[2]],
172
+ ],
173
+ FRONT: [
174
+ [COLOR.G[6], COLOR.G[3], COLOR.G[0]],
175
+ [COLOR.G[7], COLOR.G[4], COLOR.G[1]],
176
+ [COLOR.G[8], COLOR.G[5], COLOR.G[2]],
177
+ ],
178
+ RIGHT: [
179
+ [COLOR.Y[6], COLOR.R[1], COLOR.R[2]],
180
+ [COLOR.Y[7], COLOR.R[4], COLOR.R[5]],
181
+ [COLOR.Y[8], COLOR.R[7], COLOR.R[8]],
182
+ ],
183
+ BACK: [
184
+ [COLOR.B[0], COLOR.B[1], COLOR.B[2]],
185
+ [COLOR.B[3], COLOR.B[4], COLOR.B[5]],
186
+ [COLOR.B[6], COLOR.B[7], COLOR.B[8]],
187
+ ],
188
+ DOWN: [
189
+ [COLOR.O[2], COLOR.O[1], COLOR.O[0]],
190
+ [COLOR.Y[3], COLOR.Y[4], COLOR.Y[5]],
191
+ [COLOR.Y[6], COLOR.Y[7], COLOR.Y[8]],
192
+ ],
193
+ };
194
+ expect(JSON.stringify(state)).toBe(JSON.stringify(result));
195
+ });
196
+
197
+ test("ROTATE x", () => {
198
+ const virtualization = new CubeEngine();
199
+ virtualization.rotateX();
200
+ const state = virtualization.state();
201
+ const result = {
202
+ UPPER: [
203
+ [COLOR.G[0], COLOR.G[1], COLOR.G[2]],
204
+ [COLOR.G[3], COLOR.G[4], COLOR.G[5]],
205
+ [COLOR.G[6], COLOR.G[7], COLOR.G[8]],
206
+ ],
207
+ LEFT: [
208
+ [COLOR.O[2], COLOR.O[5], COLOR.O[8]],
209
+ [COLOR.O[1], COLOR.O[4], COLOR.O[7]],
210
+ [COLOR.O[0], COLOR.O[3], COLOR.O[6]],
211
+ ],
212
+ FRONT: [
213
+ [COLOR.Y[0], COLOR.Y[1], COLOR.Y[2]],
214
+ [COLOR.Y[3], COLOR.Y[4], COLOR.Y[5]],
215
+ [COLOR.Y[6], COLOR.Y[7], COLOR.Y[8]],
216
+ ],
217
+ RIGHT: [
218
+ [COLOR.R[6], COLOR.R[3], COLOR.R[0]],
219
+ [COLOR.R[7], COLOR.R[4], COLOR.R[1]],
220
+ [COLOR.R[8], COLOR.R[5], COLOR.R[2]],
221
+ ],
222
+ BACK: [
223
+ [COLOR.W[8], COLOR.W[7], COLOR.W[6]],
224
+ [COLOR.W[5], COLOR.W[4], COLOR.W[3]],
225
+ [COLOR.W[2], COLOR.W[1], COLOR.W[0]],
226
+ ],
227
+ DOWN: [
228
+ [COLOR.B[0], COLOR.B[1], COLOR.B[2]],
229
+ [COLOR.B[3], COLOR.B[4], COLOR.B[5]],
230
+ [COLOR.B[6], COLOR.B[7], COLOR.B[8]],
231
+ ],
232
+ };
233
+ expect(JSON.stringify(state)).toBe(JSON.stringify(result));
234
+ });
235
+
236
+ test("ROTATE x'", () => {
237
+ const virtualization = new CubeEngine();
238
+ virtualization.rotateX(false);
239
+ const state = virtualization.state();
240
+ const result = {
241
+ UPPER: [
242
+ [COLOR.B[6], COLOR.B[7], COLOR.B[8]],
243
+ [COLOR.B[3], COLOR.B[4], COLOR.B[5]],
244
+ [COLOR.B[0], COLOR.B[1], COLOR.B[2]],
245
+ ],
246
+ LEFT: [
247
+ [COLOR.O[6], COLOR.O[3], COLOR.O[0]],
248
+ [COLOR.O[7], COLOR.O[4], COLOR.O[1]],
249
+ [COLOR.O[8], COLOR.O[5], COLOR.O[2]],
250
+ ],
251
+ FRONT: [
252
+ [COLOR.W[0], COLOR.W[1], COLOR.W[2]],
253
+ [COLOR.W[3], COLOR.W[4], COLOR.W[5]],
254
+ [COLOR.W[6], COLOR.W[7], COLOR.W[8]],
255
+ ],
256
+ RIGHT: [
257
+ [COLOR.R[2], COLOR.R[5], COLOR.R[8]],
258
+ [COLOR.R[1], COLOR.R[4], COLOR.R[7]],
259
+ [COLOR.R[0], COLOR.R[3], COLOR.R[6]],
260
+ ],
261
+ BACK: [
262
+ [COLOR.Y[8], COLOR.Y[7], COLOR.Y[6]],
263
+ [COLOR.Y[5], COLOR.Y[4], COLOR.Y[3]],
264
+ [COLOR.Y[2], COLOR.Y[1], COLOR.Y[0]],
265
+ ],
266
+ DOWN: [
267
+ [COLOR.G[0], COLOR.G[1], COLOR.G[2]],
268
+ [COLOR.G[3], COLOR.G[4], COLOR.G[5]],
269
+ [COLOR.G[6], COLOR.G[7], COLOR.G[8]],
270
+ ],
271
+ };
272
+ expect(JSON.stringify(state)).toBe(JSON.stringify(result));
273
+ });
274
+
275
+ test("ROTATE y", () => {
276
+ const virtualization = new CubeEngine();
277
+ virtualization.rotateY();
278
+ const state = virtualization.state();
279
+ const result = {
280
+ UPPER: [
281
+ [COLOR.W[6], COLOR.W[3], COLOR.W[0]],
282
+ [COLOR.W[7], COLOR.W[4], COLOR.W[1]],
283
+ [COLOR.W[8], COLOR.W[5], COLOR.W[2]],
284
+ ],
285
+ LEFT: [
286
+ [COLOR.G[0], COLOR.G[1], COLOR.G[2]],
287
+ [COLOR.G[3], COLOR.G[4], COLOR.G[5]],
288
+ [COLOR.G[6], COLOR.G[7], COLOR.G[8]],
289
+ ],
290
+ FRONT: [
291
+ [COLOR.R[0], COLOR.R[1], COLOR.R[2]],
292
+ [COLOR.R[3], COLOR.R[4], COLOR.R[5]],
293
+ [COLOR.R[6], COLOR.R[7], COLOR.R[8]],
294
+ ],
295
+ RIGHT: [
296
+ [COLOR.B[0], COLOR.B[1], COLOR.B[2]],
297
+ [COLOR.B[3], COLOR.B[4], COLOR.B[5]],
298
+ [COLOR.B[6], COLOR.B[7], COLOR.B[8]],
299
+ ],
300
+ BACK: [
301
+ [COLOR.O[0], COLOR.O[1], COLOR.O[2]],
302
+ [COLOR.O[3], COLOR.O[4], COLOR.O[5]],
303
+ [COLOR.O[6], COLOR.O[7], COLOR.O[8]],
304
+ ],
305
+ DOWN: [
306
+ [COLOR.Y[2], COLOR.Y[5], COLOR.Y[8]],
307
+ [COLOR.Y[1], COLOR.Y[4], COLOR.Y[7]],
308
+ [COLOR.Y[0], COLOR.Y[3], COLOR.Y[6]],
309
+ ],
310
+ };
311
+ expect(JSON.stringify(state)).toBe(JSON.stringify(result));
312
+ });
313
+
314
+ test("ROTATE y'", () => {
315
+ const virtualization = new CubeEngine();
316
+ virtualization.rotateY(false);
317
+ const state = virtualization.state();
318
+ const result = {
319
+ UPPER: [
320
+ [COLOR.W[2], COLOR.W[5], COLOR.W[8]],
321
+ [COLOR.W[1], COLOR.W[4], COLOR.W[7]],
322
+ [COLOR.W[0], COLOR.W[3], COLOR.W[6]],
323
+ ],
324
+ LEFT: [
325
+ [COLOR.B[0], COLOR.B[1], COLOR.B[2]],
326
+ [COLOR.B[3], COLOR.B[4], COLOR.B[5]],
327
+ [COLOR.B[6], COLOR.B[7], COLOR.B[8]],
328
+ ],
329
+ FRONT: [
330
+ [COLOR.O[0], COLOR.O[1], COLOR.O[2]],
331
+ [COLOR.O[3], COLOR.O[4], COLOR.O[5]],
332
+ [COLOR.O[6], COLOR.O[7], COLOR.O[8]],
333
+ ],
334
+ RIGHT: [
335
+ [COLOR.G[0], COLOR.G[1], COLOR.G[2]],
336
+ [COLOR.G[3], COLOR.G[4], COLOR.G[5]],
337
+ [COLOR.G[6], COLOR.G[7], COLOR.G[8]],
338
+ ],
339
+ BACK: [
340
+ [COLOR.R[0], COLOR.R[1], COLOR.R[2]],
341
+ [COLOR.R[3], COLOR.R[4], COLOR.R[5]],
342
+ [COLOR.R[6], COLOR.R[7], COLOR.R[8]],
343
+ ],
344
+ DOWN: [
345
+ [COLOR.Y[6], COLOR.Y[3], COLOR.Y[0]],
346
+ [COLOR.Y[7], COLOR.Y[4], COLOR.Y[1]],
347
+ [COLOR.Y[8], COLOR.Y[5], COLOR.Y[2]],
348
+ ],
349
+ };
350
+ expect(JSON.stringify(state)).toBe(JSON.stringify(result));
351
+ });
352
+
353
+ test("ROTATE R", () => {
354
+ const virtualization = new CubeEngine();
355
+ virtualization.rotateR(true);
356
+ const state = virtualization.state();
357
+ const result = {
358
+ UPPER: [
359
+ [COLOR.W[0], COLOR.W[1], COLOR.G[2]],
360
+ [COLOR.W[3], COLOR.W[4], COLOR.G[5]],
361
+ [COLOR.W[6], COLOR.W[7], COLOR.G[8]],
362
+ ],
363
+ LEFT: [
364
+ [COLOR.O[0], COLOR.O[1], COLOR.O[2]],
365
+ [COLOR.O[3], COLOR.O[4], COLOR.O[5]],
366
+ [COLOR.O[6], COLOR.O[7], COLOR.O[8]],
367
+ ],
368
+ FRONT: [
369
+ [COLOR.G[0], COLOR.G[1], COLOR.Y[2]],
370
+ [COLOR.G[3], COLOR.G[4], COLOR.Y[5]],
371
+ [COLOR.G[6], COLOR.G[7], COLOR.Y[8]],
372
+ ],
373
+ RIGHT: [
374
+ [COLOR.R[0], COLOR.R[1], COLOR.R[2]],
375
+ [COLOR.R[3], COLOR.R[4], COLOR.R[5]],
376
+ [COLOR.R[6], COLOR.R[7], COLOR.R[8]],
377
+ ],
378
+ BACK: [
379
+ [COLOR.W[0], COLOR.B[1], COLOR.B[2]],
380
+ [COLOR.W[3], COLOR.B[4], COLOR.B[5]],
381
+ [COLOR.W[6], COLOR.B[7], COLOR.B[8]],
382
+ ],
383
+ DOWN: [
384
+ [COLOR.Y[0], COLOR.Y[1], COLOR.B[2]],
385
+ [COLOR.Y[3], COLOR.Y[4], COLOR.B[5]],
386
+ [COLOR.Y[6], COLOR.Y[7], COLOR.B[8]],
387
+ ],
388
+ };
389
+ expect(JSON.stringify(state)).toBe(JSON.stringify(result));
390
+ });
391
+
392
+ test("ROTATE R'", () => {
393
+ const virtualization = new CubeEngine();
394
+ virtualization.rotateR(false);
395
+ const state = virtualization.state();
396
+ const result = {
397
+ UPPER: [
398
+ [COLOR.W[0], COLOR.W[1], COLOR.B[2]],
399
+ [COLOR.W[3], COLOR.W[4], COLOR.B[5]],
400
+ [COLOR.W[6], COLOR.W[7], COLOR.B[8]],
401
+ ],
402
+ LEFT: [
403
+ [COLOR.O[0], COLOR.O[1], COLOR.O[2]],
404
+ [COLOR.O[3], COLOR.O[4], COLOR.O[5]],
405
+ [COLOR.O[6], COLOR.O[7], COLOR.O[8]],
406
+ ],
407
+ FRONT: [
408
+ [COLOR.G[0], COLOR.G[1], COLOR.W[2]],
409
+ [COLOR.G[3], COLOR.G[4], COLOR.W[5]],
410
+ [COLOR.G[6], COLOR.G[7], COLOR.W[8]],
411
+ ],
412
+ RIGHT: [
413
+ [COLOR.R[0], COLOR.R[1], COLOR.R[2]],
414
+ [COLOR.R[3], COLOR.R[4], COLOR.R[5]],
415
+ [COLOR.R[6], COLOR.R[7], COLOR.R[8]],
416
+ ],
417
+ BACK: [
418
+ [COLOR.Y[0], COLOR.B[1], COLOR.B[2]],
419
+ [COLOR.Y[3], COLOR.B[4], COLOR.B[5]],
420
+ [COLOR.Y[6], COLOR.B[7], COLOR.B[8]],
421
+ ],
422
+ DOWN: [
423
+ [COLOR.Y[0], COLOR.Y[1], COLOR.G[2]],
424
+ [COLOR.Y[3], COLOR.Y[4], COLOR.G[5]],
425
+ [COLOR.Y[6], COLOR.Y[7], COLOR.G[8]],
426
+ ],
427
+ };
428
+ expect(JSON.stringify(state)).toBe(JSON.stringify(result));
429
+ });
430
+
431
+ test("ROTATE L", () => {
432
+ const virtualization = new CubeEngine();
433
+ virtualization.rotateL(true);
434
+ const state = virtualization.state();
435
+ const result = {
436
+ UPPER: [
437
+ [COLOR.B[0], COLOR.W[1], COLOR.W[2]],
438
+ [COLOR.B[3], COLOR.W[4], COLOR.W[5]],
439
+ [COLOR.B[6], COLOR.W[7], COLOR.W[8]],
440
+ ],
441
+ LEFT: [
442
+ [COLOR.O[0], COLOR.O[1], COLOR.O[2]],
443
+ [COLOR.O[3], COLOR.O[4], COLOR.O[5]],
444
+ [COLOR.O[6], COLOR.O[7], COLOR.O[8]],
445
+ ],
446
+ FRONT: [
447
+ [COLOR.W[0], COLOR.G[1], COLOR.G[2]],
448
+ [COLOR.W[3], COLOR.G[4], COLOR.G[5]],
449
+ [COLOR.W[6], COLOR.G[7], COLOR.G[8]],
450
+ ],
451
+ RIGHT: [
452
+ [COLOR.R[0], COLOR.R[1], COLOR.R[2]],
453
+ [COLOR.R[3], COLOR.R[4], COLOR.R[5]],
454
+ [COLOR.R[6], COLOR.R[7], COLOR.R[8]],
455
+ ],
456
+ BACK: [
457
+ [COLOR.B[0], COLOR.B[1], COLOR.Y[2]],
458
+ [COLOR.B[3], COLOR.B[4], COLOR.Y[5]],
459
+ [COLOR.B[6], COLOR.B[7], COLOR.Y[8]],
460
+ ],
461
+ DOWN: [
462
+ [COLOR.G[0], COLOR.Y[1], COLOR.Y[2]],
463
+ [COLOR.G[3], COLOR.Y[4], COLOR.Y[5]],
464
+ [COLOR.G[6], COLOR.Y[7], COLOR.Y[8]],
465
+ ],
466
+ };
467
+ expect(JSON.stringify(state)).toBe(JSON.stringify(result));
468
+ });
469
+
470
+ test("ROTATE L'", () => {
471
+ const virtualization = new CubeEngine();
472
+ virtualization.rotateL(false);
473
+ const state = virtualization.state();
474
+ const result = {
475
+ UPPER: [
476
+ [COLOR.G[0], COLOR.W[1], COLOR.W[2]],
477
+ [COLOR.G[3], COLOR.W[4], COLOR.W[5]],
478
+ [COLOR.G[6], COLOR.W[7], COLOR.W[8]],
479
+ ],
480
+ LEFT: [
481
+ [COLOR.O[0], COLOR.O[1], COLOR.O[2]],
482
+ [COLOR.O[3], COLOR.O[4], COLOR.O[5]],
483
+ [COLOR.O[6], COLOR.O[7], COLOR.O[8]],
484
+ ],
485
+ FRONT: [
486
+ [COLOR.Y[0], COLOR.G[1], COLOR.G[2]],
487
+ [COLOR.Y[3], COLOR.G[4], COLOR.G[5]],
488
+ [COLOR.Y[6], COLOR.G[7], COLOR.G[8]],
489
+ ],
490
+ RIGHT: [
491
+ [COLOR.R[0], COLOR.R[1], COLOR.R[2]],
492
+ [COLOR.R[3], COLOR.R[4], COLOR.R[5]],
493
+ [COLOR.R[6], COLOR.R[7], COLOR.R[8]],
494
+ ],
495
+ BACK: [
496
+ [COLOR.B[0], COLOR.B[1], COLOR.W[2]],
497
+ [COLOR.B[3], COLOR.B[4], COLOR.W[5]],
498
+ [COLOR.B[6], COLOR.B[7], COLOR.W[8]],
499
+ ],
500
+ DOWN: [
501
+ [COLOR.B[0], COLOR.Y[1], COLOR.Y[2]],
502
+ [COLOR.B[3], COLOR.Y[4], COLOR.Y[5]],
503
+ [COLOR.B[6], COLOR.Y[7], COLOR.Y[8]],
504
+ ],
505
+ };
506
+ expect(JSON.stringify(state)).toBe(JSON.stringify(result));
507
+ });
508
+
509
+ test("ROTATE D", () => {
510
+ const virtualization = new CubeEngine();
511
+ virtualization.rotateD(true);
512
+ const state = virtualization.state();
513
+ const result = {
514
+ UPPER: [
515
+ [COLOR.W[0], COLOR.W[1], COLOR.W[2]],
516
+ [COLOR.W[3], COLOR.W[4], COLOR.W[5]],
517
+ [COLOR.W[6], COLOR.W[7], COLOR.W[8]],
518
+ ],
519
+ LEFT: [
520
+ [COLOR.O[0], COLOR.O[1], COLOR.O[2]],
521
+ [COLOR.O[3], COLOR.O[4], COLOR.O[5]],
522
+ [COLOR.B[6], COLOR.B[7], COLOR.B[8]],
523
+ ],
524
+ FRONT: [
525
+ [COLOR.G[0], COLOR.G[1], COLOR.G[2]],
526
+ [COLOR.G[3], COLOR.G[4], COLOR.G[5]],
527
+ [COLOR.O[6], COLOR.O[7], COLOR.O[8]],
528
+ ],
529
+ RIGHT: [
530
+ [COLOR.R[0], COLOR.R[1], COLOR.R[2]],
531
+ [COLOR.R[3], COLOR.R[4], COLOR.R[5]],
532
+ [COLOR.G[6], COLOR.G[7], COLOR.G[8]],
533
+ ],
534
+ BACK: [
535
+ [COLOR.B[0], COLOR.B[1], COLOR.B[2]],
536
+ [COLOR.B[3], COLOR.B[4], COLOR.B[5]],
537
+ [COLOR.R[6], COLOR.R[7], COLOR.R[8]],
538
+ ],
539
+ DOWN: [
540
+ [COLOR.Y[0], COLOR.Y[1], COLOR.Y[2]],
541
+ [COLOR.Y[3], COLOR.Y[4], COLOR.Y[5]],
542
+ [COLOR.Y[6], COLOR.Y[7], COLOR.Y[8]],
543
+ ],
544
+ };
545
+ expect(JSON.stringify(state)).toBe(JSON.stringify(result));
546
+ });
547
+
548
+ test("ROTATE D'", () => {
549
+ const virtualization = new CubeEngine();
550
+ virtualization.rotateD(false);
551
+ const state = virtualization.state();
552
+ const result = {
553
+ UPPER: [
554
+ [COLOR.W[0], COLOR.W[1], COLOR.W[2]],
555
+ [COLOR.W[3], COLOR.W[4], COLOR.W[5]],
556
+ [COLOR.W[6], COLOR.W[7], COLOR.W[8]],
557
+ ],
558
+ LEFT: [
559
+ [COLOR.O[0], COLOR.O[1], COLOR.O[2]],
560
+ [COLOR.O[3], COLOR.O[4], COLOR.O[5]],
561
+ [COLOR.G[6], COLOR.G[7], COLOR.G[8]],
562
+ ],
563
+ FRONT: [
564
+ [COLOR.G[0], COLOR.G[1], COLOR.G[2]],
565
+ [COLOR.G[3], COLOR.G[4], COLOR.G[5]],
566
+ [COLOR.R[6], COLOR.R[7], COLOR.R[8]],
567
+ ],
568
+ RIGHT: [
569
+ [COLOR.R[0], COLOR.R[1], COLOR.R[2]],
570
+ [COLOR.R[3], COLOR.R[4], COLOR.R[5]],
571
+ [COLOR.B[6], COLOR.B[7], COLOR.B[8]],
572
+ ],
573
+ BACK: [
574
+ [COLOR.B[0], COLOR.B[1], COLOR.B[2]],
575
+ [COLOR.B[3], COLOR.B[4], COLOR.B[5]],
576
+ [COLOR.O[6], COLOR.O[7], COLOR.O[8]],
577
+ ],
578
+ DOWN: [
579
+ [COLOR.Y[0], COLOR.Y[1], COLOR.Y[2]],
580
+ [COLOR.Y[3], COLOR.Y[4], COLOR.Y[5]],
581
+ [COLOR.Y[6], COLOR.Y[7], COLOR.Y[8]],
582
+ ],
583
+ };
584
+ expect(JSON.stringify(state)).toBe(JSON.stringify(result));
585
+ });
586
+
587
+ test("U' U L R D' F L F' x' L D y' U' L D y' R D y' L x L D y' L F", () => {
588
+ const virtualization = new CubeEngine();
589
+ virtualization.rotateU(false);
590
+ virtualization.rotateU();
591
+ virtualization.rotateL();
592
+ virtualization.rotateR();
593
+ virtualization.rotateD(false);
594
+ virtualization.rotateF();
595
+ virtualization.rotateL();
596
+ virtualization.rotateF(false);
597
+ virtualization.rotateX(false);
598
+ virtualization.rotateL();
599
+ virtualization.rotateD();
600
+ virtualization.rotateY(false);
601
+ virtualization.rotateU(false);
602
+ virtualization.rotateL();
603
+ virtualization.rotateD();
604
+ virtualization.rotateY(false);
605
+ virtualization.rotateR();
606
+ virtualization.rotateD();
607
+ virtualization.rotateY(false);
608
+ virtualization.rotateL();
609
+ virtualization.rotateX();
610
+ virtualization.rotateL();
611
+ virtualization.rotateD();
612
+ virtualization.rotateY(false);
613
+ virtualization.rotateL();
614
+ virtualization.rotateF();
615
+ const state = virtualization.state();
616
+ const result = {
617
+ UPPER: [
618
+ [COLOR.O[0], COLOR.Y[1], COLOR.W[2]],
619
+ [COLOR.Y[3], COLOR.R[4], COLOR.O[5]],
620
+ [COLOR.O[6], COLOR.B[7], COLOR.G[8]],
621
+ ],
622
+ LEFT: [
623
+ [COLOR.W[0], COLOR.B[1], COLOR.Y[2]],
624
+ [COLOR.O[3], COLOR.B[4], COLOR.W[5]],
625
+ [COLOR.B[6], COLOR.Y[7], COLOR.O[8]],
626
+ ],
627
+ FRONT: [
628
+ [COLOR.G[0], COLOR.W[1], COLOR.Y[2]],
629
+ [COLOR.R[3], COLOR.W[4], COLOR.O[5]],
630
+ [COLOR.Y[6], COLOR.B[7], COLOR.G[8]],
631
+ ],
632
+ RIGHT: [
633
+ [COLOR.R[0], COLOR.B[1], COLOR.B[2]],
634
+ [COLOR.G[3], COLOR.G[4], COLOR.Y[5]],
635
+ [COLOR.W[6], COLOR.G[7], COLOR.B[8]],
636
+ ],
637
+ BACK: [
638
+ [COLOR.O[0], COLOR.R[1], COLOR.G[2]],
639
+ [COLOR.O[3], COLOR.Y[4], COLOR.W[5]],
640
+ [COLOR.R[6], COLOR.G[7], COLOR.R[8]],
641
+ ],
642
+ DOWN: [
643
+ [COLOR.B[0], COLOR.R[1], COLOR.R[2]],
644
+ [COLOR.G[3], COLOR.O[4], COLOR.R[5]],
645
+ [COLOR.Y[6], COLOR.W[7], COLOR.W[8]],
646
+ ],
647
+ };
648
+ expect(JSON.stringify(state)).toBe(JSON.stringify(result));
649
+ });