-
Notifications
You must be signed in to change notification settings - Fork 0
/
js_module_cube.js
88 lines (88 loc) · 3.39 KB
/
js_module_cube.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
80
81
82
83
84
85
86
87
88
let cube = [
[
['W', 'W', 'W'],
['W', 'W', 'W'],
['W', 'W', 'W'] // Top
],
[
['G', 'G', 'G'],
['G', 'G', 'G'],
['G', 'G', 'G'] // Front
],
[
['R', 'R', 'R'],
['R', 'R', 'R'],
['R', 'R', 'R'] // Side
],
[
['B', 'B', 'B'],
['B', 'B', 'B'],
['B', 'B', 'B']
],
[
['O', 'O', 'O'],
['O', 'O', 'O'],
['O', 'O', 'O']
],
[
['Y', 'Y', 'Y'],
['Y', 'Y', 'Y'],
['Y', 'Y', 'Y']
],
];
document.querySelectorAll('.cube-notation').forEach(move => move.addEventListener('click', ()=>{
cube_notation(move);
}));
function cube_notation(move){
const notation = move.innerHTML;
if (['U','U\'','D','D\''].includes(notation)){
let layer = 0;
let direction = 1;
let lateral_cube = structuredClone(cube.slice(1,-1));
const cube_g = structuredClone(cube[1]);
if (['D','U\''].includes(notation)) direction = -1;
if (['D','D\''].includes(notation)) layer = 2;
for (let i = 0, n = lateral_cube.length; i < n; i++){
if (i+direction == 4) cube[4][layer] = cube_g[layer];
else cube[i+1][layer] = lateral_cube.at(i+direction)[layer];
};
}else if (['R','R\'','L','L\''].includes(notation)){
let layer = 2;
let direction = -1;
let front_cube = structuredClone([cube[1],cube[0],cube[3],cube[5]]);
let front_cube_clone = structuredClone(front_cube);
if (['R\'','L'].includes(notation)) direction = 1;
if (['L','L\''].includes(notation)) layer = 0;
for (let i = 0, n = front_cube.length; i < n; i++){
for (let j = 0; j < 3; j++){
if (i == 2) front_cube[i][j][Math.abs(layer-2)] = front_cube_clone.at(i+direction)[j][layer]; // To inverse the B
else if (i+direction == 4) front_cube[i][j][layer] = front_cube_clone[0][j][layer];
else if (i == 3 || i == direction) front_cube[i][j][layer] = front_cube_clone.at(i+direction)[j][Math.abs(layer-2)] // To inverse of B on Y
else front_cube[i][j][layer] = front_cube_clone.at(i+direction)[j][layer];
};
};
for (const[i,j] of Object.entries({0:1,1:0,2:3,3:5})){
cube[j] = front_cube[i];
};
}else if (['F','F\'','B','B\''].includes(notation)){
let layer = 2;
let sel = 0;
let direction = -1;
let dynamic_cube = structuredClone([cube[0],cube[2],cube[5],cube[4]]);
let dynamic_cube_copy = structuredClone(dynamic_cube);
if (['F\'','B'].includes(notation)) direction = 1;
if (['F\'','B\''].includes(notation)) sel = 2;
if (['B','B\''].includes(notation)) layer = 0;
for (let j = 0; j < 3; j++){
dynamic_cube[0][layer][j] = dynamic_cube_copy.at(0+direction)[j][Math.abs(sel-2)];
dynamic_cube[1][j][Math.abs(layer-2)] = dynamic_cube_copy[1+direction][Math.abs(sel-2)][j];
dynamic_cube[2][Math.abs(layer-2)][j] = dynamic_cube_copy[2+direction][j][sel];
if (3 + direction == 4) dynamic_cube[3][j][layer] = dynamic_cube_copy[0][sel][j];
else dynamic_cube[3][j][layer] = dynamic_cube_copy[3+direction][sel][j];
};
for (const[i,j] of Object.entries({0:0,1:2,2:5,3:4})){
cube[j] = dynamic_cube[i];
};
};
console.log(cube);
};