-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcube.py
1656 lines (1389 loc) · 58.1 KB
/
cube.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
# ---KEY---
'''
values order
L 0-7
R 8-15
B 16-23
U 24-31
D 32-39
F 40-47
Unfolded cube value indices
-- -- -- 16 17 18 -- -- --
-- -- -- 23 BB 19 -- -- --
-- -- -- 22 21 20 -- -- --
00 01 02 24 25 26 08 09 10
07 LL 03 31 UU 27 15 RR 11
06 05 04 30 29 28 14 13 12
-- -- -- 40 41 42 -- -- --
-- -- -- 47 FF 43 -- -- --
-- -- -- 46 45 44 -- -- --
-- -- -- 32 33 34 -- -- --
-- -- -- 39 DD 35 -- -- --
-- -- -- 38 37 36 -- -- --
'''
# constants to increase readability
NUM_SIDE_PIECES = 8
NUM_SIDES = 6
CORRECT = 0
CLOCKWISE = 1
COUNTER_CLOCKWISE = -1
WRONG_POSITION = 2
CRISS_CROSS = 8
ZIGZAG = 7
NORMAL = 0
PRIME = 1
DOUBLE = 2
ERROR = 666
# a cube object that stores the colors on a cube in a list with 48 elements
class Cube:
sides = ("L", "R", "B", "U", "D", "F")
edges = {"L": [24, 31, 30, 40, 47, 46, 32, 39, 38, 16, 23, 22],
"R": [28, 27, 26, 20, 19, 18, 36, 35, 34, 44, 43, 42],
"B": [26, 25, 24, 2, 1, 0, 38, 37, 36, 10, 9, 8],
"U": [4, 3, 2, 22, 21, 20, 8, 15, 14, 42, 41, 40],
"D": [0, 7, 6, 46, 45, 44, 12, 11, 10, 18, 17, 16],
"F": [30, 29, 28, 14, 13, 12, 34, 33, 32, 6, 5, 4]}
faceStartIndex = {"L": 0, "R": 8, "B": 16, "U": 24, "D": 32, "F": 40}
# Equatorial Layer, between U and D, reference D
eLayer = [47, 43, 13, 9, 19, 23, 1, 5]
# Middle Layer, between R and L, reference L
mLayer = [25, 29, 41, 45, 33, 37, 17, 21]
# Standing Layer, between F and B, reference F
sLayer = [31, 27, 15, 11, 35, 39, 7, 3]
edgePieces = {"UL": (31, 3), "UR": (27, 15), "UB": (25, 21), "UF": (29, 41),
"FL": (47, 5), "FR": (43, 13), "BL": (23, 1), "BR": (19, 9),
"DL": (39, 7), "DR": (35, 11), "DB": (37, 17), "DF": (33, 45)}
# [toFace][fromFace][edgeLabel] => command
edgeToFaceCommand = {"L": {"L": {"BL": "", "DL": "", "UL": "", "FL": ""},
"R": {"UR": "U2", "FR": "F2", "BR": "B2", "DR": "D2"},
"B": {"UB": "B", "BL": "", "BR": "B2", "DB": "B'"},
"U": {"UL": "", "UR": "U2", "UB": "U'", "UF": "U"},
"D": {"DL": "", "DR": "D2", "DB": "D", "DF": "D'"},
"F": {"UF": "F'", "FL": "", "FR": "F2", "DF": "F"}},
"R": {"L": {"BL": "B2", "DL": "D2", "UL": "U2", "FL": "F2"},
"R": {"UR": "", "FR": "", "BR": "", "DR": ""},
"B": {"UB": "B'", "BL": "B2", "BR": "", "DB": "B"},
"U": {"UL": "U2", "UR": "", "UB": "U", "UF": "U'"},
"D": {"DL": "D2", "DR": "", "DB": "D'", "DF": "D"},
"F": {"UF": "F", "FL": "F2", "FR": "", "DF": "F'"}},
"B": {"L": {"BL": "", "DL": "L", "UL": "L'", "FL": "L2"},
"R": {"UR": "R", "FR": "R2", "BR": "", "DR": "R'"},
"B": {"UB": "", "BL": "", "BR": "", "DB": ""},
"U": {"UL": "U", "UR": "U'", "UB": "", "UF": "U2"},
"D": {"DL": "D'", "DR": "D", "DB": "", "DF": "D2"},
"F": {"UF": "U2", "FL": "L2", "FR": "R2", "DF": "D2"}},
"U": {"L": {"BL": "L", "DL": "L2", "UL": "", "FL": "L'"},
"R": {"UR": "", "FR": "R", "BR": "R'", "DR": "R2"},
"B": {"UB": "", "BL": "B'", "BR": "B", "DB": "B2"},
"U": {"UL": "", "UR": "", "UB": "", "UF": ""},
"D": {"DL": "L2", "DR": "R2", "DB": "B2", "DF": "F2"},
"F": {"UF": "", "FL": "F", "FR": "F'", "DF": "F2"}},
"D": {"L": {"BL": "L'", "DL": "", "UL": "L2", "FL": "L"},
"R": {"UR": "R2", "FR": "R'", "BR": "R", "DR": ""},
"B": {"UB": "B2", "BL": "L'", "BR": "R", "DB": ""},
"U": {"UL": "L2", "UR": "R2", "UB": "B2", "UF": "F2"},
"D": {"DL": "", "DR": "", "DB": "", "DF": ""},
"F": {"UF": "F2", "FL": "L", "FR": "R'", "DF": ""}},
"F": {"L": {"BL": "L2", "DL": "L'", "UL": "L", "FL": ""},
"R": {"UR": "R'", "FR": "", "BR": "R2", "DR": "R"},
"B": {"UB": "U2", "BL": "L2", "BR": "R2", "DB": "D2"},
"U": {"UL": "U'", "UR": "U", "UB": "U2", "UF": ""},
"D": {"DL": "D", "DR": "D'", "DB": "D2", "DF": ""},
"F": {"UF": "", "FL": "", "FR": "", "DF": ""}}}
cornerPieces = {"UFL": (30, 40, 4), "URF": (28, 14, 42), "UBR": (26, 20, 8), "ULB": (24, 2, 22),
"DLF": (32, 6, 46), "DFR": (34, 44, 12), "DBL": (38, 16, 0), "DRB": (36, 10, 18)}
movements = {"L": "UFDB", "R": "FUBD", "B": "RULD",
"U": "FLBR", "D": "LFRB", "F": "LURD"}
cubies = ["UFL", "URF", "UBR", "ULB", "DLF", "DFR", "DBL", "DRB",
"UL", "UR", "UB", "UF", "FL", "FR", "BL", "BR", "DL", "DR", "DB", "DF"]
# [faceThatGetsTurned][faceThatGetsChange][newFaceName]
# newFaceName = "{nameIfClockwiseRotation}{nameIfCounterClockwiseRotation}{nameIfDoubleRotation}"
faceOrder = {"L": {"F": "UBD", "U": "BDF", "B": "DFU", "D": "FUB"},
"R": {"F": "DBU", "D": "BUF", "B": "UFD", "U": "FDB"},
"B": {"D": "LUR", "L": "URD", "U": "RDL", "R": "DLU"},
"U": {"F": "RBL", "R": "BLF", "B": "LFR", "L": "FRB"},
"D": {"F": "LBR", "L": "BRF", "B": "RFL", "R": "FLB"},
"F": {"R": "ULD", "U": "LDR", "L": "DRU", "D": "RUL"}}
def __init__(self):
self.values = [0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 4, 4,
5, 5, 5, 5, 5, 5, 5, 5]
self.sideToValue = {"L": 0, "R": 1, "B": 2, "U": 3, "D": 4, "F": 5}
self.colors = {0: "r", 1: "o", 2: "y", 3: "g", 4: "b", 5: "w"}
self.edgePiecesState = {"UL": True, "UR": True, "UB": True, "UF": True,
"FL": True, "FR": True, "BL": True, "BR": True,
"DL": True, "DR": True, "DB": True, "DF": True}
self.edgesPerFace = {"L": 0, "R": 0, "B": 0, "U": 0, "D": 0, "F": 0}
self.numBadEdges = 0
self.edgesOriented = False
self.moves = ""
self.badEdges = []
self.leftCubies = []
self.rightCubies = []
self.upCubies = []
self.possibilities = []
self.visitedCorners = []
def set(self, values, colors):
print(values)
print(colors)
self.values = values
if type(colors) != "default":
for i in range(NUM_SIDES):
self.colors[i] = colors[i]
def isSolved(self):
start = 0
end = 8
for face in self.faceStartIndex:
subList = self.values.copy()[start:end]
if (len(set(subList)) != 1) or (subList[0] != self.sideToValue[face]):
return False
start += 8
end += 8
return True
def chartString(self):
string = ""
for i in range(NUM_SIDES):
side = self.sides[i]
color = self.colors[i]
string += f"{side} {color} \t"
if color == "red" or color == "blue":
string += "\t"
for j in range(NUM_SIDE_PIECES):
value = self.values[i*NUM_SIDE_PIECES + j]
character = self.colors[value]
string += f"{character} "
string += "\n"
return str(string)
def unfoldedCubeString(self):
string = ""
file = open("reference_information/unfoldedCubeKey.txt")
content = file.readlines()
SIDE_KEYWORDS = ["LL", "RR", "BB", "UU", "DD", "FF"]
for line in content:
values = line.split()
for val in values:
val = val.strip()
if val == "--":
string += " "
elif val in SIDE_KEYWORDS:
side = val[0]
sideValue = self.sideToValue[side]
string += self.colors[sideValue]
else:
index = self.values[int(val)]
string += self.colors[index]
string += " "
string += "\n"
return str(string)
def __str__(self):
string = "\n"
# string += self.chartString()
# string += "\n"
string += self.unfoldedCubeString()
return str(string)
def shiftValues(self, indices, step, prime):
pivot = step
copyValues = self.values.copy()
for i in range(len(indices)):
if prime:
oldIndex = indices[i]
newIndex = indices[pivot]
else:
oldIndex = indices[pivot]
newIndex = indices[i]
self.values[oldIndex] = copyValues[newIndex]
pivot = (pivot + 1) % len(indices)
def shiftCenters(self, sides, prime):
copySideToValues = self.sideToValue.copy()
pivot = 1
for i in range(len(sides)):
if prime:
old = sides[i]
new = sides[pivot]
else:
old = sides[pivot]
new = sides[i]
self.sideToValue[old] = copySideToValues[new]
pivot = (pivot + 1) % len(sides)
def rotations(self, algorithm):
commands = algorithm.split()
for command in commands:
command = command.strip()
self.rotation(command)
def rotation(self, command:str):
command = command.strip()
#print(command, end=" ")
self.moves += command + " "
if len(command) == 1:
self.rotate(command, False)
elif len(command) == 2:
if command[1] == "'":
self.rotate(command[0], True)
elif command[1] == "2":
self.rotate(command[0], False)
self.rotate(command[0], False)
# update which edges are oriented from an F or B turn
# if not self.edgesOriented:
# self.updateBadEdges()
def rotate(self, side, prime):
if side == "x" or side == "y" or side == "z":
self.rotateCube(side, prime)
elif side == "M" or side == "E" or side == "S":
self.rotateSlice(side, prime)
else:
self.rotateEdge(side, prime)
def rotateSlice(self, side, prime):
if side == "M":
self.shiftValues(self.mLayer, 2, prime)
sides = ["U", "F", "D", "B"]
self.shiftCenters(sides, prime)
elif side == "E":
self.shiftValues(self.eLayer, 2, prime)
sides = ["L", "F", "R", "B"]
self.shiftCenters(sides, prime)
elif side == "S":
self.shiftValues(self.sLayer, 2, prime)
sides = ["U", "R", "D", "L"]
self.shiftCenters(sides, prime)
else:
print("Input error with self.rotateSlice({}, {}). Expected M, E or S".format(
side, prime))
def rotateCube(self, side, prime):
if side == "x":
self.rotateSlice("M", not prime)
self.rotateEdge("L", not prime)
self.rotateEdge("R", prime)
elif side == "y":
self.rotateSlice("E", not prime)
self.rotateEdge("D", not prime)
self.rotateEdge("U", prime)
elif side == "z":
self.rotateSlice("S", prime)
self.rotateEdge("B", not prime)
self.rotateEdge("F", prime)
else:
print("Input error with self.rotateCube({}, {}). Expected x, y or z".format(
side, prime))
def rotateEdge(self, side, prime):
# rotate face
start = self.faceStartIndex[side]
indices = [start+i for i in range(NUM_SIDE_PIECES)]
self.shiftValues(indices, 2, prime)
# rotate edges touching the face
indices = self.edges[side].copy()
self.shiftValues(indices, 3, prime)
def areCubiesSolved(self, cubies: list):
for cubie in cubies:
if not self.isCubieSolved(cubie):
return False
return True
def isCubieSolved(self, cubie):
if (len(cubie) == 2):
return self.isEdgeCorrect(cubie)
if (len(cubie) == 3):
return self.isCornerCorrect(cubie)
def isEdgeCorrect(self, edge):
desired1 = self.sideToValue[edge[0]]
desired2 = self.sideToValue[edge[1]]
index1 = self.edgePieces[edge][0]
index2 = self.edgePieces[edge][1]
actual1 = self.values[index1]
actual2 = self.values[index2]
return (desired1 == actual1 and desired2 == actual2)
def isCornerCorrect(self, corner):
desired1 = self.sideToValue[corner[0]]
desired2 = self.sideToValue[corner[1]]
desired3 = self.sideToValue[corner[2]]
index1 = self.cornerPieces[corner][0]
index2 = self.cornerPieces[corner][1]
index3 = self.cornerPieces[corner][2]
actual1 = self.values[index1]
actual2 = self.values[index2]
actual3 = self.values[index3]
return (desired1 == actual1 and
desired2 == actual2 and
desired3 == actual3)
def isDesiredCubie(self, position, cubie):
if len(cubie) == 2:
return self.isDesiredEdge(position, cubie)
if len(cubie) == 3:
return self.isDesiredCorner(position, cubie)
def isDesiredEdge(self, position, edge):
desired1 = self.sideToValue[edge[0]]
desired2 = self.sideToValue[edge[1]]
index1 = self.edgePieces[position][0]
index2 = self.edgePieces[position][1]
actual1 = self.values[index1]
actual2 = self.values[index2]
if desired1 == actual1 and desired2 == actual2:
return True
elif desired1 == actual2 and desired2 == actual1:
return True
else:
return False
def isDesiredCorner(self, position, corner):
desired1 = self.sideToValue[corner[0]]
desired2 = self.sideToValue[corner[1]]
desired3 = self.sideToValue[corner[2]]
index1 = self.cornerPieces[position][0]
index2 = self.cornerPieces[position][1]
index3 = self.cornerPieces[position][2]
position = self.values[index1]
actual2 = self.values[index2]
actual3 = self.values[index3]
if desired1 == position and desired2 == actual2 and desired3 == actual3:
return True
elif desired1 == actual3 and desired2 == position and desired3 == actual2:
return True
elif desired1 == actual2 and desired2 == actual3 and desired3 == position:
return True
else:
return False
def stickersMatch(self, index1, index2):
return self.values[index1] == self.values[index2]
def correctCubieName(self, name):
if len(name) == 2:
ch1 = name[0]
ch2 = name[1]
for edge in self.edgePieces:
if ch1 in edge and ch2 in edge:
return edge
if len(name) == 3:
ch1 = name[0]
ch2 = name[1]
ch3 = name[2]
for corner in self.cornerPieces:
if ch1 in corner and ch2 in corner and ch3 in corner:
return corner
"""
Tells whether a corner is rotated incorrectly. Returns 0 for correct, 1 for rotated incorrectly once
clockwise, 2 for rotated incorrectly once counter-clockwise. Note that it does not require being the
corner in the correct location, it just is based off of the reference face and colorVal
"""
def isCornerRotated(self, corner: str, face: str, colorVal: int):
stickerIndices = self.cornerPieces[corner]
pivotIndex = corner.find(face)
if pivotIndex == -1:
raise ValueError(
"Face label:{} not found in the corner:{}".format(face, corner))
else:
counter = 0
for i in range(len(corner)):
index = stickerIndices[pivotIndex]
if self.values[index] == colorVal:
break
else:
counter += 1
pivotIndex = (pivotIndex + 1) % 3
if counter == 0:
return CORRECT
elif counter == 1:
return CLOCKWISE
elif counter == 2:
return COUNTER_CLOCKWISE
else:
raise ValueError(
"Color:{} not found on this corner:{}".format(colorVal, corner))
def getCubieLocation(self, cubie):
if len(cubie) == 3:
lst = self.cornerPieces.keys()
if len(cubie) == 2:
lst = self.edgePieces.keys()
for x in lst:
if self.isDesiredCubie(x, cubie):
return x
def colorOfSideOfCubie(self, side: str, cubie: str):
index = cubie.find(side)
if index == -1:
raise ValueError(
"Side:{} not found in cubie:{}".format(side, cubie))
else:
if len(cubie) == 2:
cubieIndices = self.edgePieces[cubie]
if len(cubie) == 3:
cubieIndices = self.cornerPieces[cubie]
return self.values[cubieIndices[index]]
def cubiesMatch(self, cubie1:str, cubie2:str):
if len(cubie1) == 2 and len(cubie2) == 3:
edge = cubie1
corner = cubie2
elif len(cubie1) == 3 and len(cubie2) == 2:
corner = cubie1
edge = cubie2
else:
raise ValueError("{} and {} can't match. One needs to be an edge and other needs to be a corner")
touching = True
cornerIndices = []
for ch in edge:
if ch not in corner:
touching = False
else:
cornerIndices.append(corner.find(ch))
if not touching:
raise ValueError("{} and {} can't match since they need to be next to eachother")
for i in range(len(cornerIndices)):
cornerIndices[i] = self.cornerPieces[corner][cornerIndices[i]]
edgeIndices = self.edgePieces[edge]
for index in edgeIndices:
for i in cornerIndices:
if index + 1 == i or index - 1 == i:
if self.values[index] != self.values[i]:
return False
return True
def getCommandFromIndices(self, fromIndex, toIndex, face):
toIndex = toIndex if toIndex > fromIndex else toIndex+4
result = toIndex - fromIndex
command = ""
if result == 1:
command = face
elif result == 2:
command = face + "2"
elif result == 3:
command = face + "'"
return command
def algorithmEfficiency(self, algorithm: str):
algorithm = algorithm.split()
algorithm = [x.strip() for x in algorithm]
val = 0
for x in algorithm:
if "2" in x:
val += 0.5
if "'" in x:
val += 0.1
if "F" in x or "B" in x:
val += 2.0
val += 1.0
return val
def getPossibility(self, fromPosition, toPosition, previousCommand: str, visitedCubies: list = [], offlimitsFaces: list = ["F", "B", "D"]):
visitedCubies = visitedCubies.copy()
visitedCubies.append(fromPosition)
if len(toPosition) == 1 and toPosition in fromPosition:
return previousCommand
if fromPosition == toPosition:
return previousCommand
for ch in fromPosition:
previousMoveCheck = False
if len(previousCommand) == 0:
previousMoveCheck = True
else:
previousMove = previousCommand.split().pop()
if ch not in previousMove:
previousMoveCheck = True
if ch not in offlimitsFaces and previousMoveCheck:
cubies = {}
order = self.movements[ch]
fromIndex = 0
for i in range(len(order)):
nextIndex = (i + 1) % len(order)
cubie = ch + order[i]
if len(fromPosition) == 3:
cubie += order[nextIndex]
cubie = self.correctCubieName(cubie)
if cubie == fromPosition:
fromIndex = i
if cubie not in visitedCubies:
cubies[cubie] = i
if len(cubies) == 0:
return False
for cubie in cubies:
rotation = self.getCommandFromIndices(
fromIndex, cubies[cubie], ch)
command = previousCommand + rotation + " "
result = self.getPossibility(
cubie, toPosition, command, visitedCubies, offlimitsFaces)
if result != False and result != None:
self.possibilities.append(result)
def moveCubie(self, fromPosition, toPosition, desiredFace="", offlimitCommands=[], offlimitsFaces: list = ["F", "B", "D"], getList=False):
if fromPosition == toPosition:
return ""
self.possibilities.clear()
visitedCubies = []
self.getPossibility(fromPosition, toPosition, "",
visitedCubies, offlimitsFaces)
if len(self.possibilities) == 0:
raise ValueError(
"Not possible to move {} at location {} to {} with given offlimits faces={}"
.format(fromPosition, self.getCubieLocation(fromPosition), toPosition, offlimitsFaces))
#print(self.possibilities)
if desiredFace != "":
updatedPossibilities = []
for possibility in self.possibilities:
if desiredFace in possibility:
updatedPossibilities.append(possibility)
self.possibilities = updatedPossibilities.copy()
if len(offlimitCommands) != 0:
updatedPossibilities = []
for possibility in self.possibilities:
for command in offlimitCommands:
if command not in possibility:
updatedPossibilities.append(possibility)
self.possibilities = updatedPossibilities.copy()
if getList:
return self.possibilities
bestCase = self.algorithmEfficiency(self.possibilities[0])
bestPossibility = self.possibilities[0]
for possibility in self.possibilities:
currentCase = self.algorithmEfficiency(possibility)
if currentCase < bestCase:
bestCase = currentCase
bestPossibility = possibility
return bestPossibility
def colorAt(self, index):
return self.values[index]
def getValues(self):
return self.values
def getColorsOrder(self):
return self.colors
def oppositeSide(self, referenceSide):
if referenceSide == "L":
return "R"
elif referenceSide == "R":
return "L"
elif referenceSide == "D":
return "U"
elif referenceSide == "U":
return "D"
elif referenceSide == "F":
return "B"
elif referenceSide == "B":
return "F"
def updateBadEdges(self):
total = 0
lColor = self.sideToValue["L"]
rColor = self.sideToValue["R"]
uColor = self.sideToValue["U"]
dColor = self.sideToValue["D"]
fColor = self.sideToValue["F"]
bColor = self.sideToValue["B"]
self.badEdges.clear()
for edge in self.edgesPerFace:
self.edgesPerFace[edge] = 0
for edge in self.edgePieces:
self.edgePiecesState[edge] = True
indices = self.edgePieces[edge]
initialColor = self.values[indices[0]]
secondColor = self.values[indices[1]]
if initialColor == lColor or initialColor == rColor:
total += 1
self.edgePiecesState[edge] = False
self.badEdges.append(edge)
self.edgesPerFace[edge[0]] += 1
self.edgesPerFace[edge[1]] += 1
if initialColor == fColor or initialColor == bColor:
if secondColor == uColor or secondColor == dColor:
total += 1
self.edgePiecesState[edge] = False
self.badEdges.append(edge)
self.edgesPerFace[edge[0]] += 1
self.edgesPerFace[edge[1]] += 1
self.numBadEdges = total
return total
def isFrontOrBack(self, face):
return (face == "F" or face == "B")
def isOriented(self, edge):
return self.edgePiecesState[edge]
def setToFront(self, face):
if face == "L":
self.rotation("y'")
elif face == "R":
self.rotation("y")
elif face == "U":
self.rotation("x'")
elif face == "D":
self.rotation("x")
def edgeOrientationAlgorithm(self, primaryFace, faceEdges, frontCondition, numbersOnFace):
for faceEdge in faceEdges:
side = ""
if faceEdge[0] == primaryFace:
side = faceEdge[1]
else:
side = faceEdge[0]
if self.isOriented(faceEdge) == frontCondition and self.edgesPerFace[side] == numbersOnFace:
edge = ""
for e in self.badEdges:
if side in e and primaryFace not in e:
edge = e
if primaryFace == "F":
command = self.edgeToFaceCommand["F"][side][edge]
if numbersOnFace == 2 and "2" in command:
command = command[0]
self.rotation(command)
return True
if primaryFace == "B":
command = self.edgeToFaceCommand["B"][side][edge]
if numbersOnFace == 2 and "2" in command:
command = command[0]
self.rotation(command)
return True
return False
def moveOrientedToFront(self, primaryFace, faceEdges):
for edge in faceEdges:
side = ""
if edge[0] == primaryFace:
side = edge[1]
else:
side = edge[0]
if self.edgesPerFace[side] < 4:
sideEdges = []
for e in self.edgePieces:
if side in e:
sideEdges.append(e)
for e in sideEdges:
if self.edgePiecesState[e] == True:
if primaryFace == "F":
command = self.edgeToFaceCommand["F"][side][e]
else:
command = self.edgeToFaceCommand["B"][side][e]
self.rotation(command)
return True
return False
def specialCase(self, primaryFace, faceEdges):
emptyFaceEdge = ""
emptyFaceSide = ""
if self.edgesPerFace[primaryFace] == 3:
for edge in faceEdges:
if self.edgePiecesState[edge] == True:
emptyFaceEdge = edge
break
if emptyFaceEdge[0] == primaryFace:
emptyFaceSide = emptyFaceEdge[1]
else:
emptyFaceSide = emptyFaceEdge[0]
if self.edgesPerFace[emptyFaceSide] == 0:
oppositeSide = self.oppositeSide(emptyFaceSide)
if self.edgesPerFace[oppositeSide] == 2:
return True
return False
def twoEdgeOrientation(self):
if self.edgesPerFace["F"] == 1:
self.rotation("F")
elif self.edgesPerFace["B"] == 1:
self.rotation("B")
else:
for face in self.edgesPerFace:
if self.edgesPerFace[face] == 1:
self.rotation(face)
return self.orientEdges()
return self.orientEdges()
def fourEdgeOrientation(self):
primaryFace = "F"
if self.edgesPerFace["B"] > self.edgesPerFace["F"]:
primaryFace = "B"
numEdgesPerSides = []
for edge in self.edgesPerFace:
if edge != "F" and edge != "B":
numEdgesPerSides.append(self.edgesPerFace[edge])
faceEdges = []
for edge in self.edgePiecesState:
if primaryFace in edge:
faceEdges.append(edge)
# base case where all edges are in the front
if self.edgesPerFace[primaryFace] == 4:
self.rotation(primaryFace)
return self.orientEdges()
# special case where three in front and the fourth isn't in the open layer
if self.specialCase(primaryFace, faceEdges):
self.rotation(primaryFace + "2")
return self.orientEdges()
# algorithm that handles all cases or puts it to the special case
if self.edgeOrientationAlgorithm(primaryFace, faceEdges, True, 1):
return self.orientEdges()
elif self.edgeOrientationAlgorithm(primaryFace, faceEdges, True, 2):
return self.orientEdges()
elif self.edgeOrientationAlgorithm(primaryFace, faceEdges, True, 3):
return self.orientEdges()
elif self.edgeOrientationAlgorithm(primaryFace, faceEdges, False, 2):
return self.orientEdges()
elif self.edgeOrientationAlgorithm(primaryFace, faceEdges, False, 3):
return self.orientEdges()
else:
return self.orientEdges()
def sixEdgeOrientation(self):
primaryFace = "F"
if self.edgesPerFace["B"] > self.edgesPerFace["F"]:
primaryFace = "B"
faceEdges = []
for edge in self.edgePiecesState:
if primaryFace in edge:
faceEdges.append(edge)
if self.edgesPerFace[primaryFace] == 3:
self.rotation(primaryFace)
return self.orientEdges()
elif self.edgesPerFace[primaryFace] < 3:
if self.edgeOrientationAlgorithm(primaryFace, faceEdges, True, 1):
return self.orientEdges()
elif self.edgeOrientationAlgorithm(primaryFace, faceEdges, True, 2):
return self.orientEdges()
elif self.edgeOrientationAlgorithm(primaryFace, faceEdges, True, 3):
return self.orientEdges()
elif self.edgeOrientationAlgorithm(primaryFace, faceEdges, False, 2):
return self.orientEdges()
elif self.edgeOrientationAlgorithm(primaryFace, faceEdges, False, 3):
return self.orientEdges()
else:
return self.orientEdges()
elif self.moveOrientedToFront(primaryFace, faceEdges):
return self.orientEdges()
else:
print("There was an error in the sixEdgeOrientationAlgorithm")
return False
def tenEdgeOrientation(self):
frontOriented = (self.edgesPerFace["F"] == 4)
backOriented = (self.edgesPerFace["B"] == 4)
if frontOriented and backOriented:
self.rotation("F")
self.rotation("B")
return self.orientEdges()
else:
return self.fourEdgeOrientation()
def twelveEdgeOrientation(self):
self.rotation("F")
self.rotation("B")
return self.orientEdges()
def orientEdges(self):
self.updateBadEdges()
if self.numBadEdges == 0:
self.edgesOriented = True
return True
elif self.numBadEdges == 2:
return self.twoEdgeOrientation()
elif self.numBadEdges == 4:
return self.fourEdgeOrientation()
elif self.numBadEdges == 6:
return self.sixEdgeOrientation()
elif self.numBadEdges == 8:
return self.fourEdgeOrientation()
elif self.numBadEdges == 10:
return self.fourEdgeOrientation()
elif self.numBadEdges == 12:
return self.fourEdgeOrientation()
def updateLineEdges(self):
total = 0
self.badEdges.clear()
for face in self.edgesPerFace:
self.edgesPerFace[face] = 0
for edge in self.edgePieces:
if self.isDesiredCubie(edge, "DF"):
self.edgePiecesState[edge] = "DF"
self.badEdges.append(edge)
self.edgesPerFace[edge[0]] += 1
self.edgesPerFace[edge[1]] += 1
if edge != "DF":
total += 1
elif self.isDesiredCubie(edge, "DB"):
self.edgePiecesState[edge] = "DB"
self.badEdges.append(edge)
self.edgesPerFace[edge[0]] += 1
self.edgesPerFace[edge[1]] += 1
if edge != "DB":
total += 1
else:
self.edgePiecesState[edge] = ""
self.numBadEdges = total
return total
def getDesiredAndOppositeFaces(self, dEdge):
desiredFace = ""
oppositeFace = ""
lEdges = self.edgesPerFace["L"]
rEdges = self.edgesPerFace["R"]
if lEdges == 2:
desiredFace = "L"
elif rEdges == 2:
desiredFace = "R"
elif lEdges == 1:
if "L" in dEdge:
desiredFace = "R"
else:
desiredFace = "L"
elif rEdges == 1:
if "R" in dEdge:
desiredFace = "L"
else:
desiredFace = "R"
else:
desiredFace = "R"
if desiredFace == "L":
oppositeFace = "R"
else:
oppositeFace = "L"
return desiredFace, oppositeFace
def solveLineTwoOnTheBottom(self):
# bottom pieces are across from eachother and in the correct position
if self.isCubieSolved("DF") and self.isCubieSolved("DB"):
return True
# bottom pieces are across from eachother but in swapped positions
elif self.isDesiredCubie("DB", "DF") and self.isDesiredCubie("DF", "DB"):
self.rotation("D2")
return True
# bottom pieces are across from eachother but not in the correct position
elif self.edgePiecesState["DR"] != "" and self.edgePiecesState["DL"] != "":
if self.isDesiredCubie("DR", "DF"):
self.rotation("D'")
return True
if self.isDesiredCubie("DL", "DF"):
self.rotation("D")
return True
# bottom pieces are right next to eachother
else:
fbEdge = ""