-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithm.js
396 lines (369 loc) · 12.3 KB
/
algorithm.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
let shuffleBool = false;
let playBool = false;
function keyTyped() {
if (!shuffleBool) {
shuffleBool = true;
shuffleCube(20);
} else {
if (turnQueue == 0 && playBool == false) {
playBool = true;
}
}
}
function shuffleCube(steps) {
let slot = ['F','~F','R','~R','L','~L','U','~U','D','~D','B','~B'];
setView();
for (let i = 0; i < steps; i++) {
index = Math.floor(Math.random() * slot.length);
pushTurns([slot[index]]);
}
}
let playStep = 0;
function play() {
if (playBool == true && turnQueue.length == 0 && animate == false) {
switch (playStep) {
case 0:
if (makeBottomPlus()) { // Needs optimization. Reduce probablity of flipped colors at edge-bottom.
playStep++;
}
break;
case 1:
if (makeBottomCorner()) { // Needs optimization. Reduce propablity of flipped colors at bottom - corner.
playStep++;
}
break;
case 2:
if (makeSideEdges()) {
playStep++;
}
break;
case 3:
if (makeTopCross()) {
playStep++;
}
break;
case 4:
if (makeTopWhite()) {
playStep++;
}
break;
case 5:
if (alignWhiteCorners()) {
playStep++;
}
break;
case 6:
if (alignWhiteEdges()) {
playStep++;
}
break;
case 7:
console.log('Done');
break;
}
}
}
function alignWhiteEdges() {
let topEdges = getCubiesAtYDefault(-1).filter((qb) => abs(qb.defaultPos.x + qb.defaultPos.z) == 1);
if (topEdges.every((c) => c.pos.equals(c.defaultPos))) {
return true;
} else {
if (checkIfAllEdgeWrong(topEdges)) {
return false;
}
for (let i = 0; i < topEdges.length; i++) {
let qb = topEdges[i];
setView(qb);
if (checkTriEdge(topEdges)) {
return false;
}
}
}
}
function checkTriEdge(topEdges) {
if ((topEdges.some((c) => c.vPos.x == 1 && c.vPos.z == 0 && c.vPos.equals(c.vDefaultPos) == false)) &&
(topEdges.some((c) => c.vPos.x == -1 && c.vPos.z == 0 && c.vPos.equals(c.vDefaultPos) == false)) &&
(topEdges.some((c) => c.vPos.x == 0 && c.vPos.z == 1 && c.vPos.equals(c.vDefaultPos) == false))) {
let center = topEdges.find((c) => c.vPos.x == 0 && c.vPos.z == 1);
if (center.vDefaultPos.x == 1) { //Anti clock
pushTurns(['F','F','~U','L','~R','F','F','~L','R','~U','F','F']);
} else { //Clock
pushTurns(['F','F','U','L','~R','F','F','~L','R','U','F','F']);
}
return true;
}
return false;
}
function checkIfAllEdgeWrong(topEdges) {
if (topEdges.every((c) => c.pos.equals(c.defaultPos) == false)) {
pushTurns(['F','F','U','L','~R','F','F','~L','R','U','F','F']);
return true;
}
return false;
}
function alignWhiteCorners() {
let topCorners = getCubiesAtYDefault(-1).filter((qb) => abs(qb.defaultPos.x) + abs(qb.defaultPos.z) == 2);
if (topCorners.every((c) => c.pos.equals(c.defaultPos))) {
return true;
} else {
for (let a = 0; a < 2; a++) {
for (let i = 0; i < topCorners.length; i++) {
let qb = topCorners[i];
setView(qb);
switch(a) {
case 0:
if (checkTwoCorners(topCorners)) {
return false;
}
break;
case 1:
pushTurns(['U']);
return false;
}
}
}
}
}
function checkTwoCorners(topCorners) {
let backLeft = topCorners.find((qb) => qb.vPos.x == -1 && qb.vPos.z == -1);
let backRight = topCorners.find((qb) => qb.vPos.x == 1 && qb.vPos.z == -1);
let frontLeft = topCorners.find((qb) => qb.vPos.x == -1 && qb.vPos.z == 1);
if ((backLeft.vPos.equals(backLeft.vDefaultPos) && backRight.vPos.equals(backRight.vDefaultPos)) ||
(frontLeft.vPos.equals(frontLeft.vDefaultPos) && backRight.vPos.equals(backRight.vDefaultPos))) {
pushTurns(['~R','F','~R','B','B','R','~F','~R','B','B','R','R','~U']);
return true;
}
return false;
}
function makeTopWhite() {
let topCorners = getCubiesAtYDefault(-1).filter((qb) => abs(qb.defaultPos.x) + abs(qb.defaultPos.z) == 2);
if (topCorners.every((c) => c.pointer.equals(defaultRot))) {
return true;
} else {
for (let a = 0; a < 3; a++) {
for (let i = 0; i < topCorners.length; i++) {
let qb = topCorners[i];
setView(qb);
switch(a) {
case 0:
if (checkBlock(topCorners)) {
return false;
}
break;
case 1:
if (checkTopCorner(topCorners)) {
return false;
}
break;
case 2:
pushTurns(['R','U','~R','U','R','U','U','~R']);
return false;
}
}
}
}
}
function checkBlock(topCorners) {
if (topCorners.some((qb) => qb.vPos.x == -1 && qb.vPos.z == -1 && qb.vPointer.equals(vDefaultRot))) {
if (topCorners.some((qb) => qb.vPos.x == 1 && qb.vPos.z == -1 && qb.vPointer.equals(vDefaultRot))) {
pushTurns(['~R','~D','R','D','~R','~D','R','D','~U','~R','~D','R','D','~R','~D','R','D','~R','~D','R','D','~R','~D','R','D']);
return true;
}
}
return false;
}
function checkTopCorner(topCorners) {
if (topCorners.some((qb) => qb.vPos.x == -1 && qb.vPos.z == 1 && qb.vPointer.equals(vDefaultRot))) {
pushTurns(['R','U','~R','U','R','U','U','~R']);
return true;
}
return false;
}
function makeTopCross() {
let topCrossCubies = getCubiesAtYDefault(-1).filter((qb) => abs(qb.defaultPos.x + qb.defaultPos.z) == 1);
if (topCrossCubies.every((c) => c.pointer.equals(defaultRot))) {
return true;
} else {
for (let a = 0; a < 3; a++) {
for (let i = 0; i < topCrossCubies.length; i++) {
let qb = topCrossCubies[i];
setView(qb);
switch(a) {
case 0:
if (checkHorizontal(topCrossCubies)) {
return false;
}
break;
case 1:
if (checkLSign(topCrossCubies)) {
return false;
}
break;
case 2:
pushTurns(['F','U','R','~U','~R','~F', 'U']);
return false;
}
}
}
}
}
function checkLSign(topCrossCubies) {
// Check if L sign is made.
if (topCrossCubies.some((qb) => qb.vPos.x == 0 && qb.vPos.z == -1 && qb.vPointer.equals(vDefaultRot))) {
if (topCrossCubies.some((qb) => qb.vPos.x == -1 && qb.vPos.z == 0 && qb.vPointer.equals(vDefaultRot))) {
pushTurns(['F','U','R','~U','~R','~F']);
return true;
}
}
return false;
}
function checkHorizontal(topCrossCubies) {
// Check if horizontal line is made.
if (topCrossCubies.some((qb) => qb.vPos.x == 1 && qb.vPos.z == 0 && qb.vPointer.equals(vDefaultRot))) {
if (topCrossCubies.some((qb) => qb.vPos.x == -1 && qb.vPos.z == 0 && qb.vPointer.equals(vDefaultRot))) {
pushTurns(['F','U','R','~U','~R','~F']);
return true;
}
}
return false;
}
function makeSideEdges() {
let sideEdgeCubies = getCubiesAtYDefault(0).filter((qb) => abs(qb.defaultPos.x) + abs(qb.defaultPos.z) == 2);
for (let i = 0; i < sideEdgeCubies.length; i++) {
let qb = sideEdgeCubies[i];
if (qb.defaultPos.x != qb.pos.x || qb.defaultPos.y != qb.pos.y || qb.defaultPos.z != qb.pos.z || qb.pointer.equals(defaultRot) == false) {
setView(qb);
makeSideEdgesQb(qb);
return false;
}
if (i == sideEdgeCubies.length - 1) {
return true;
}
}
}
function makeSideEdgesQb(qb) {
// At top layer. Middle.
if (qb.vPos.y == -1) {
if (qb.vDefaultPos.x == 1 && qb.vDefaultPos.z == 1) { // If default location is at right-middle.
pushTurns(['U', 'R', '~U', '~R', '~U', '~F', 'U', 'F']);
} else if (qb.vDefaultPos.x == -1 && qb.vDefaultPos.z == 1) {
pushTurns(['U']);
} else if (qb.vDefaultPos.x == 1 && qb.vDefaultPos.z == -1) {
pushTurns(['~U']);
} else {
pushTurns(['~U', '~U']);
}
} else { // At middle layer. Right.
if (qb.vPos.x == qb.vDefaultPos.x && qb.vPos.z == qb.vDefaultPos.z) { // If default location is at middle.
// Conditions to place at perfect orientation.
if (qb.vPointer.equals(vDefaultRot) == false) { // If at place, but at wrong orientation.
pushTurns(['R', '~U', '~R', 'U', 'R', '~U', '~R', 'U', 'R', 'U', 'U', '~R', '~F', 'U', 'U', 'F']);
}
} else if (qb.vPos.x != qb.vDefaultPos.x && qb.vPos.z == qb.vDefaultPos.z) { // At other side
pushTurns(['U', 'R', '~U', '~R', '~U', '~F', 'U', 'F']); // Will put it on the top - middle.
} else { // At other side side OR diagonal side.
pushTurns(['U', 'R', '~U', '~R', '~U', '~F', 'U', 'F', '~U']); // Will put it on other middle.
}
}
}
function makeBottomCorner() {
let cornerCubies = getCubiesAtYDefault(1).filter((qb) => abs(qb.defaultPos.x) + abs(qb.defaultPos.z) == 2);
for (let i = 0; i < cornerCubies.length; i++) {
let qb = cornerCubies[i];
if (qb.defaultPos.x != qb.pos.x || qb.defaultPos.y != qb.pos.y || qb.defaultPos.z != qb.pos.z || qb.pointer.equals(defaultRot) == false) {
setView(qb);
makeBottomCornerQb(qb);
return false;
}
if (i == cornerCubies.length - 1) {
return true;
}
}
}
function makeBottomCornerQb(qb) {
// At top layer.
if (qb.vPos.y == -1) {
if (qb.vPos.x == qb.vDefaultPos.x && qb.vPos.z == qb.vDefaultPos.z) { // If default location is at bottom.
if (qb.vPointer.equals(vDefaultRot)) { // Conditions to place at perfect orientation.
pushTurns(['~U', '~F', 'U', 'F']);
} else {
pushTurns(['U', 'R', '~U', '~R']);
}
} else if (qb.vPos.x != qb.vDefaultPos.x && qb.vPos.z == qb.vDefaultPos.z) {
pushTurns(['U']);
} else {
pushTurns(['~U']);
}
} else { // At bottom layer.
if (qb.vPos.x == qb.vDefaultPos.x && qb.vPos.z == qb.vDefaultPos.z) { // If default location is at bottom.
// Conditions to place at perfect orientation.
if (qb.vPointer.equals(vDefaultRot) == false) { // If at place, but at wrong orientation.
pushTurns(['R', 'U', '~R', '~U', '~U', '~F', 'U', 'F']);
}
} else if (qb.vPos.x != qb.vDefaultPos.x && qb.vPos.z == qb.vDefaultPos.z) { // At other side
pushTurns(['R', 'U', '~R']); // Will put it on the top of other exact side.
} else { // At other side side OR diagonal side.
pushTurns(['~F', '~U', 'F']);
}
}
}
function makeBottomPlus() {
let edgeCubies = getCubiesAtYDefault(1).filter((qb) => abs(qb.defaultPos.x + qb.defaultPos.z) == 1);
for (let i = 0; i < edgeCubies.length; i++) {
let qb = edgeCubies[i];
if (qb.defaultPos.x != qb.pos.x || qb.defaultPos.y != qb.pos.y || qb.defaultPos.z != qb.pos.z || qb.pointer.equals(defaultRot) == false) {
setView(qb);
makeBottomPlusQb(qb);
return false;
}
if (i == edgeCubies.length - 1) {
return true;
}
}
}
function makeBottomPlusQb(qb) {
if (qb.vPos.y == 1) { // If at bottom layer on y-axis.
if (qb.vPos.x != qb.vDefaultPos.x || qb.vPos.y != qb.vDefaultPos.y || qb.vPos.z != qb.vDefaultPos.z) { // If not at correct position.
if (qb.vDefaultPos.x == -1) {
pushTurns(['F']);
} else if (qb.vDefaultPos.x == 1) {
pushTurns(['~F']);
} else {
pushTurns(['F', 'F']);
}
} else { // If at correct position.
if (qb.vPointer.equals(vDefaultRot) == false) { // If not in correct orientation.
pushTurns(['~F', 'R', 'U', '~R', 'F', 'F']);
}
}
} else if (qb.vPos.y == 0) { // If at middle layer on y-axis.
if (qb.vDefaultPos.x == 1) {
pushTurns(['~R']); // Transalate to place. //Maybe optimised more so that we dont need to flip color later.
} else if (qb.vDefaultPos.z == 1) {
pushTurns(['F']); // At place.
} else {
pushTurns(['~F', 'U', 'F']); // Bring it to top
}
} else if (qb.vPos.y == -1) { // If at top layer on y-axis.
if (qb.vPos.x == qb.vDefaultPos.x && qb.vPos.z == qb.vDefaultPos.z) { // If just on the top of its actual position.
pushTurns(['F', 'F']);
} else { // Not below actual position.
if (qb.vDefaultPos.x == -1) {
pushTurns(['U']);
} else if (qb.vDefaultPos.x == 1) {
pushTurns(['~U']);
} else {
pushTurns(['U', 'U']);
}
}
}
}
function getCubiesAtYDefault(yIndex) {
result = [];
for (let i = 0; i < cube.length; i++) {
if (cube[i].defaultPos.y == yIndex) {
result.push(cube[i]);
}
}
return result;
}