-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolvingEngine.py
More file actions
746 lines (666 loc) · 29.1 KB
/
SolvingEngine.py
File metadata and controls
746 lines (666 loc) · 29.1 KB
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
from RubiksCube import *
import random
# This file has the AI for solving the Rubik's Cube
# Helper functions, used for solving the cube
# Function that translates the instructions for solving cube
# in case the cube is rotated left, Used for reusing solve green
# white cross function for other sides as well (for red side)
def translateLeft(list):
if list == []:
return []
if list[0]=="F": return ["L"] + translateLeft(list[1:])
if list[0]=="F'": return ["L'"] + translateLeft(list[1:])
if list[0]=="R": return ["F"] + translateLeft(list[1:])
if list[0]=="R'": return ["F'"] + translateLeft(list[1:])
if list[0]=="L": return ["B"] + translateLeft(list[1:])
if list[0]=="L'": return ["B'"] + translateLeft(list[1:])
if list[0]=="B": return ["R"] + translateLeft(list[1:])
if list[0]=="B'": return ["R'"] + translateLeft(list[1:])
if list[0]=="D" or list[0]=="D'":
return [list[0]] + translateLeft(list[1:])
if list[0]=="U" or list[0]=="U'":
return[list[0]] + translateLeft(list[1:])
# Same as above, only if rotated right (orange side)
def translateRight(list):
if list == []:
return []
if list[0]=="F": return ["R"] + translateRight(list[1:])
if list[0]=="F'": return ["R'"] + translateRight(list[1:])
if list[0]=="R": return ["B"] + translateRight(list[1:])
if list[0]=="R'": return ["B'"] + translateRight(list[1:])
if list[0]=="L": return ["F"] + translateRight(list[1:])
if list[0]=="L'": return ["F'"] + translateRight(list[1:])
if list[0]=="B": return ["L"] + translateRight(list[1:])
if list[0]=="B'": return ["L'"] + translateRight(list[1:])
if list[0] == "D" or list[0] == "D'":
return [list[0]] + translateRight(list[1:])
if list[0] == "U" or list[0] == "U'":
return [list[0]] + translateRight(list[1:])
# Used if the cube is rotated 180 degrees, (blue side)
def translateBack(list):
if list == []:
return []
if list[0]=="F": return ["B"] + translateBack(list[1:])
if list[0]=="F'": return ["B'"] + translateBack(list[1:])
if list[0]=="R": return ["L"] + translateBack(list[1:])
if list[0]=="R'": return ["L'"] + translateBack(list[1:])
if list[0]=="L": return ["R"] + translateBack(list[1:])
if list[0]=="L'": return ["R'"] + translateBack(list[1:])
if list[0]=="B": return ["F"] + translateBack(list[1:])
if list[0]=="B'": return ["F'"] + translateBack(list[1:])
if list[0] == "D" or list[0] == "D'":
return [list[0]] + translateBack(list[1:])
if list[0] == "U" or list[0] == "U'":
return [list[0]] + translateBack(list[1:])
# This class solves the cross for any cube permutation
class SolveCross(RubikCube):
# Function that shortens the solution, As the AI isn't always smart
# it can include longer moves for doing the same task, this function
# cancels those moves and shortens the solution
def getSolution (self):
sol = self.solution[:]
self.solution = []
return sol
# Has the solution if the white green cubicle for cross
# is in the yellow side, gets the solution by extending solution list
# And then executes them to see the next course of action
def whiteY(self, wSide, ySide, frontSide, backSide, rightSide, leftSide, color):
sol = []
if wSide[1]=="w" and frontSide[7]==color:
sol.extend([])
elif ySide[7]=="w" and frontSide[1]==color:
sol.extend(["F", "F"])
elif ySide[7]==color and frontSide[1]=="w":
sol.extend(["U'", "R'", "F", "R"])
elif ySide[5]=="w" and rightSide[1]==color:
sol.extend(["U", "F", "F"])
elif ySide[5] == color and rightSide[1] == "w":
sol.extend(["R'", "F", "R"])
elif ySide[3]=="w" and leftSide[1]==color:
sol.extend(["U'", "F", "F"])
elif ySide[3]==color and leftSide[1]=="w":
sol.extend(["L", "F'", "L'"])
elif ySide[1]=="w" and backSide[1]==color:
sol.extend(["U", "U", "F", "F"])
elif ySide[1]==color and backSide[1]=="w":
sol.extend(["U", "R'", "F", "R"])
return sol
# Same as above, just for the white side
def whiteW(self, wSide,frontSide, backSide, rightSide, leftSide, color):
sol = []
if wSide[1]=="w" and frontSide[7]==color:
sol.extend([])
elif wSide[3]=="w" and leftSide[7]==color:
sol.extend(["L", "L", "U'", "F", "F"])
elif wSide[5]=="w" and rightSide[7]==color:
sol.extend(["R", "R", "U", "F", "F"])
elif wSide[7]=="w" and backSide[7]==color:
sol.extend(["B", "B", "U", "U", "F", "F"])
elif wSide[1]==color and frontSide[7]=="w":
sol.extend(["F'", "D", "R'", "D'"])
elif wSide[3]==color and leftSide[7]=="w":
sol.extend(["L'", "F'", "L"])
elif wSide[5]==color and rightSide[7]=="w":
sol.extend(["R", "F"])
elif wSide[7]==color and backSide[7]=="w":
sol.extend(["B","R'","U", "R", "F", "F"])
return sol
# Same as above, but for rest of sides
def white(self, wSide,frontSide, backSide, rightSide, leftSide, color):
sol = []
if wSide[1] == "w" and frontSide[7] == color:
sol.extend([])
elif frontSide[5]==color and rightSide[3]=="w":
sol.extend(["F"])
elif frontSide[3]==color and leftSide[5] =="w":
sol.extend(["F'"])
elif backSide[3]=="w" and rightSide[5]==color:
sol.extend(["D", "R", "D'"])
elif backSide[5]=="w" and leftSide[3]==color:
sol.extend(["D'", "L'", "D"])
elif frontSide[5]=="w" and rightSide[3]==color:
sol.extend(["D", "R'", "D'"])
elif frontSide[3]=="w" and leftSide[5]==color:
sol.extend(["D'", "L", "D"])
elif backSide[5]==color and leftSide[3]=="w":
sol.extend(["D", "D", "B", "D", "D"])
elif backSide[3]==color and rightSide[5]=="w":
sol.extend(["D", "D", "B'", "D", "D"])
return sol
#Uses the above function to put green cubicle in correct place
#The function was written originally for green so requires minimal changes
def greenCubicle(self):
w, y, g, b = self.wSide , self.ySide , self.gSide, self.bSide
r, o = self.rSide, self.oSide
x = self.whiteY(w, y, g, b, o, r, "g")
y = self.whiteW(w, g, b, o, r, "g")
z = self.white(w, g, b, o, r, "g")
for i in [x,y,z]:
if i != []:
self.solution.extend(i)
self.move(i)
# Uses the solve cross functions, only difference is, the functions
# were written assuming green as the front-side, makes use of that
# by "rotating" the cube and making red the front side
# That way, the logic for the green side still holds
def redCubicle(self):
w, y, g, b = self.wSide[:], self.ySide[:], self.gSide, self.bSide
r, o = self.rSide, self.oSide
# Rotates the white and yellow side as rotating the cube
# mixes up the indices for those lists otherwise
self.rotate(w)
self.rotatePrime(y)
# Gives red side as the front side for solving
x = self.whiteY(w, y, r, o, g, b, "r" )
y = self.whiteW(w, r, o, g, b, "r" )
z = self.white(w, r, o, g, b, "r" )
# After checking in all positions, executes the one
# which holds the solution for current position
for i in [x,y,z]:
if i != []:
# Translates the moves for a left rotated cube
# into what it would look like with the green side as front
translation = translateLeft(i)
self.solution.extend(translation)
self.move(translation)
# Same logic as red side, only applied for orange side
def orangeCubicle(self):
w, y, g, b = self.wSide[:], self.ySide[:], self.gSide, self.bSide
r, o = self.rSide, self.oSide
self.rotatePrime(w)
self.rotate(y)
x = self.whiteY(w, y, o, r, b, g, "o")
y = self.whiteW(w, o, r, b, g, "o")
z = self.white(w, o, r, b, g, "o")
for i in [x,y,z]:
if i != []:
translation = translateRight(i)
self.solution.extend(translation)
self.move(translation)
# Same logic as red side, only applied for blue side
def blueCubicle(self):
w, y, g, b = self.wSide[:], self.ySide[:], self.gSide, self.bSide
r, o = self.rSide, self.oSide
self.rotate(w); self.rotate(w)
self.rotate(y); self.rotate(y)
x = self.whiteY(w, y, b, g, r, o, "b")
y = self.whiteW(w, b, g, r, o, "b")
z = self.white(w, b, g, r, o, "b")
for i in [x,y,z]:
if i != []:
translation = translateBack(i)
self.solution.extend(translation)
self.move(translation)
def solveAllSides(self):
self.greenCubicle()
self.redCubicle()
self.orangeCubicle()
self.blueCubicle()
def getSides(self):
return (self.wSide, self.ySide, self.gSide, self.bSide, self.oSide, self.rSide)
# z = cross.getSides()
# Solves first two layers of the cube after having the cross done
# whiteSide, yellowSide, frontSide, backSide, rightSide, leftSide
class SolveFirstTwoLayers(RubikCube):
def getSolution (self):
sol = self.solution[:]
self.solution = []
return sol
def easyCases(self, w, y, f, b, r, l):
# Saves the colors for the front side and the right side
# Are the colors we are looking for.
fC, rC = f[4], r[4]
if f[2]==fC and r[0]=="w" and y[8]==rC and y[1]==fC and b[1]==rC:
return ["R", "U", "R'"]
elif f[2]=="w" and r[0]==rC and y[8]==fC and y[3]==rC and l[1]==fC:
return ["F'", "U'", "F"]
elif f[2]==fC and r[0]=="w" and y[8]==rC and y[7]==rC and f[1]==fC:
return ["U'", "F'", "U", "F"]
elif f[2]=="w" and r[0]==rC and y[8]==fC and y[5]==fC and r[1]==rC:
return ["U", "R", "U'", "R'"]
elif f[1]==fC and f[8]==fC and r[6]==rC and w[2]=="w" and y[7]==rC:
return ["U", "R", "U'", "R'", "U'", "F'", "U", "F"]
elif r[1]==rC and f[8]==fC and r[6]==rC and w[2]=="w" and y[5]==fC:
return ["U'", "F'", "U", "F", "U", "R", "U'", "R'"]
elif f[1]==fC and y[7]==rC and f[8]==rC and r[6]=="w" and w[2]==fC:
return ["F'", "U", "F", "U'", "F'", "U", "F"]
elif r[1]==rC and y[5]==fC and f[8]==rC and r[6]=="w" and w[2]==fC:
return ["R", "U", "R'", "U'", "R", "U", "R'"]
def casesPartTwo(self, w, y, f, b, r, l):
fC, rC = f[4], r[4]
if f[8]=="w" and r[6]==fC and w[2]==rC and y[5]==fC and r[1]==rC:
return ["R", "U'", "R'", "U", "R", "U'", "R'"]
elif f[1]==fC and y[7]==rC and f[8]=="w" and r[6]==fC and w[2]==rC:
return ["F'", "U'", "F", "U", "F'", "U'", "F"]
elif f[5]==fC and r[3]==rC and y[8]=="w" and f[2]==rC and r[0]==fC:
return ["R", "U", "R'", "U'", "R", "U", "R'", "U'", "R", "U", "R'"]
elif f[5]==rC and r[3]==fC and f[2]==rC and r[0]==fC and y[8]=="w":
return ["R", "U'", "R'", "U", "F'", "U", "F"]
elif f[5]==fC and r[3]==rC and f[2]==fC and y[8]==rC and r[0]=="w":
return ["U", "F'", "U", "F", "U", "F'", "U", "U", "F"]
elif f[5]==rC and r[3]==fC and f[2]==fC and r[0]=="w" and y[8]==rC:
return ["U", "F'", "U'", "F", "U'", "R", "U", "R'"]
elif f[5]==fC and r[3]==rC and f[2]=="w" and r[0]==rC and y[8]==fC:
return ["U'", "R", "U'", "R'", "U'", "R", "U", "U", "R'"]
elif f[5]==rC and r[3]==fC and f[2]=="w" and y[8]==fC and r[0]==rC:
return ["U'", "R", "U", "R'", "U", "F'", "U'", "F"]
elif y[5]==rC and r[1]==fC and f[2]==fC and y[8]==rC and r[0]=="w":
return ["R", "U'", "R'", "U", "U", "F'", "U'", "F"]
def casesPartThree (self, w, y, f, b, r, l):
fC, rC = f[4], r[4]
if f[1]==rC and y[7]==fC and f[2]=="w" and r[0]==rC and y[8]==fC:
return ["F'","U", "F", "U'", "U'", "R", "U", "R'"]
elif y[1]==rC and b[1]==fC and f[2]==fC and y[8]==rC and r[0]=="w":
return ["U", "F'", "U", "U", "F", "U", "F'", "U", "U", "F"]
elif f[2]=="w" and r[0]==rC and y[8]==fC and y[3]==fC and l[1]==rC:
return ["U'", "R", "U", "U", "R'", "U'", "R", "U", "U", "R'"]
elif y[3]==rC and l[1]==fC and f[2]==fC and y[8]==rC and r[0]=="w":
return ["U", "F'", "U'", "F", "U", "F'", "U", "U", "F"]
elif y[1]==fC and b[1]==rC and f[2]=="w" and r[0]==rC and y[8]==fC:
return ["U'", "R", "U", "R'", "U'", "R", "U", "U", "R'"]
elif r[1]==rC and y[5]==fC and f[2]==fC and r[0]=="w" and y[8]==rC:
return ["U'", "R", "U'", "R'", "U", "R", "U", "R'"]
elif f[1]==fC and y[7]==rC and r[0]==rC and y[8]==fC and f[2]=="w":
return ["U", "F'", "U", "F", "U'", "F'", "U'", "F"]
elif y[3]==fC and l[1]==rC and f[2]==fC and y[8]==rC and r[0]=="w":
return ["U'", "R", "U", "R'", "U", "R", "U", "R'"]
elif y[1]==rC and b[1]==fC and f[2]=="w" and y[8]==fC and r[0]==rC:
return ["U", "F'", "U'", "F", "U'", "F'", "U'", "F"]
def casesPartFour (self, w, y, f, b, r, l):
fC, rC = f[4], r[4]
if f[1]==rC and y[7]==fC and f[2]==fC and y[8]==rC and r[0]=="w":
return ["U", "F'", "U", "U", "F", "U'", "R", "U", "R'"]
elif r[1]==fC and y[5]==rC and f[2]=="w" and y[8]==fC and r[0]==rC:
return ["U'", "R", "U", "U", "R'", "U", "F'", "U'", "F"]
elif f[1]==rC and y[7]==fC and f[2]==rC and r[0]==fC and y[8]=="w":
return ["R", "U", "R'", "U'", "U'", "R", "U", "R'", "U'", "R", "U", "R'"]
elif r[1]==fC and y[5]==rC and f[2]==rC and r[0]==fC and y[8]=="w":
return ["F'", "U'", 'F', 'U', 'U', "F'", "U'", 'F', 'U', "F'", "U'", 'F']
elif y[3]==fC and l[1]==rC and f[2]==rC and r[0]==fC and y[8]=="w":
return ["U", "U", "R", "U", "R'", "U", "R", "U'", "R'"]
elif y[1]==rC and b[1]==fC and f[2]==rC and r[0]==fC and y[8]=="w":
return ["U", "U", "F'", "U'", "F", "U'", "F'", "U", "F"]
elif y[1]==fC and b[1]==rC and f[2]==rC and r[0]==fC and y[8]=="w":
return ["U", "R", "U", "U", "R'", "U", "R", "U'", "R'"]
elif y[3]==rC and l[1]==fC and f[2]==rC and r[0]==fC and y[8]=="w":
return ["U'", "F'", "U", "U", "F", "U'", "F'", "U", "F"]
elif r[1]==rC and y[5]==fC and f[2]==rC and r[0]==fC and y[8]=="w":
return ["R", "U", "U", "R'", "U'", "R", "U", "R'"]
def casesPartFive (self, w, y, f, b, r, l):
fC, rC = f[4], r[4]
if f[1]==fC and y[7]==rC and f[2]==rC and r[0]==fC and y[8]=="w":
return ["F'", "U", "U", "F", "U", "F'", "U'", "F"]
elif f[5]==rC and r[3]==fC and f[8]==fC and r[6]==rC and w[2]=="w":
return ["R", "U'", "R'", "U", "F'", 'U', 'U', 'F', 'U', "F'", 'U', 'U','F']
elif f[5]==fC and r[3]==rC and f[8]==rC and r[6]=="w" and w[2]==fC:
return ["R", "U'", "R'", "U", "R", "U", "U", "R'", "U", "R", "U'", "R'"]
elif f[5]==fC and r[3]==rC and f[8]=="w" and r[6]==fC and w[2]==rC:
return ["R", "U'", "R'", "U'", "R", "U", "R'", "U'", "R", "U", "U", "R'"]
elif f[5]==rC and r[3]==fC and f[8]==rC and r[6]=="w" and w[2]==fC :
return ["R", "U", "R'", "U'", "R", "U'", "R'", "U", "U", "F'", "U'", "F"]
elif f[5]==rC and r[3]==fC and f[8]=="w" and r[6]==fC and w[2]==rC:
return ["R", "U'", "R'", "U", "F'", "U'", "F", "U'", "F'", "U'", "F"]
def greenFirstTwoLayers(self):
w, y, g, b = self.wSide, self.ySide, self.gSide, self.bSide
r, o = self.rSide, self.oSide
final1 = self.easyCases(w, y, g, b, o, r)
final2 = self.casesPartTwo(w, y, g, b, o, r)
final3 = self.casesPartThree(w, y, g, b, o, r)
final4 = self.casesPartFour(w, y, g, b, o, r)
final5 = self.casesPartFive(w, y, g, b, o, r)
for i in [final1, final2, final3, final4, final5]:
if i!=None and i!=[]:
return i
def orangeFirstTwoLayers(self):
w, y, g, b = self.wSide[:], self.ySide[:], self.gSide, self.bSide
r, o = self.rSide, self.oSide
self.rotatePrime(w)
self.rotate(y)
final1 = self.easyCases(w, y, o, r, b, g)
final2 = self.casesPartTwo(w, y, o, r, b, g)
final3 = self.casesPartThree(w, y, o, r, b, g)
final4 = self.casesPartFour(w, y, o, r, b, g)
final5 = self.casesPartFive(w, y, o, r, b, g)
for i in [final1, final2, final3, final4, final5]:
if i != None and i != []:
translation = translateRight(i)
return translation
def redFirstTwoLayers(self):
w, y, g, b = self.wSide[:], self.ySide[:], self.gSide, self.bSide
r, o = self.rSide, self.oSide
self.rotate(w)
self.rotatePrime(y)
final1 = self.easyCases(w, y, r, o, g, b)
final2 = self.casesPartTwo(w, y, r, o, g, b)
final3 = self.casesPartThree(w, y, r, o, g, b)
final4 = self.casesPartFour(w, y, r, o, g, b)
final5 = self.casesPartFive(w, y, r, o, g, b)
for i in [final1, final2, final3, final4, final5]:
if i != None and i != []:
translation = translateLeft(i)
return translation
def blueFirstTwoLayers(self):
w, y, g, b = self.wSide[:], self.ySide[:], self.gSide, self.bSide
r, o = self.rSide, self.oSide
self.rotate(w);self.rotate(w)
self.rotate(y);self.rotate(y)
final1 = self.easyCases(w, y, b, g , r, o)
final2 = self.casesPartTwo(w, y, b, g, r, o)
final3 = self.casesPartThree(w, y, b, g, r, o)
final4 = self.casesPartFour(w, y, b, g, r, o)
final5 = self.casesPartFive(w, y, b, g, r, o)
for i in [final1, final2, final3, final4, final5]:
if i != None and i != []:
translation = translateBack(i)
return translation
# Function used for trying out different orders of solving
def tryRotate(self, func):
x = func()
if x==None:
self.move(["U"]);self.solution.extend(["U"])
x = func()
if x==None:
self.move(["U"]);self.solution.extend(["U"])
x = func()
if x==None:
self.move(["U", "U"]); self.solution = self.solution[:-2]
self.move(["U'"]); self.solution.extend(["U'"])
x = func()
if x!=None:
self.move(x);self.solution.extend(x)
return False
else:
self.move(["U"]);self.solution = self.solution[:-1]
return True
def greenContinued(self):
condition = self.tryRotate(self.greenFirstTwoLayers)
if condition :
move = ["F", "U", "F'"]
self.move(move);self.solution.extend(move)
condition = self.tryRotate(self.greenFirstTwoLayers)
if condition :
self.move(move);self.solution.extend(move)
condition = self.tryRotate(self.greenFirstTwoLayers)
if condition :
self.move(["F", "U'", "F'"])
self.solution = self.solution[:-3]
self.move(["R'", "U'", "R"]);self.solution.extend(["R'", "U'", "R"])
condition = self.tryRotate(self.greenFirstTwoLayers)
if condition :
self.move(["R'", "U", "R",])####Switched the r prime and r
self.solution = self.solution[:-3]
self.move(["L","U'","L'"]);self.solution.extend(["L","U'","L'"])
condition = self.tryRotate(self.greenFirstTwoLayers)
if condition :
self.move(["L", "U", "L'", "F", "U'", "F'"])
self.solution = self.solution[:-6]
move = ["R'", "U", "R"]
self.move(move); self.solution.extend(move)
condition = self.tryRotate(self.greenFirstTwoLayers)
if condition :
self.move(move); self.solution.extend(move)
condition = self.tryRotate(self.greenFirstTwoLayers)
if condition :
self.move(["R'", "U'", "U'", "R"]);self.solution = self.solution[:-6]
move = ["L", "U'", "L'"]
self.move(move);self.solution.extend(move)
condition = self.tryRotate(self.greenFirstTwoLayers)
if condition :
self.move(move); self.solution.extend(move)
condition = self.tryRotate(self.greenFirstTwoLayers)
if condition:
# Cancels the changes made from previous attempt
# And tries out a combination of left and right shifting
self.move(["L", "U", "U", "L'"])
self.solution = self.solution[:-6]
self.move(["R'", "U", "R", "L", "U", "U", "L'"])
self.solution.extend(["R'", "U", "R", "L", "U", "U", "L'"])
condition = self.tryRotate(self.greenFirstTwoLayers)
def orangeContinued (self):
condition = self.tryRotate(self.orangeFirstTwoLayers)
if condition :
move = ["B'", 'U', 'B']
self.move(move); self.solution.extend(move)
condition = self.tryRotate(self.orangeFirstTwoLayers)
if condition :
self.move(move); self.solution.extend(move)
condition = self.tryRotate(self.orangeFirstTwoLayers)
if condition :
self.move(["B'", "U'", "U'", 'B'])
self.solution = self.solution[:-6]
move = ['F', "U'", "F'"]
self.move(move);self.solution.extend(move)
condition = self.tryRotate(self.orangeFirstTwoLayers)
if condition :
self.move(move); self.solution.extend(move)
condition = self.tryRotate(self.orangeFirstTwoLayers)
if condition:
# Cancels the changes made from previous attempt
self.move(['F', "U'", "U'", "F'"])
self.solution = self.solution[:-6]
self.move(["L", "U", "L'", "F", "U", "F'"])
self.solution.extend(["L", "U", "L'", "F", "U", "F'"])
condition = self.tryRotate(self.orangeFirstTwoLayers)
def blueContinued (self):
condition = self.tryRotate(self.blueFirstTwoLayers)
if condition:
move = ["L'", 'U', 'L']
self.move(move)
self.solution.extend(move)
condition = self.tryRotate(self.blueFirstTwoLayers)
if condition:
self.move(move)
self.solution.extend(move)
condition = self.tryRotate(self.blueFirstTwoLayers)
def redContinued(self):
condition = self.tryRotate(self.redFirstTwoLayers)
def solveTwoLayers(self):
self.greenContinued()
self.orangeContinued()
self.blueContinued()
self.redContinued()
def getSides(self):
return (self.wSide, self.ySide, self.gSide, self.bSide, self.oSide, self.rSide)
# layer.printCube()
# Solves the final part of the cube
class LastLayer(RubikCube):
def getSolution (self):
sol = self.solution[:]
self.solution = []
# Is used if the same move appears three consecutive time
# Turns them into one move with opposite direction
for i in range(2, len(sol)):
if sol[i]==sol[i-1] and sol[i-1]==sol[i-2]:
sol[i] = "X"
sol[i-1] = "X"
if "'" in sol[i]:
sol[i-2] = sol[i-2][:1]
else:
sol[i-2] = sol[i-2] + "'"
# Is used when the same move appears four times, deletes them
for i in range (3, len(sol)):
if sol[i]==sol[i-1] and sol[i-1]==sol[i-2] and sol[i-2]==sol[i-3]:
sol[i], sol[i-1], sol[i-2], sol[i-3] = "X", "X", "X", "X"
# Is used when a move and their opposite direction move appear
# consecutively, deletes them
for i in range(1, len(sol)):
if sol[i]==sol[i-1]+"'" or sol[i-1]==sol[i]+"'":
sol[i], sol[i-1] = "X", "X"
# "Deleted" moves were converted into X's before, now will be deleted
while "X" in sol:
sol.remove("X")
return sol
def yellowCross(self):
y = self.ySide
if y[1]!="y" and y[3]!="y" and y[5]!="y" and y[7]!="y":
self.solution.extend(["F", "R", "U", "R'", "U'", "F'"])
self.move(["F", "R", "U", "R'", "U'", "F'"])
if y[5]=="y" and y[7]=="y" and y[1]!="y" and y[3]!="y":
self.solution.extend(["B", "U", "L", "U'", "L'", "B'"])
self.move(["B", "U", "L", "U'", "L'", "B'"])
elif y[1]=="y" and y[3]=="y" and y[5]!="y" and y[7]!="y":
self.solution.extend(["U", "U", "B", "U", "L", "U'", "L'", "B'"])
self.move(["U", "U", "B", "U", "L", "U'", "L'", "B'"])
elif y[1]=="y" and y[5]=="y" and y[3]!="y" and y[7]!="y":
self.solution.extend(["U", "B", "U", "L", "U'", "L'", "B'"])
self.move(["U", "B", "U", "L", "U'", "L'", "B'"])
elif y[3]=="y" and y[7]=="y" and y[1]!="y" and y[5]!="y":
self.solution.extend(["U'", "B", "U", "L", "U'", "L'", "B'"])
self.move(["U'", "B", "U", "L", "U'", "L'", "B'"])
elif y[1]=="y" and y[7]=="y" and y[5]!="y" and y[3]!="y":
self.solution.extend(["U", "F", "R", "U", "R'", "U'", "F'" ])
self.move(["U", "F", "R", "U", "R'", "U'", "F'" ])
elif y[3]=="y" and y[5]=="y" and y[1]!="y" and y[7]!="y":
self.solution.extend(["F", "R", "U", "R'", "U'", "F'"])
self.move(["F", "R", "U", "R'", "U'", "F'"])
def allYellow(self):
y,f,r,l = self.ySide, self.gSide, self.oSide, self.rSide
if y[2]=="y" and y[0]!="y" and y[6]!="y" and y[8]!="y" and r[0]=="y":
return ["R", "U", "U", "R'", "U'", "R", "U'", "R'"]
elif y[0]!="y" and y[2]!="y" and y[6]=="y" and y[8]!="y" and f[2]=="y":
return ["R", "U", "R'", "U", "R", "U", "U", "R'"]
elif y[0]!="y" and y[2]=="y" and y[6]!="y" and y[8]=="y" and f[0]=="y":
return ["L", "F", "R'", "F'", "L'", "F", "R", "F'"]
elif y[0]!="y" and y[2]=="y" and y[6]=="y" and y[8]!="y" and f[2]=="y":
return ["F'", "L", "F", "R'", "F'", "L'", "F", "R"]
elif y[0]=="y" and y[2]=="y" and y[6]!="y" and y[8]!="y" and f[0]=="y":
return ["R", "R", "D", "R'", "U", "U", "R", "D'", "R'", "U", "U", "R'"]
elif y[0]!="y" and y[2]!="y" and y[6]!="y" and y[8]!="y" and f[2]=="y" and l[2]=="y":
return ["R","U","U","R","R","U'","R","R","U'","R","R","U","U", "R"]
elif y[0]!="y" and y[2]!="y" and y[6]!="y" and y[8]!="y" and r[2]=="y" and l[2]=="y":
return ["R", "U", "R'", "U", "R", "U'", "R'", "U", "R", "U", "U" , "R'"]
def tryRotate(self, func):
x = func()
if x==None:
self.move(["U"]);self.solution.extend(["U"])
x = func()
if x==None:
self.move(["U"]);self.solution.extend(["U"])
x = func()
if x==None:
self.move(["U", "U"]); self.solution = self.solution[:-2]
self.move(["U'"]); self.solution.extend(["U'"])
x = func()
if x!=None:
self.move(x);self.solution.extend(x)
return False
else:
self.move(["U"]);self.solution = self.solution[:-1]
return True
# Has the cases with a probability of 1/18
def pLLPartOne(self):
y, f, r, l = self.ySide, self.gSide, self.oSide, self.rSide
b = self.bSide
if b[0]==b[1] and b[1]==b[2] and f[1]==r[0] and r[0]==r[2]:
return ["R","U'", "R", "U", "R", "U", "R", "U'", "R'", "U'", "R", "R"]
elif b[0]==b[1] and b[1]==b[2] and f[1]==l[0] and l[0]==l[2]:
return ["R", "R", "U", "R", "U", "R'","U'", "R'", "U'", "R'", "U", "R'"]
elif f[0]==f[1] and l[1]==l[2] and b[0]==b[2] and b[1]==l[0]:
return ["R'", "F", "R'", "B", "B", "R", "F'", "R'", "B", "B", "R", "R"]
elif f[0]==f[1] and l[1]==l[2] and r[0]==r[2] and r[1]==l[0]:
return ["R", "R", "B", "B", "R", "F", "R'", "B", "B", "R", "F'", "R"]
elif l[0]==l[2] and f[0]==f[1] and l[1]==b[2] and b[0]==f[2]:
return ['B','U','U',"B'",'U','U','B',"L'","B'","U'",'B','U','B','L','B','B','U']
elif l[1]==l[2] and f[0]==f[2] and l[0]==r[2] and r[1]==f[0]:
return ["R'","U","U","R'","D'","R","U'","R'","D","R","U","R","U'","R'","U'","R","U'"]
elif f[0]==f[1] and f[1]==f[2] and r[0]==r[1] and l[2]==b[1]:
return ["R'","U","L'","U","U","R","U'","R'","U","U","R","L","U'"]
elif l[0]==l[1] and l[1]==l[2] and b[1]==b[2] and f[0]==r[1]:
return ["R","U","R'","F'","R","U","R'","U'","R'","F","R","R","U'","R'","U'"]
elif f[0]==f[1] and b[1]==b[2] and l[0]==l[2] and r[1]==l[0]:
return ["R","U","R'","U'","R'","F","R","R","U'","R'","U'","R","U","R'","F'"]
elif l[0] == l[1] and l[1] == l[2] and f[0] == b[1] and f[1] == b[2] and f[2] == r[1] and b[0] == r[1]:
return ["R'","U'","F'","R","U","R'","U'","R'","F","R","R","U'","R'","U'","R","U","R'","U","R"]
def pLLPartTwo(self):
y, f, r, l = self.ySide, self.gSide, self.oSide, self.rSide
b = self.bSide
if l[1]==l[2] and f[0]==f[1] and r[0]==l[1] and r[1]==b[0]:
return ["R'","U","R'","U'","B'","R'",'B','B',"U'","B'",'U',"B'",'R','B','R']
elif f[0]==f[1] and r[1]==r[2] and f[2]==l[1] and r[0]==l[2]:
return ["F","R","U'","R'","U'","R","U","R'","F'","R","U","R'","U'","R'","F","R","F'"]
elif l[0]==l[2] and f[1]==f[2] and f[0]==b[1] and l[2]==r[1]:
return ["R","R","U","R'","U","R'","U'","R","U'","R","R","D","U'","R'","U","R","D'","U"]
elif f[1]==f[2] and b[0]==b[2] and r[1]==b[0] and f[0]==l[1]:
return ["F'","U'","F","R","R","D","B'",'U','B',"U'",'B',"D'","R","R"]
elif l[0]==l[2] and b[0]==b[1] and f[2]==b[1] and l[1]==r[2]:
return ["R", "R", "U'", "R","U'","R","U","R'","U","R","R","D'","U","R","U'","R'","D","U'"]
elif l[0]==l[2] and r[0]==r[1] and f[1]==l[0] and f[0]==b[1]:
return ["R", "U","R'","F","F","D'",'L', "U'", "L'", 'U', "L'", "D", "F","F"]
def pLLPartThree(self):
y, f, r, l = self.ySide, self.gSide, self.oSide, self.rSide
b = self.bSide
if f[1]==b[0] and b[0]==b[2] and r[1]==l[0] and l[0]==l[2]:
return ["L'", "R'", "U","U","L","R","F","B","U","U","F'","B'"]
elif f[1]==r[0] and r[0]==r[2] and l[1]==b[0] and b[0]==b[2]:
return ["R","B'","R'","B","F","R'","F","B'","R'","B","R","F","F"]
elif r[0]==l[2] and r[2]==l[0] and f[0]==b[2] and f[2]==b[0] and l[2] ==b[1]:
return ["F","R","B","R'","F'","R","L","F","L'","B'","L","F'","R'","L'"]
elif f[0] == f[1] and l[0] == l[1] and f[2] == b[1] and l[2] == r[1]:
return ["R","U'","R","R","F","F","U'","R","F","F","R'","U","F","F","R","R","U","R'"]
elif f[1] == f[2] and l[2] == l[1] and f[0] == b[1] and r[0] == l[1]:
return ["R'", "U", "R","R","B","B","U","R'","B","B","R","U'","B","B","R","R","U'","R"]
def finishLastLayer(self):
self.yellowCross()
self.tryRotate(self.allYellow)
self.tryRotate(self.pLLPartOne)
self.tryRotate(self.pLLPartTwo)
self.tryRotate(self.pLLPartThree)
if self.gSide[1]=="r":
self.solution.extend(["U"])
self.move(["U"])
elif self.gSide[1]=="b":
self.solution.extend(["U","U"])
self.move(["U", "U"])
elif self.gSide[1]=="o":
self.solution.extend(["U'"])
self.move(["U'"])
### Used for testing:
# g="g";b="b";r="r";o="o";w="w";y="y"
# choices = ["R", "U", "B", "F", "L" ]
# wSide = [w, w, w,
# w, w, w,
# w, w, w]
# ySide = [y, y, y,
# y, y, y,
# y, y, y]
# bSide = [b, b, b,
# b, b, b,
# b, b, b]
# gSide = [g, g, g,
# g, g, g,
# g, g, g]
# rSide = [r, r, r,
# r, r, r,
# r, r, r]
# oSide = [o, o, o,
# o, o, o,
# o, o, o]
# randomScramble = []
# averageSolution = 0
# count = 0
# list = [[g,g,g,g,g,g,g,g,g], [b,b,b,b,b,b,b,b,b],[w,w,w,w,w,w,w,w,w],[y,y,y,y,y,y,y,y,y],[r,r,r,r,r,r,r,r,r],[o,o,o,o,o,o,o,o,o]]
# answer = [[g,g,g,g,g,g,g,g,g], [b,b,b,b,b,b,b,b,b],[w,w,w,w,w,w,w,w,w],[y,y,y,y,y,y,y,y,y],[r,r,r,r,r,r,r,r,r],[o,o,o,o,o,o,o,o,o]]
# while list==answer and count<10000:
# randomScramble = []
# cross, layer, yellow = 0,0,0
# for i in range(40):
# randomScramble.append(random.choice(choices))
# cross = SolveCross(gSide, bSide, wSide, ySide, rSide, oSide,[])
# cross.move(randomScramble)
# cross.solveAllSides()
# w, y, g, b, o, r = cross.getSides()
# crossSolution = cross.getSolution()
# layer = SolveFirstTwoLayers(g, b, w, y, r, o, [])
# layer.solveTwoLayers()
# layerSolution = layer.getSolution()
# w, y, g, b, o, r = layer.getSides()
# yellow = LastLayer(g, b, w, y, r, o, [])
# yellow.finishLastLayer()
# answer[0], answer[1], answer[2]=yellow.gSide, yellow.bSide, yellow.wSide
# answer[3], answer[4], answer[5] = yellow.ySide, yellow.rSide, yellow.oSide
# averageSolution+=len(yellow.getSolution()+layerSolution+crossSolution)
# count+=1
# if count%100==0:
# print (count)
# print ("Average solution length= "+str(averageSolution//10000))
# print (count)