-
Notifications
You must be signed in to change notification settings - Fork 0
/
cube.py
571 lines (472 loc) · 19 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
import os
import random
from io import BytesIO
import requests
from matplotlib import image as mpimg, pyplot as plt
COLORS = {
'W': 'white',
'Y': 'yellow',
'R': 'red',
'O': 'orange',
'B': 'blue',
'G': 'green'
}
FACES = {
'U': 'up',
'D': 'down',
'L': 'left',
'R': 'right',
'F': 'front',
'B': 'back'
}
FACES_COLORS = {
'U': 'Y',
'D': 'W',
'L': 'O',
'R': 'R',
'F': 'B',
'B': 'G'
}
FACES_COLORS_INV = {v: k for k, v in FACES_COLORS.items()}
FACES_CODES = {
'U': 0,
'R': 1,
'F': 2,
'D': 3,
'L': 4,
'B': 5
}
FACES_CODES_INV = {v: k for k, v in FACES_CODES.items()}
CORNERS_CODES = {
'URF': 0,
'UFL': 1,
'ULB': 2,
'UBR': 3,
'DFR': 4,
'DLF': 5,
'DBL': 6,
'DRB': 7
}
CORNERS_CODES_INV = {v: k for k, v in CORNERS_CODES.items()}
EDGES_CODES = {
'UR': 0,
'UF': 1,
'UL': 2,
'UB': 3,
'DR': 4,
'DF': 5,
'DL': 6,
'DB': 7,
'FR': 8,
'FL': 9,
'BL': 10,
'BR': 11
}
EDGES_CODES_INV = {v: k for k, v in EDGES_CODES.items()}
class Cube:
VISUAL_CUBE_HOST = 'http://cube.rider.biz' if not 'VISUAL_CUBE_HOST' in os.environ else os.environ[
'VISUAL_CUBE_HOST']
if VISUAL_CUBE_HOST[-1] == '/':
VISUAL_CUBE_HOST = VISUAL_CUBE_HOST[:-1]
ALLOWED_MOVES = [
'U', 'U\'', 'U2',
'R', 'R\'', 'R2',
'F', 'F\'', 'F2',
'D', 'D\'', 'D2',
'L', 'L\'', 'L2',
'B', 'B\'', 'B2'
]
OTHERS_ALLOWED_MOVES = [
'M', 'M\'', 'M2',
'E', 'E\'', 'E2',
'S', 'S\'', 'S2',
'X', 'X\'', 'X2',
'Y', 'Y\'', 'Y2',
'Z', 'Z\'', 'Z2'
]
def __init__(self):
self.cube = {
'corners': {
'URF': CORNERS_CODES['URF'],
'UFL': CORNERS_CODES['UFL'],
'ULB': CORNERS_CODES['ULB'],
'UBR': CORNERS_CODES['UBR'],
'DFR': CORNERS_CODES['DFR'],
'DLF': CORNERS_CODES['DLF'],
'DBL': CORNERS_CODES['DBL'],
'DRB': CORNERS_CODES['DRB']
},
'corners_rotations': {
'URF': 0,
'UFL': 0,
'ULB': 0,
'UBR': 0,
'DFR': 0,
'DLF': 0,
'DBL': 0,
'DRB': 0
},
'edges': {
'UR': EDGES_CODES['UR'],
'UF': EDGES_CODES['UF'],
'UL': EDGES_CODES['UL'],
'UB': EDGES_CODES['UB'],
'DR': EDGES_CODES['DR'],
'DF': EDGES_CODES['DF'],
'DL': EDGES_CODES['DL'],
'DB': EDGES_CODES['DB'],
'FR': EDGES_CODES['FR'],
'FL': EDGES_CODES['FL'],
'BL': EDGES_CODES['BL'],
'BR': EDGES_CODES['BR']
},
'edges_rotations': {
'UR': 0,
'UF': 0,
'UL': 0,
'UB': 0,
'DR': 0,
'DF': 0,
'DL': 0,
'DB': 0,
'FR': 0,
'FL': 0,
'BL': 0,
'BR': 0
},
'faces': {
'U': FACES_CODES['U'],
'D': FACES_CODES['D'],
'L': FACES_CODES['L'],
'R': FACES_CODES['R'],
'F': FACES_CODES['F'],
'B': FACES_CODES['B']
}
}
def get_relative_piece_position(self, piece='URF'):
piece = ''.join([FACES_CODES_INV[self.cube['faces'][face]] for face in piece])
if len(piece) == 2:
if piece not in EDGES_CODES:
piece = piece[::-1]
return piece
def get_relative_piece(self, piece='URF'):
piece = self.get_relative_piece_position(piece)
relative_piece = ''
if len(piece) == 1:
relative_piece = FACES_CODES_INV[self.cube['faces'][piece]]
elif len(piece) == 2:
relative_piece = EDGES_CODES_INV[self.cube['edges'][piece]]
elif len(piece) == 3:
relative_piece = CORNERS_CODES_INV[self.cube['corners'][piece]]
return self.get_relative_piece_position(relative_piece)
def get_piece(self, piece='URF'):
if len(piece) == 1:
return FACES_CODES_INV[self.cube['faces'][piece]]
elif len(piece) == 2:
return EDGES_CODES_INV[self.cube['edges'][piece]]
elif len(piece) == 3:
return CORNERS_CODES_INV[self.cube['corners'][piece]]
def get_face_piece_color(self, face, piece, error=True):
if not error:
if face not in piece:
return None
if len(piece) == 1:
return FACES_COLORS[FACES_CODES_INV[self.cube['faces'][piece]]]
elif len(piece) == 2:
piece_idx = piece.index(face)
piece_idx = piece_idx + self.cube['edges_rotations'][piece]
while piece_idx not in [0, 1]:
if piece_idx > 1:
piece_idx = piece_idx - 2
elif piece_idx < 0:
piece_idx = piece_idx + 2
return FACES_COLORS[EDGES_CODES_INV[self.cube['edges'][piece]][piece_idx]]
elif len(piece) == 3:
piece_idx = piece.index(face)
piece_idx = piece_idx + self.cube['corners_rotations'][piece]
while piece_idx not in [0, 1, 2]:
if piece_idx > 2:
piece_idx = piece_idx - 3
elif piece_idx < 0:
piece_idx = piece_idx + 3
return FACES_COLORS[CORNERS_CODES_INV[self.cube['corners'][piece]][piece_idx]]
def normalize_rotations(self):
for piece in self.cube['corners_rotations']:
self.cube['corners_rotations'][piece] = self.cube['corners_rotations'][piece] % 3
for piece in self.cube['edges_rotations']:
self.cube['edges_rotations'][piece] = self.cube['edges_rotations'][piece] % 2
def get_relative_piece_color(self, face, piece):
# piece = self.get_piece(piece)
return self.get_face_piece_color(face, piece)
def search_piece(self, piece):
piece = self.get_relative_piece_position(piece)
if len(piece) == 1:
for face in FACES:
if self.get_piece(face) == piece:
return face
elif len(piece) == 2:
for edge in EDGES_CODES:
if self.get_piece(edge) == piece:
return edge
elif len(piece) == 3:
for corner in CORNERS_CODES:
if self.get_piece(corner) == piece:
return corner
def search_piece_rotation(self, piece):
piece = self.get_relative_piece_position(piece)
if len(piece) == 1:
return 0
elif len(piece) == 2:
return self.cube['edges_rotations'][piece]
elif len(piece) == 3:
return self.cube['corners_rotations'][piece]
def get_face_of_color(self, color, piece):
for face in piece:
if self.get_face_piece_color(face, piece) == color:
return face
def pprint(self):
face = 'B'
for piece_idx, piece in enumerate([
'DBL', 'DB', 'DRB',
'BL', 'B', 'BR',
'ULB', 'UB', 'UBR',
'DBL', 'BL', 'ULB', 'ULB', 'UB', 'UBR', 'UBR', 'BR', 'DRB', 'DRB', 'DB', 'DBL',
'DL', 'L', 'UL', 'UL', 'U', 'UR', 'UR', 'R', 'DR', 'DR', 'D', 'DL',
'DLF', 'FL', 'UFL', 'UFL', 'UF', 'URF', 'URF', 'FR', 'DFR', 'DFR', 'DF', 'DLF',
'UFL', 'UF', 'URF',
'FL', 'F', 'FR',
'DLF', 'DF', 'DFR', ]):
if piece_idx in [0, 3, 6, 45, 48, 51]:
print('\n' + ' ' * 4, end='')
if piece_idx >= 45:
face = 'F'
elif piece_idx in [9, 21, 33]:
print('\n', end='')
face = 'L'
elif piece_idx in [12, 24, 36]:
print(' ', end='')
face = 'U'
elif piece_idx in [15, 27, 39]:
print(' ', end='')
face = 'R'
elif piece_idx in [18, 30, 42]:
print(' ', end='')
face = 'D'
print(self.get_face_piece_color(face, piece), end='')
print()
def facelet_colors(self):
facelet_colors = [
self.get_face_piece_color('U', 'ULB'), self.get_face_piece_color('U', 'UB'),
self.get_face_piece_color('U', 'UBR'),
self.get_face_piece_color('U', 'UL'), self.get_face_piece_color('U', 'U'),
self.get_face_piece_color('U', 'UR'),
self.get_face_piece_color('U', 'UFL'), self.get_face_piece_color('U', 'UF'),
self.get_face_piece_color('U', 'URF'),
self.get_face_piece_color('R', 'URF'), self.get_face_piece_color('R', 'UR'),
self.get_face_piece_color('R', 'UBR'),
self.get_face_piece_color('R', 'FR'), self.get_face_piece_color('R', 'R'),
self.get_face_piece_color('R', 'BR'),
self.get_face_piece_color('R', 'DFR'), self.get_face_piece_color('R', 'DR'),
self.get_face_piece_color('R', 'DRB'),
self.get_face_piece_color('F', 'UFL'), self.get_face_piece_color('F', 'UF'),
self.get_face_piece_color('F', 'URF'),
self.get_face_piece_color('F', 'FL'), self.get_face_piece_color('F', 'F'),
self.get_face_piece_color('F', 'FR'),
self.get_face_piece_color('F', 'DLF'), self.get_face_piece_color('F', 'DF'),
self.get_face_piece_color('F', 'DFR'),
self.get_face_piece_color('D', 'DLF'), self.get_face_piece_color('D', 'DF'),
self.get_face_piece_color('D', 'DFR'),
self.get_face_piece_color('D', 'DL'), self.get_face_piece_color('D', 'D'),
self.get_face_piece_color('D', 'DR'),
self.get_face_piece_color('D', 'DBL'), self.get_face_piece_color('D', 'DB'),
self.get_face_piece_color('D', 'DRB'),
self.get_face_piece_color('L', 'ULB'), self.get_face_piece_color('L', 'UL'),
self.get_face_piece_color('L', 'UFL'),
self.get_face_piece_color('L', 'BL'), self.get_face_piece_color('L', 'L'),
self.get_face_piece_color('L', 'FL'),
self.get_face_piece_color('L', 'DBL'), self.get_face_piece_color('L', 'DL'),
self.get_face_piece_color('L', 'DLF'),
self.get_face_piece_color('B', 'UBR'), self.get_face_piece_color('B', 'UB'),
self.get_face_piece_color('B', 'ULB'),
self.get_face_piece_color('B', 'BR'), self.get_face_piece_color('B', 'B'),
self.get_face_piece_color('B', 'BL'),
self.get_face_piece_color('B', 'DRB'), self.get_face_piece_color('B', 'DB'),
self.get_face_piece_color('B', 'DBL')
]
return facelet_colors
def facelet(self):
facelet_colors = self.facelet_colors()
facelet = [FACES_COLORS_INV[color] for color in facelet_colors]
return facelet
def move(self, move, direction='cw', quantity=1):
faces_swap = []
if move == 'U':
corners_swap = ['URF', 'UBR', 'ULB', 'UFL']
edges_swap = ['UL', 'UF', 'UR', 'UB']
corners_swap_rotations = [0, 0, 0, 0]
edges_swap_rotations = [0, 0, 0, 0]
elif move == 'D':
corners_swap = ['DLF', 'DBL', 'DRB', 'DFR']
edges_swap = ['DL', 'DB', 'DR', 'DF']
corners_swap_rotations = [0, 0, 0, 0]
edges_swap_rotations = [0, 0, 0, 0]
elif move == 'R':
corners_swap = ['URF', 'DFR', 'DRB', 'UBR']
edges_swap = ['UR', 'FR', 'DR', 'BR']
corners_swap_rotations = [1, 2, 1, 2]
edges_swap_rotations = [0, 0, 0, 0]
elif move == 'L':
corners_swap = ['UFL', 'ULB', 'DBL', 'DLF']
edges_swap = ['UL', 'BL', 'DL', 'FL']
corners_swap_rotations = [2, 1, 2, 1]
edges_swap_rotations = [0, 0, 0, 0]
elif move == 'F':
corners_swap = ['UFL', 'DLF', 'DFR', 'URF']
edges_swap = ['UF', 'FL', 'DF', 'FR']
corners_swap_rotations = [1, 2, 1, 2]
edges_swap_rotations = [1, 1, 1, 1]
elif move == 'B':
corners_swap = ['UBR', 'DRB', 'DBL', 'ULB']
edges_swap = ['UB', 'BR', 'DB', 'BL']
corners_swap_rotations = [1, 2, 1, 2]
edges_swap_rotations = [1, 1, 1, 1]
elif move == 'M':
corners_swap = []
edges_swap = ['UF', 'UB', 'DB', 'DF']
faces_swap = ['U', 'B', 'D', 'F']
corners_swap_rotations = []
edges_swap_rotations = [1, 1, 1, 1]
elif move == 'E':
corners_swap = []
edges_swap = ['FL', 'BL', 'BR', 'FR']
faces_swap = ['F', 'L', 'B', 'R']
corners_swap_rotations = []
edges_swap_rotations = [1, 1, 1, 1]
elif move == 'S':
corners_swap = []
edges_swap = ['UL', 'DL', 'DR', 'UR']
faces_swap = ['L', 'D', 'R', 'U']
corners_swap_rotations = []
edges_swap_rotations = [1, 1, 1, 1]
elif move in ['X', 'Y', 'Z']:
for _ in range(quantity):
self.rotate_cube('x' if move == 'X' else 'y' if move == 'Y' else 'z', direction)
return
if direction == 'ccw' or quantity == 3:
edges_swap = edges_swap[::-1]
corners_swap = corners_swap[::-1]
edges_swap_rotations = edges_swap_rotations[::-1]
corners_swap_rotations = corners_swap_rotations[::-1]
faces_swap = faces_swap[::-1]
if len(corners_swap) == 4:
(self.cube['corners'][corners_swap[0]],
self.cube['corners'][corners_swap[1]],
self.cube['corners'][corners_swap[2]],
self.cube['corners'][corners_swap[3]]) = (
self.cube['corners'][corners_swap[1]],
self.cube['corners'][corners_swap[2]],
self.cube['corners'][corners_swap[3]],
self.cube['corners'][corners_swap[0]])
(self.cube['corners_rotations'][corners_swap[0]],
self.cube['corners_rotations'][corners_swap[1]],
self.cube['corners_rotations'][corners_swap[2]],
self.cube['corners_rotations'][corners_swap[3]]) = (
self.cube['corners_rotations'][corners_swap[1]] + corners_swap_rotations[0],
self.cube['corners_rotations'][corners_swap[2]] + corners_swap_rotations[1],
self.cube['corners_rotations'][corners_swap[3]] + corners_swap_rotations[2],
self.cube['corners_rotations'][corners_swap[0]] + corners_swap_rotations[3])
(self.cube['edges'][edges_swap[0]],
self.cube['edges'][edges_swap[1]],
self.cube['edges'][edges_swap[2]],
self.cube['edges'][edges_swap[3]]) = (
self.cube['edges'][edges_swap[1]],
self.cube['edges'][edges_swap[2]],
self.cube['edges'][edges_swap[3]],
self.cube['edges'][edges_swap[0]])
(self.cube['edges_rotations'][edges_swap[0]],
self.cube['edges_rotations'][edges_swap[1]],
self.cube['edges_rotations'][edges_swap[2]],
self.cube['edges_rotations'][edges_swap[3]]) = (
self.cube['edges_rotations'][edges_swap[1]] + edges_swap_rotations[0],
self.cube['edges_rotations'][edges_swap[2]] + edges_swap_rotations[1],
self.cube['edges_rotations'][edges_swap[3]] + edges_swap_rotations[2],
self.cube['edges_rotations'][edges_swap[0]] + edges_swap_rotations[3])
if len(faces_swap) == 4:
(self.cube['faces'][faces_swap[0]],
self.cube['faces'][faces_swap[1]],
self.cube['faces'][faces_swap[2]],
self.cube['faces'][faces_swap[3]]) = (
self.cube['faces'][faces_swap[1]],
self.cube['faces'][faces_swap[2]],
self.cube['faces'][faces_swap[3]],
self.cube['faces'][faces_swap[0]])
if quantity == 2:
self.move(move, direction, 1)
def rotate_cube(self, ax='x', direction='cw'):
if ax == 'x':
self.move('R', direction)
self.move('L', 'cw' if direction == 'ccw' else 'ccw')
self.move('M', 'cw' if direction == 'ccw' else 'ccw')
elif ax == 'y':
self.move('U', direction)
self.move('D', 'cw' if direction == 'ccw' else 'ccw')
self.move('E', 'cw' if direction == 'ccw' else 'ccw')
elif ax == 'z':
self.move('F', direction)
self.move('B', 'cw' if direction == 'ccw' else 'ccw')
self.move('S', direction)
def is_solved(self, cube_rotates=None):
facelets = self.facelet_colors()
for i in range(0, 54, 9):
if facelets[i:i + 9] != [facelets[i]] * 9:
return False
return True
def is_cross_solved(self):
for edge in [edge for edge in EDGES_CODES if 'D' in edge]:
for face in edge:
if self.get_face_piece_color(face, edge) != self.get_face_piece_color(face, face):
return False
return True
def moves(self, moves=None):
moves_ = ['F', 'F\'', 'F2', 'B', 'B\'', 'B2', 'R', 'R\'', 'R2', 'L', 'L\'', 'L2', 'U', 'U\'', 'U2', 'D', 'D\'',
'D2']
if moves is None:
moves = random.choices(moves_, k=100)
elif type(moves) is str:
moves = moves.split(' ')
for move in moves:
self.move(move[0],
'cw' if len(move) == 1 else 'ccw' if move[1] == '\'' else 'cw',
1 if len(move) == 1 else 2 if move[1] == '2' else 1)
def visualcube_url(self):
url = f"{self.VISUAL_CUBE_HOST}/visualcube.php?fmt=png&size=200&fc={''.join(self.facelet_colors()).lower()}"
return url
def visualcube_image(self):
response = requests.get(self.visualcube_url())
return mpimg.imread(BytesIO(response.content))
def plot(self):
img = self.visualcube_image()
plt.imshow(img)
plt.show()
def white_to_bottom(self):
if self.cube['faces']['U'] == FACES_CODES['D']:
self.move('X', 'cw', 2)
elif self.cube['faces']['F'] == FACES_CODES['D']:
self.move('X', 'ccw', 1)
elif self.cube['faces']['B'] == FACES_CODES['D']:
self.move('X', 'cw', 1)
elif self.cube['faces']['R'] == FACES_CODES['D']:
self.move('Z', 'cw', 1)
elif self.cube['faces']['L'] == FACES_CODES['D']:
self.move('Z', 'ccw', 1)
else:
return
def blue_to_front(self):
if self.cube['faces']['R'] == FACES_CODES['F']:
self.move('Y', 'cw', 1)
elif self.cube['faces']['L'] == FACES_CODES['F']:
self.move('Y', 'ccw', 1)
elif self.cube['faces']['B'] == FACES_CODES['F']:
self.move('Y', 'cw', 2)
else:
return