-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
79 lines (68 loc) · 2.08 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { CubeEngine } from "./src/index.js";
window.CubeEngine = new CubeEngine();
render();
const player = document.querySelector("twisty-player");
const CE = window.CubeEngine;
const movesMap = {
f: { move: () => CE.rotateU(false) },
j: { move: () => CE.rotateU(true) },
g: { move: () => CE.rotateF(false) },
h: { move: () => CE.rotateF(true) },
i: { move: () => CE.rotateR(true) },
k: { move: () => CE.rotateR(false) },
d: { move: () => CE.rotateL(true) },
e: { move: () => CE.rotateL(false) },
t: { move: () => CE.rotateX(true) },
y: { move: () => CE.rotateX(true) },
b: { move: () => CE.rotateX(false) },
n: { move: () => CE.rotateX(false) },
ñ: { move: () => CE.rotateY(true) },
a: { move: () => CE.rotateY(false) },
s: { move: () => CE.rotateD(true) },
l: { move: () => CE.rotateD(false) },
};
/**
* Execute a move and update the UI.
* @param {string} key - The key corresponding to the movement.
*/
function executeMove(key) {
const moveObj = movesMap[key];
if (moveObj) {
// Execute the movement
moveObj.move();
const moves = CE.getMoves(false);
// Update the user interface
document.querySelector("#total").textContent = moves.length;
document.querySelector("#moves").textContent = moves.join(" ");
document.querySelector(
"#is-solve"
).textContent = `${window.CubeEngine.isSolved()}`;
player.alg = moves.join(" ");
render();
}
}
// setInterval for random movements
setInterval(() => {
const keys = Object.keys(movesMap);
const randomKey = keys[Math.floor(Math.random() * keys.length)];
executeMove(randomKey);
}, 250);
/**
* Key listener for cube movements.
*/
document.addEventListener("keyup", (e) => {
const key = e.key.toLowerCase();
executeMove(key);
});
function render() {
const state = window.CubeEngine.state();
Object.keys(state).forEach((layer) => {
const layerContent = state[layer]
.map(
(row) =>
`<div>${row.map((cell) => `<span>${cell}</span>`).join("")}</div>`
)
.join("");
document.querySelector(`#${layer}`).innerHTML = layerContent;
});
}