-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathrubiks-cube-solver.py
1771 lines (1651 loc) · 64.8 KB
/
rubiks-cube-solver.py
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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from random import randint
import tkinter as tk
import copy, webbrowser, os
from tkinter import *
#This is the Cube Solver
#This version contains a GUI
#Last Edited on: 12/5/2014
#Written by: Lucas Liberacki & Tom Brannan
#globals
moves_list = []
last_scramble = []
f2l_list = []
step_moves_list = []
solution_length = 0
#creates a 3d list representing a solved cube
def make_cube():
global step_moves_list, f2l_list, moves_list
step_moves_list = [0,0,0,0]
f2l_list = []
moves_list = []
return [ [['W', 'W', 'W'],
['W', 'W', 'W'],
['W', 'W', 'W']], #Up/white
[['G', 'G', 'G'],
['G', 'G', 'G'],
['G', 'G', 'G']], #front/green
[['R', 'R', 'R'],
['R', 'R', 'R'],
['R', 'R', 'R']], #right/red
[['O', 'O', 'O'],
['O', 'O', 'O'],
['O', 'O', 'O']], #left/orange
[['Y', 'Y', 'Y'],
['Y', 'Y', 'Y'],
['Y', 'Y', 'Y']], #down/yellow
[['B', 'B', 'B'],
['B', 'B', 'B'],
['B', 'B', 'B']]] #back/blue
a = make_cube()
#prints a string representation of the cube to the interpreter
def print_cube():
print('\t\t'+str(a[5][0])+'\n\t\t'+str(a[5][1])+'\n\t\t'+str(a[5][2]))
print(str(a[3][0])+' '+str(a[0][0])+' '+str(a[2][0]))
print(str(a[3][1])+' '+str(a[0][1])+' '+str(a[2][1]))
print(str(a[3][2])+' '+str(a[0][2])+' '+str(a[2][2]))
print('\t\t'+str(a[1][0])+'\n\t\t'+str(a[1][1])+'\n\t\t'+str(a[1][2]))
print('\t\t'+str(a[4][0])+'\n\t\t'+str(a[4][1])+'\n\t\t'+str(a[4][2]))
#simplifies the list of moves and returns a string representation of the moves
def get_moves():
simplify_moves()
s = ""
for i in moves_list:
s += str(i) + " "
s = str.replace(s, "i", "'")[:-1]
return s
#returns a string representation of the last scramble
def get_scramble():
s = ""
for i in last_scramble:
s += str(i) + " "
s = str.replace(s, "i", "'")[:-1]
return s
#helper function: returns True if all elements in a set are equal
def all_same(items):
return all(x == items[0] for x in items)
#Transforms a given move into the corresponding move after a Y-rotation
def yTransform(move):
if move[0] in ["U", "D"]:
return move
if move[0] == "F":
return "R" + move[1:]
if move[0] == "R":
return "B" + move[1:]
if move[0] == "B":
return "L" + move[1:]
if move[0] == "L":
return "F" + move[1:]
raise Exception("Invalid move to yTransform: " + move)
#modifies the global moves list by removing redundancies
def simplify_moves():
global moves_list, solution_length
new_list = []
prev_move = ""
yCount = 0
for move in moves_list:
if move == "Y":
yCount += 1
yCount %= 4
continue
if move == "Yi":
yCount += 3
yCount %= 4
continue
if move == "Y2":
yCount += 2
yCount %= 4
continue
if yCount > 0:
for i in range(yCount):
move = yTransform(move)
if prev_move == "" or prev_move == '':
prev_move = move
new_list.append(move)
continue
if move[0] == prev_move[0]:
if len(move) == 1:
if len(prev_move) <= 1:
del new_list[-1]
mv = move[0] + "2"
new_list.append(mv)
prev_move = mv
continue
if prev_move[1] == "i":
del new_list[-1]
prev_move = new_list[-1] if len(new_list) > 0 else ""
continue
if prev_move[1] == "2":
del new_list[-1]
mv = move[0] + "i"
new_list.append(mv)
prev_move = mv
continue
if move[1] == "i":
if len(prev_move) == 1:
del new_list[-1]
prev_move = new_list[-1] if len(new_list) > 0 else ""
continue
if prev_move[1] == "i":
del new_list[-1]
mv = move[0] + "2"
new_list.append(mv)
prev_move = mv
continue
if prev_move[1] == "2":
del new_list[-1]
mv = move[0]
new_list.append(mv)
prev_move = mv
continue
if move[1] == "2":
if len(prev_move) == 1:
del new_list[-1]
mv = move[0] + "i"
new_list.append(mv)
prev_move = mv
continue
if prev_move[1] == "i":
del new_list[-1]
mv = move[0]
new_list.append(mv)
prev_move = mv
continue
if prev_move[1] == "2":
del new_list[-1]
prev_move = new_list[-1] if len(new_list) > 0 else ""
continue
new_list.append(move)
prev_move = move
solution_length = len(new_list)
moves_list = new_list
#sets up the cube to perform a move by rotating that face to the top
def setup(face):
face = str.lower(face)
if face == "f":
move("X")
elif face == "r":
move("Zi")
elif face == "l":
move("Z")
elif face == "d":
move("X2")
elif face == "b":
move("Xi")
else:
raise Exception("Invalid setup; face: " + face)
#performs the inverse of setup to restore the cube's previous orientation
def undo(face):
face = str.lower(face)
if face == "f":
move("Xi")
elif face == "r":
move("Z")
elif face == "l":
move("Zi")
elif face == "d":
move("X2")
elif face == "b":
move("X")
else:
raise Exception("Invalid undo; face: " + face)
#Tokenizes a string of moves
def m(s):
s = str.replace(s, "'", "i")
k = s.split(' ')
global moves_list, solution_length
solution_length += len(k)
for word in k:
moves_list.append(word)
move(word)
#performs a move by setting up, performing U moves, and undoing the setup
def move(mv):
mv = str.lower(mv)
if mv == "u":
U()
elif mv == "u2":
move("U"); move("U");
elif mv == "ui":
move("U"); move("U"); move("U");
elif mv == "f":
setup("F"); U(); undo("F");
elif mv == "f2":
move("F"); move("F");
elif mv == "fi":
move("F"); move("F"); move("F");
elif mv == "r":
setup("R"); U(); undo("R");
elif mv == "r2":
move("R"); move("R");
elif mv == "ri":
move("R"); move("R"); move("R");
elif mv == "l":
setup("L"); U(); undo("L");
elif mv == "l2":
move("L"); move("L");
elif mv == "li":
move("L"); move("L"); move("L");
elif mv == "b":
setup("B"); U(); undo("B");
elif mv == "b2":
move("B"); move("B");
elif mv == "bi":
move("B"); move("B"); move("B");
elif mv == "d":
setup("D"); U(); undo("D");
elif mv == "d2":
move("D"); move("D");
elif mv == "di":
move("D"); move("D"); move("D");
elif mv == "x":
rotate("X")
elif mv == "x2":
move("X"); move("X");
elif mv == "xi":
move("X"); move("X"); move("X");
elif mv == "y":
rotate("Y")
elif mv == "y2":
move("Y"); move("Y");
elif mv == "yi":
move("Y"); move("Y"); move("Y");
elif mv == "z":
rotate("Z")
elif mv == "z2":
move("Z"); move("Z");
elif mv == "zi":
move("Z"); move("Z"); move("Z");
elif mv == "uw":
move("D"); move("Y");
elif mv == "uw2":
move("UW"); move("UW");
elif mv == "uwi":
move("UW"); move("UW"); move("UW");
elif mv == "m":
move("Li"); move("R"); move("Xi");
elif mv == "mi":
move("M"); move("M"); move("M");
elif mv == "m2":
move("M"); move("M");
elif mv == "rw":
move("L"); move("X");
elif mv == "rwi":
move("RW"); move("RW"); move("RW");
elif mv == "rw2":
move("RW"); move("RW");
elif mv == "fw":
move("Bi"); move("Z");
elif mv == "fwi":
move("FW"); move("FW"); move("FW");
elif mv == "fw2":
move("FW"); move("FW");
elif mv == "lw":
move("R"); move("Xi");
elif mv == "lwi":
move("LW"); move("LW"); move("LW");
elif mv == "lw2":
move("LW"); move("LW");
elif mv == "bw":
move("F"); move("Zi");
elif mv == "bwi":
move("BW"); move("BW"); move("BW");
elif mv == "bw2":
move("BW"); move("BW");
elif mv == "dw":
move("U"); move("Yi");
elif mv == "dwi":
move("DW"); move("DW"); move("DW");
elif mv == "dw2":
move("DW"); move("DW");
else:
raise Exception("Invalid Move: " + str(mv))
#rotates the entire cube along a particular axis
def rotate(axis):
axis = str.lower(axis)
if axis == 'x': #R
temp = a[0]
a[0] = a[1]
a[1] = a[4]
a[4] = a[5]
a[5] = temp
rotate_face_counterclockwise("L")
rotate_face_clockwise("R")
elif axis == 'y': #U
temp = a[1]
a[1] = a[2]
a[2] = a[5]
a[5] = a[3]
a[3] = temp
#after swaps,
rotate_face_clockwise("L")
rotate_face_clockwise("F")
rotate_face_clockwise("R")
rotate_face_clockwise("B")
rotate_face_clockwise("U")
rotate_face_counterclockwise("D")
elif axis == 'z': #F
temp = a[0]
a[0] = a[3]
a[3] = a[4]
a[4] = a[2]
a[2] = temp
rotate_face_clockwise("L"); rotate_face_clockwise("L");
rotate_face_clockwise("D"); rotate_face_clockwise("D");
rotate_face_clockwise("F")
rotate_face_counterclockwise("B")
else:
raise Exception("Invalid rotation: " + axis)
#performs a U move
def U():
#rotate U face
temp = a[0][0][0]
a[0][0][0] = a[0][2][0]
a[0][2][0] = a[0][2][2]
a[0][2][2] = a[0][0][2]
a[0][0][2] = temp
temp = a[0][0][1]
a[0][0][1] = a[0][1][0]
a[0][1][0] = a[0][2][1]
a[0][2][1] = a[0][1][2]
a[0][1][2] = temp
#rotate others
temp = a[5][2][0]
a[5][2][0] = a[3][2][2]
a[3][2][2] = a[1][0][2]
a[1][0][2] = a[2][0][0]
a[2][0][0] = temp
temp = a[5][2][1]
a[5][2][1] = a[3][1][2]
a[3][1][2] = a[1][0][1]
a[1][0][1] = a[2][1][0]
a[2][1][0] = temp
temp = a[5][2][2]
a[5][2][2] = a[3][0][2]
a[3][0][2] = a[1][0][0]
a[1][0][0] = a[2][2][0]
a[2][2][0] = temp
#Rotates a particular face counter-clockwise
def rotate_face_counterclockwise(face):
rotate_face_clockwise(face)
rotate_face_clockwise(face)
rotate_face_clockwise(face)
#Rotates a particular face clockwise
def rotate_face_clockwise(face):
f_id = -1
face = str.lower(face)
if face == "u":
f_id = 0
elif face == "f":
f_id = 1
elif face == "r":
f_id = 2
elif face == "l":
f_id = 3
elif face == "d":
f_id = 4
elif face == "b":
f_id = 5
else:
raise Exception("Invalid face: " + face)
temp = a[f_id][0][0]
a[f_id][0][0] = a[f_id][2][0]
a[f_id][2][0] = a[f_id][2][2]
a[f_id][2][2] = a[f_id][0][2]
a[f_id][0][2] = temp
temp = a[f_id][0][1]
a[f_id][0][1] = a[f_id][1][0]
a[f_id][1][0] = a[f_id][2][1]
a[f_id][2][1] = a[f_id][1][2]
a[f_id][1][2] = temp
#Randomly scrambles the cube given a number of moves, or given a list of moves
def scramble(moves=25):
global last_scramble, moves_list, solution_length, a
a = make_cube()
if hasattr(moves, '__iter__'): #scramble given a list of moves
m(moves)
moves_list = []
solution_length = 0
temp = moves.split(' ')
last_scramble = temp
else: #scramble randomly a certain number of times
moves_list = [] #reset moves_list
last_scramble = [] #reset last scramble
prevMove = ""
for i in range(moves):
while True:
thisMove = ""
r = randint(0, 5)
if r == 0:
thisMove += "U"
elif r == 1:
thisMove += "F"
elif r == 2:
thisMove += "R"
elif r == 3:
thisMove += "L"
elif r == 4:
thisMove += "D"
elif r == 5:
thisMove += "B"
if thisMove == "U" and prevMove != "U" and prevMove != "D":
break
if thisMove == "F" and prevMove != "F" and prevMove != "B":
break
if thisMove == "R" and prevMove != "R" and prevMove != "L":
break
if thisMove == "L" and prevMove != "L" and prevMove != "R":
break
if thisMove == "D" and prevMove != "D" and prevMove != "U":
break
if thisMove == "B" and prevMove != "B" and prevMove != "F":
break
r = randint(0, 3)
if r == 1:
move(thisMove + "i")
last_scramble.append(thisMove + "i")
elif r == 2:
move(thisMove + "2")
last_scramble.append(thisMove + "2")
else:
move(thisMove)
last_scramble.append(thisMove)
prevMove = thisMove
#Solves the top cross as part of the OLL step
def topCross():
# if all the edges are all equal to eachother (all being white)
if a[0][0][1] == a[0][1][0] == a[0][1][2] == a[0][2][1]:
#print("Cross already done, step skipped")
return
#If this is true, we have our cross and we can go onto the next step
else:
while a[0][0][1] != "W" or a[0][1][0] != "W" or a[0][1][2] != "W" or a[0][2][1] != "W":
if a[0][1][0] == a[0][1][2]:
#if we have a horizontal line Just do alg
m("F R U Ri Ui Fi")
break #breaking w/o having to recheck while conditions again, this will give us a cross
elif a[0][0][1] == a[0][2][1]:
# if we have a vertical line, do a U then alg
m("U F R U Ri Ui Fi")
break
elif a[0][0][1] != "W" and a[0][1][0] != "W" and a[0][1][2] != "W" and a[0][2][1] != "W":
#This would mean we have a dot case, so perform
m("F U R Ui Ri Fi U F R U Ri Ui Fi")
break
elif a[0][1][2] == a[0][2][1] or a[0][0][1] == a[0][1][0]:
# If we have an L case in the top left or the bottom right, will give us a line
m("F R U Ri Ui Fi")
else:
#This is we dont have a line, dot, cross, or L in top left or bottom right
m("U")
#returns True if the top is solved
def isTopSolved():
#determines if the top of the cube is solved.
if a[0][0][0] == a[0][0][1] == a[0][0][2] == a[0][1][0] == a[0][1][1] == a[0][1][2] == a[0][2][0] == a[0][2][1] == a[0][2][2]:
return True
else:
return False
#puts a single edge piece in the proper location for the cross
#Assumes the cross is formed on the bottom and is the yellow face
#Checks all edges in front/up face, then back-right/left if needed
def putCrossEdge():
global moves_list
for i in range(3):
if i == 1:
m("Ri U R F2") #bring out back-right edge
elif i == 2:
m("L Ui Li F2") #bring out back-left edge
for j in range(4):
for k in range(4):
if "Y" in [a[4][0][1], a[1][2][1]]:
return
m("F")
m("U")
#Performs the first step of the solution: the cross
def cross():
for i in range(4):
putCrossEdge()
assert "Y" in [a[4][0][1], a[1][2][1]]
if a[1][2][1] == "Y":
m("Fi R U Ri F2") #orient if necessary
m("Di")
#permute to correct face: move down face until 2 are lined up,
#then swap the other 2 if they need to be swapped
condition = False
while not condition:
fSame = a[1][1][1] == a[1][2][1]
rSame = a[2][1][1] == a[2][1][2]
bSame = a[5][1][1] == a[5][0][1]
lSame = a[3][1][1] == a[3][1][0]
condition = (fSame, rSame, bSame, lSame).count(True) >= 2
if not condition:
m("D")
if (fSame, rSame, bSame, lSame).count(True) == 4:
return
assert (fSame, rSame, bSame, lSame).count(True) == 2
if not fSame and not bSame:
m("F2 U2 B2 U2 F2") #swap front-back
elif not rSame and not lSame:
m("R2 U2 L2 U2 R2") #swap right-left
elif not fSame and not rSame:
m("F2 Ui R2 U F2") #swap front-right
elif not rSame and not bSame:
m("R2 Ui B2 U R2") #swap right-back
elif not bSame and not lSame:
m("B2 Ui L2 U B2") #swap back-left
elif not lSame and not fSame:
m("L2 Ui F2 U L2") #swap left-front
fSame = a[1][1][1] == a[1][2][1]
rSame = a[2][1][1] == a[2][1][2]
bSame = a[5][1][1] == a[5][0][1]
lSame = a[3][1][1] == a[3][1][0]
assert all([fSame, rSame, bSame, lSame])
#This is uses all the f2l algs to solve all the cases possible
def solveFrontSlot():
#This will be F2L, with all 42 cases
rmid = a[2][1][1]
fmid = a[1][1][1]
dmid = a[4][1][1]
#corner orientations if in U layer, first letter means the direction that the color is facing
fCorU = a[1][0][2] == dmid and a[0][2][2] == fmid and a[2][2][0] == rmid
rCorU = a[2][2][0] == dmid and a[1][0][2] == fmid and a[0][2][2] == rmid
uCorU = a[0][2][2] == dmid and a[2][2][0] == fmid and a[1][0][2] == rmid
#Corner orientations for correct location in D layer
fCorD = a[1][2][2] == dmid and a[2][2][2] == fmid and a[4][0][2] == rmid
rCorD = a[2][2][2] == dmid and a[4][0][2] == fmid and a[1][2][2] == rmid
dCorD = a[4][0][2] == dmid and a[1][2][2] == fmid and a[2][2][2] == rmid #This is solved spot
#edge orientations on U layer, normal or flipped version based on F face
norEdgeFU = a[1][0][1] == fmid and a[0][2][1] == rmid
norEdgeLU = a[3][1][2] == fmid and a[0][1][0] == rmid
norEdgeBU = a[5][2][1] == fmid and a[0][0][1] == rmid
norEdgeRU = a[2][1][0] == fmid and a[0][1][2] == rmid
norEdgeAny = norEdgeFU or norEdgeLU or norEdgeBU or norEdgeRU
flipEdgeFU = a[0][2][1] == fmid and a[1][0][1] == rmid
flipEdgeLU = a[0][1][0] == fmid and a[3][1][2] == rmid
flipEdgeBU = a[0][0][1] == fmid and a[5][2][1] == rmid
flipEdgeRU = a[0][1][2] == fmid and a[2][1][0] == rmid
flipEdgeAny = flipEdgeFU or flipEdgeLU or flipEdgeBU or flipEdgeRU
#edge orientations for normal or flipped insertion into slot
norEdgeInsert = a[1][1][2] == fmid and a[2][2][1] == rmid #This is solved spot
flipEdgeInsert = a[2][2][1] == fmid and a[1][1][2] == rmid
#these are for if the back right or front left slots are open or not
backRight = a[4][2][2] == dmid and a[5][1][2] == a[5][0][2] == a[5][1][1] and a[2][0][1] == a[2][0][2] == rmid
frontLeft = a[4][0][0] == dmid and a[1][1][0] == a[1][2][0] == fmid and a[3][2][0] == a[3][2][1] == a[3][1][1]
if dCorD and norEdgeInsert:
return
#Easy Cases
elif fCorU and flipEdgeRU: #Case 1
m("U R Ui Ri")
elif rCorU and norEdgeFU: #Case 2
m("F Ri Fi R")
elif fCorU and norEdgeLU: #Case 3
m("Fi Ui F")
elif rCorU and flipEdgeBU: #Case 4
m("R U Ri")
#Reposition Edge
elif fCorU and flipEdgeBU: #Case 5
m("F2 Li Ui L U F2")
elif rCorU and norEdgeLU: #Case 6
m("R2 B U Bi Ui R2")
elif fCorU and flipEdgeLU: #Case 7
m("Ui R U2 Ri U2 R Ui Ri")
elif rCorU and norEdgeBU: #Case 8
m("U Fi U2 F Ui F Ri Fi R")
# Reposition edge and Corner Flip
elif fCorU and norEdgeBU: #Case 9
m("Ui R Ui Ri U Fi Ui F")
elif rCorU and flipEdgeLU: #Case 10
if not backRight:
m("Ri U R2 U Ri")
else:
m("Ui R U Ri U R U Ri")
elif fCorU and norEdgeRU: #Case 11
m("Ui R U2 Ri U Fi Ui F")
elif rCorU and flipEdgeFU: # Case 12
if not backRight:
m("Ri U2 R2 U Ri")
else:
m("Ri U2 R2 U R2 U R")
elif fCorU and norEdgeFU: #Case 13
if not backRight:
m("Ri U R Fi Ui F")
else:
m("U Fi U F Ui Fi Ui F")
elif rCorU and flipEdgeRU: #Case 14
m("Ui R Ui Ri U R U Ri")
# Split Pair by Going Over
elif fCorU and flipEdgeFU: #Case 15
if not backRight:
m("Ui Ri U R Ui R U Ri")
elif not frontLeft:
m("U R Ui Ri D R Ui Ri Di")
else:
m("U Ri F R Fi U R U Ri")
elif rCorU and norEdgeRU: # Case 16
m("R Ui Ri U2 Fi Ui F")
elif uCorU and flipEdgeRU: #Case 17
m("R U2 Ri Ui R U Ri")
elif uCorU and norEdgeFU: # Case 18
m("Fi U2 F U Fi Ui F")
# Pair made on side
elif uCorU and flipEdgeBU: #Case 19
m("U R U2 R2 F R Fi")
elif uCorU and norEdgeLU: #Case 20
m("Ui Fi U2 F2 Ri Fi R")
elif uCorU and flipEdgeLU: #Case 21
m("R B U2 Bi Ri")
elif uCorU and norEdgeBU: #Case 22
m("Fi Li U2 L F")
#Weird Cases
elif uCorU and flipEdgeFU: #Case 23
m("U2 R2 U2 Ri Ui R Ui R2")
elif uCorU and norEdgeRU: #Case 24
m("U Fi Li U L F R U Ri")
#Corner in Place, edge in the U face (All these cases also have set-up moves in case the edge is in the wrong orientation
elif dCorD and flipEdgeAny: #Case 25
if flipEdgeBU:
m("U") #set-up move
elif flipEdgeLU:
m("U2") #set-up move
elif flipEdgeFU:
m("Ui") #set-up move
if not backRight:
m("R2 Ui Ri U R2")
else:
m("Ri Fi R U R Ui Ri F")
elif dCorD and norEdgeAny: #Case 26
if norEdgeRU:
m("U") #set-up move
elif norEdgeBU:
m("U2") #set-up move
elif norEdgeLU:
m("Ui") #set-up move
m("U R Ui Ri F Ri Fi R")
elif fCorD and flipEdgeAny: #Case 27
if flipEdgeBU:
m("U") #set-up move
elif flipEdgeLU:
m("U2") #set-up move
elif flipEdgeFU:
m("Ui") #set-up move
m("R Ui Ri U R Ui Ri")
elif rCorD and norEdgeAny: #Case 28
if norEdgeRU:
m("U") #set-up move
elif norEdgeBU:
m("U2") #set-up move
elif norEdgeLU:
m("Ui") #set-up move
m("R U Ri Ui F Ri Fi R")
elif fCorD and norEdgeAny: #Case 29
if norEdgeRU:
m("U") #set-up move
elif norEdgeBU:
m("U2") #set-up move
elif norEdgeLU:
m("Ui") #set-up move
m("U2 R Ui Ri Fi Ui F")
elif rCorD and flipEdgeAny: #Case 30
if flipEdgeBU:
m("U") #set-up move
elif flipEdgeLU:
m("U2") #set-up move
elif flipEdgeFU:
m("Ui") #set-up move
m("R U Ri Ui R U Ri")
#Edge in place, corner in U Face
elif uCorU and flipEdgeInsert: # Case 31
m("R U2 Ri Ui F Ri Fi R")
elif uCorU and norEdgeInsert: # Case 32
m("R2 U R2 U R2 U2 R2")
elif fCorU and norEdgeInsert: # Case 33
m("Ui R Ui Ri U2 R Ui Ri")
elif rCorU and norEdgeInsert: # Case 34
m("Ui R U2 Ri U R U Ri")
elif fCorU and flipEdgeInsert: # Case 35
m("U2 R Ui Ri Ui Fi Ui F")
elif rCorU and flipEdgeInsert: # Case 36
m("U Fi Ui F Ui R U Ri")
#Edge and Corner in place
#Case 37 is Lol case, already completed
elif dCorD and flipEdgeInsert: #Case 38 (Typical flipped f2l pair case
m("R2 U2 F R2 Fi U2 Ri U Ri")
elif fCorD and norEdgeInsert: # Case 39
m("R2 U2 Ri Ui R Ui Ri U2 Ri")
elif rCorD and norEdgeInsert: # Case 40
m("R U2 R U Ri U R U2 R2")
elif fCorD and flipEdgeInsert: #Case 41
m("F2 Li Ui L U F Ui F")
elif rCorD and flipEdgeInsert: # Case 42
m("R Ui Ri Fi Li U2 L F")
#Returns true if the f2l Corner in FR spot is inserted and oriented correctly
def f2lCorner():
return a[4][0][2] == a[4][1][1] and a[1][2][2] == a[1][1][1] and a[2][2][2] == a[2][1][1] #This is solved spot
#Returns true if the f2l edge in FR spot is inserted and oriented correctly
def f2lEdge():
return a[1][1][2] == a[1][1][1] and a[2][2][1] == a[2][1][1] #This is solved spot
#Returns true if the f2l edge and corner are properly inserted and orientated in the FR position
def f2lCorrect():
return f2lCorner() and f2lEdge()
# returns if the f2l edge is on the top layer at all
def f2lEdgeOnTop():
rmid = a[2][1][1]
fmid = a[1][1][1]
dmid = a[4][1][1]
#edge orientations on U layer, normal or flipped version based on F face
norEdgeFU = a[1][0][1] == fmid and a[0][2][1] == rmid
norEdgeLU = a[3][1][2] == fmid and a[0][1][0] == rmid
norEdgeBU = a[5][2][1] == fmid and a[0][0][1] == rmid
norEdgeRU = a[2][1][0] == fmid and a[0][1][2] == rmid
norEdgeAny = norEdgeFU or norEdgeLU or norEdgeBU or norEdgeRU
flipEdgeFU = a[0][2][1] == fmid and a[1][0][1] == rmid
flipEdgeLU = a[0][1][0] == fmid and a[3][1][2] == rmid
flipEdgeBU = a[0][0][1] == fmid and a[5][2][1] == rmid
flipEdgeRU = a[0][1][2] == fmid and a[2][1][0] == rmid
flipEdgeAny = flipEdgeFU or flipEdgeLU or flipEdgeBU or flipEdgeRU
return norEdgeAny or flipEdgeAny
#returns true if the f2l edge is inserted. Can be properly orientated, or flipped.
def f2lEdgeInserted():
rmid = a[2][1][1]
fmid = a[1][1][1]
#edge orientations for normal or flipped insertion into slot
norEdgeInsert = a[1][1][2] == fmid and a[2][2][1] == rmid #This is solved spot
flipEdgeInsert = a[2][2][1] == fmid and a[1][1][2] == rmid
return norEdgeInsert or flipEdgeInsert
#This is used to determine if the front f2l edge is inserted or not, the parameter is for the requested edge. takes BR, BL, and FL as valid
def f2lEdgeInserted2(p):
rmid = a[2][1][1]
fmid = a[1][1][1]
#edge orientations for normal or flipped insertion into slot
norEdgeInsert = a[1][1][2] == fmid and a[2][2][1] == rmid #This is solved spot
flipEdgeInsert = a[2][2][1] == fmid and a[1][1][2] == rmid
#Edge orientations in comparison to Front and Right colors
BR = (a[5][1][2] == fmid and a[2][0][1] == rmid) or (a[5][1][2] == rmid and a[2][0][1] == fmid)
BL = (a[3][0][1] == fmid and a[5][1][0] == rmid) or (a[3][0][1] == rmid and a[5][1][0] == fmid)
FL = (a[3][2][1] == fmid and a[1][1][0] == rmid) or (a[3][2][1] == rmid and a[1][1][0] == fmid)
if p == "BR":
if BR:
return True
else:
return False
elif p == "BL":
if BL:
return True
return False
elif p == "FL":
if FL:
return True
return False
elif p == "FR":
if norEdgeInsert or flipEdgeInsert:
return True
return False
#returns true if f2l corner is inserted, doesn't have to be orientated correctly
def f2lCornerInserted():
rmid = a[2][1][1]
fmid = a[1][1][1]
dmid = a[4][1][1]
#Corner orientations for correct location in D layer
fCorD = a[1][2][2] == dmid and a[2][2][2] == fmid and a[4][0][2] == rmid
rCorD = a[2][2][2] == dmid and a[4][0][2] == fmid and a[1][2][2] == rmid
dCorD = a[4][0][2] == dmid and a[1][2][2] == fmid and a[2][2][2] == rmid #This is solved spot
return fCorD or rCorD or dCorD
#Returns true if there is an f2l corner located in the FR orientation
def f2lFRCor():
rmid = a[2][1][1]
fmid = a[1][1][1]
dmid = a[4][1][1]
#corner orientations if in U layer, first letter means the direction that the color is facing
fCorU = a[1][0][2] == dmid and a[0][2][2] == fmid and a[2][2][0] == rmid
rCorU = a[2][2][0] == dmid and a[1][0][2] == fmid and a[0][2][2] == rmid
uCorU = a[0][2][2] == dmid and a[2][2][0] == fmid and a[1][0][2] == rmid
return fCorU or rCorU or uCorU
#Returns true if there is an f2l Edge located in the FU position
def f2lFUEdge():
rmid = a[2][1][1]
fmid = a[1][1][1]
norEdgeFU = a[1][0][1] == fmid and a[0][2][1] == rmid
flipEdgeFU = a[0][2][1] == fmid and a[1][0][1] == rmid
return norEdgeFU or flipEdgeFU
#returns true if f2l corner is located on the U layer
def f2lCornerOnTop():
wasFound = False
for i in range(4): #Does 4 U moves to find the corner
if f2lFRCor():
wasFound = True
m("U")
return wasFound
#Will return the loction of the corner that belongs in the FR spot. Either returns BR, BL, FL, or FR.
def f2lCornerCheck():
r = "FR"
count = 0
while count < 4:
if count == 0:
if f2lCornerInserted():
r = "FR"
elif count == 1:
if f2lCornerInserted():
r = "FL"
elif count == 2:
if f2lCornerInserted():
r = "BL"
elif count == 3:
if f2lCornerInserted():
r = "BR"
m("D")
count += 1
return r
#Will return the loction of the edge that belongs in the FR spot.
#Either returns BR, BL, FL, or FR.
def f2lEdgeCheck():
if f2lEdgeInserted2("FL"):
return "FL"
elif f2lEdgeInserted2("BL"):
return "BL"
elif f2lEdgeInserted2("BR"):
return "BR"
elif f2lEdgeInserted2("FR"):
return "FR"
else:
raise Exception("f2lEdgeCheck() Exception")
#This is for the case where the Edge is inserted, but the corner is not
def f2lEdgeNoCorner():
topEdgeTop = a[0][2][1]
topEdgeFront = a[1][0][1]
rmid = a[2][1][1]
bmid = a[5][1][1]
lmid = a[3][1][1]
fmid = a[1][1][1]
#This is for comparing the front edge to other various edges for advanced algs/lookahead
BREdge = (topEdgeTop == rmid or topEdgeTop == bmid) and (topEdgeFront == rmid or topEdgeFront == bmid)
BLEdge = (topEdgeTop == lmid or topEdgeTop == bmid) and (topEdgeFront == lmid or topEdgeFront == bmid)
FLEdge = (topEdgeTop == fmid or topEdgeTop == lmid) and (topEdgeFront == fmid or topEdgeFront == lmid)
if f2lCornerOnTop():
while True:
solveFrontSlot()
if f2lCorrect():
break
m("U")
else:
if f2lCornerCheck() == "BR":
if BREdge:
m("Ri Ui R U2")
else:
m("Ri U R U")
elif f2lCornerCheck() == "BL":
if BLEdge:
m("L U Li U")
else:
m("L Ui Li U2")
elif f2lCornerCheck() == "FL":
if FLEdge:
m("Li U L Ui")
else:
m("Li Ui L")
solveFrontSlot()
if not f2lCorrect():
raise Exception("Exception found in f2lEdgeNoCorner()")
#This is the case for if the corner is inserted, but the edge is not
def f2lCornerNoEdge():
topEdgeTop = a[0][2][1]
topEdgeFront = a[1][0][1]
rmid = a[2][1][1]
bmid = a[5][1][1]
lmid = a[3][1][1]
fmid = a[1][1][1]
#This is for comparing the front edge to other various edges for advanced algs/lookahead
BREdge = (topEdgeTop == rmid or topEdgeTop == bmid) and (topEdgeFront == rmid or topEdgeFront == bmid)
BLEdge = (topEdgeTop == lmid or topEdgeTop == bmid) and (topEdgeFront == lmid or topEdgeFront == bmid)
FLEdge = (topEdgeTop == fmid or topEdgeTop == lmid) and (topEdgeFront == fmid or topEdgeFront == lmid)
if f2lEdgeOnTop():
while True:
solveFrontSlot()
if f2lCorrect():
break
m("U")
else:
if f2lEdgeCheck() == "BR":
if BREdge:
m("Ri Ui R U2")
else:
m("Ri U R U")
elif f2lEdgeCheck() == "BL":
if BLEdge:
m("L U Li U")
else:
m("L Ui Li U2")
elif f2lEdgeCheck() == "FL":
if FLEdge:
m("Li U L Ui")
else:
m("Li Ui L")
solveFrontSlot()
if not f2lCorrect():
raise Exception("Exception found in f2lCornerNoEdge()")
#this is the case for if the corner is on top, and the edge is not. Neither are inserted properly. Edge must be in another slot.
def f2lCornerTopNoEdge():
topEdgeTop = a[0][2][1]
topEdgeFront = a[1][0][1]
rmid = a[2][1][1]
bmid = a[5][1][1]
lmid = a[3][1][1]
fmid = a[1][1][1]
#This is for comparing the front edge to other various edges for advanced algs/lookahead
BREdge = (topEdgeTop == rmid or topEdgeTop == bmid) and (topEdgeFront == rmid or topEdgeFront == bmid)
BLEdge = (topEdgeTop == lmid or topEdgeTop == bmid) and (topEdgeFront == lmid or topEdgeFront == bmid)
FLEdge = (topEdgeTop == fmid or topEdgeTop == lmid) and (topEdgeFront == fmid or topEdgeFront == lmid)
#Turn the top until the corner on the U face is in the proper position
while True:
if f2lFRCor():
break
m("U")
#We will be checking additional edges to choose a more fitting alg for the sake of looking ahead
if f2lEdgeCheck() == "BR":
if BREdge:
m("Ri Ui R")
else:
m("Ri U R")
elif f2lEdgeCheck() == "BL":