-
Notifications
You must be signed in to change notification settings - Fork 0
/
rubiks.py
429 lines (352 loc) · 13.9 KB
/
rubiks.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
import numpy as np
from typing import List , Union , Tuple , Optional
from copy import deepcopy
from random import choice
from colorama import Back , Fore , Style
DEBUG = False
CUBE_COLORS = [
f"{Back.WHITE}{Fore.BLACK} W {Style.RESET_ALL}",
f"{Back.YELLOW}{Fore.BLACK} Y {Style.RESET_ALL}",
f"{Back.RED}{Fore.BLACK} R {Style.RESET_ALL}",
f"{Back.GREEN}{Fore.BLACK} G {Style.RESET_ALL}",
f"{Back.MAGENTA}{Fore.BLACK} C {Style.RESET_ALL}",
f"{Back.BLUE}{Fore.BLACK} B {Style.RESET_ALL}"
]
VALID_MOVES = [
"U",
"U'",
"D",
"D'",
"F",
"F'",
"B",
"B'",
"L",
"L'",
"R",
"R'"
]
THETA = np.radians(90)
ROT_X_CW = lambda theta : np.array([[1,0,0],[0,np.cos(theta),-np.sin(theta)],[0,np.sin(theta),np.cos(theta)]]) # rotate clockwise around x
ROT_Y_CW = lambda theta : np.array([[np.cos(theta),0,np.sin(theta)],[0,1,0],[-np.sin(theta),0,np.cos(theta)]]) # rotate clockwise around y
ROT_Z_CW = lambda theta : np.array([[np.cos(theta),-np.sin(theta),0],[np.sin(theta),np.cos(theta),0],[0,0,1]]) # rotate clockwise around z
def cube_graph_repr(element_list,row_indent_n=18,width=17,tab_width=2):
"""
Draws the cube like,
_______________
| |
| X X X |
| X X X |
| X X X |
|_______________|
| |
| X X X |
| X X X |
| X X X |
_______________|_______________|_______________
| | | |
| X X X | X X X | X X X |
| X X X | X X X | X X X |
| X X X | X X X | X X X |
|_______________|_______________|_______________|
| |
| X X X |
| X X X |
| X X X |
|_______________|
where each X represents one elements from element_dict
"""
side_up , side_back , (side_left , side_down , side_right) , side_front = element_list
side_up = side_up[0]
side_back = side_back[0]
side_front = side_front[0]
row_indent = " "*row_indent_n
delim = "|"
w = width
tab = " "*tab_width
single_hline = "_"*w
top_mid_row = row_indent + " " + single_hline
empty_row = delim + " "*w
empty_mid_row = row_indent + empty_row + delim
row_entries = lambda ele_list : ''.join([tab + str(ele) for ele in ele_list])
ending = tab + delim
single_mid_row = lambda ele_list : row_indent + delim + row_entries(ele_list) + ending
row_upper = delim + single_hline + delim
mid_row_upper = row_indent + row_upper
wide_row_upper = " " + (single_hline + delim)*2 + single_hline
wide_row_lower = "|" + (single_hline + delim)*2 + single_hline + "|"
wide_empty_row = empty_row*3 + delim
wide_row_entries = lambda ele_list_list : ''.join([delim + row_entries(ele_list) + tab for ele_list in ele_list_list]) + delim
names = ["UP (U) ","BACK (B)", "LEFT (L) DOWN (D) RIGHT (R) ","FRONT (F)"]
text_max_width = len(max(names,key=len))
lens = [text_max_width - len(name) for name in names]
empty_tab = " "*text_max_width + "\t"
cube_str = [
empty_tab + top_mid_row,
empty_tab + empty_mid_row,
empty_tab + single_mid_row(side_up[0]),
names[0] + " "*lens[0] + '\t' + single_mid_row(side_up[1]),
empty_tab + single_mid_row(side_up[2]),
empty_tab + mid_row_upper,
empty_tab + empty_mid_row,
empty_tab + single_mid_row(side_back[0]),
names[1] + " "*lens[1] + '\t' + single_mid_row(side_back[1]),
empty_tab + single_mid_row(side_back[2]),
empty_tab + wide_row_upper,
empty_tab + wide_empty_row,
empty_tab + wide_row_entries([side_left[0],side_down[0],side_right[0]]),
names[2] + ""*lens[2] + '\t' + wide_row_entries([side_left[1],side_down[1],side_right[1]]),
empty_tab + wide_row_entries([side_left[2],side_down[2],side_right[2]]),
empty_tab + wide_row_lower,
empty_tab + empty_mid_row,
empty_tab + single_mid_row(side_front[0]),
names[3] + " "*lens[3] + '\t' + single_mid_row(side_front[1]),
empty_tab + single_mid_row(side_front[2]),
empty_tab + mid_row_upper
]
return cube_str
def draw_cube(element_list,row_indent_n=2,width=15,tab_width=3):
cube_str = cube_graph_repr(element_list,row_indent_n,width,tab_width)
for s in cube_str:
print(s)
class CubeFace:
def __init__(self, face_coord : List[int], face_col : int) -> None:
self.face_col = face_col
self.face_coord = np.array(face_coord)
@property
def x(self):
return self.face_coord[0]
@property
def y(self):
return self.face_coord[1]
@property
def z(self):
return self.face_coord[2]
def get_face(self):
return self.face_coord
def rotate_face(self,rot_mat,verbose=False):
if verbose: print("Hello i am a face with coords", self.face_coord)
self.face_coord = np.round(rot_mat @ self.face_coord)
#self.face_coord = np.array(list(map(int,self.face_coord)))
if verbose: print("I (the face) was rotated and now have coord", self.face_coord)
def __repr__(self):
return f'{CUBE_COLORS[self.face_col]}'
def __str__(self):
return f'{CUBE_COLORS[self.face_col]}'
class CubeElement:
def __init__(self,coords : List[List[int]], faces : List[CubeFace]) -> None:
self.faces = faces
self.coords = coords
def rotate_element(self,rot_mat : List[List[float]],verbose : Optional[bool] = False) -> None:
if verbose: print('Hello i am an element with coords', self.coords)
self.coords = np.round(rot_mat @ self.coords)
#self.coords = np.array(list(map(int,self.coords)))
if verbose: print(f'I am now rotating all my {len(self.faces)} faces')
for face in self.faces:
if verbose: print("rotating", face)
face.rotate_face(rot_mat,verbose=verbose)
if verbose: print('\nI (the element) was rotated and my new coords are', self.coords)
def __repr__(self) -> str:
f_string = ''
for face in self.faces:
f_string += f'{face} '
return f'\nCoords: {self.coords}\nFaces: {f_string}\n'
class Cube:
pre_prev_rotated_eles : List[CubeElement]
post_prev_rotated_eles : List[CubeElement]
def __init__(self) -> None:
white_down = CubeFace([0,0,-1],0)
ylw_up = CubeFace([0,0,1],1)
red_back = CubeFace([0,1,0],2)
green_left = CubeFace([-1,0,0],3)
orange_front = CubeFace([0,-1,0],4)
blue_right = CubeFace([1,0,0],5)
facetypes = [white_down,ylw_up,red_back,green_left,orange_front,blue_right]
# Holds the cube graphics
self.eles_graphics = [
[
np.zeros((3,3),dtype=object)
], # UP
[
np.zeros((3,3),dtype=object)
], # BACK
[
np.zeros((3,3),dtype=object),
np.zeros((3,3),dtype=object),
np.zeros((3,3),dtype=object)
], # LEFT BOTTOM RIGHT
[
np.zeros((3,3),dtype=object)
] # FRONT
]
idxes = [-1,0,1]
self.eles = []
for i in idxes:
for j in idxes:
for k in idxes:
coords = np.array([i,j,k])
faces = []
for face in facetypes:
if (face.x == i and face.x != 0) or (face.y == j and face.y != 0) or (face.z == k and face.z != 0):
facecopy = deepcopy(face)
faces.append(facecopy)
glob_row , glob_col , loc_row , loc_col = face_to_graphic(facecopy,coords)
self.eles_graphics[glob_row][glob_col][loc_row,loc_col] = face
if faces:
ele = CubeElement(coords,faces)
self.eles.append(ele)
def __repr__(self):
return '\n'.join(cube_graph_repr(self.eles_graphics))
def print_cube(self):
draw_cube(self.eles_graphics)
def move(self,rot_cmd : str, show_cube : Optional[bool] = False):
send_cmd = ""
for cmd in rot_cmd:
if not cmd == "'":
if send_cmd:
try:
self.rotate(send_cmd,show_cube)
except AssertionError:
print("Illegal move! Not rotating.")
send_cmd = cmd
else:
send_cmd += cmd
self.rotate(send_cmd,show_cube)
send_cmd = ""
if send_cmd:
self.rotate(send_cmd,show_cube)
def rotate(self,rot_cmd : str, show_cube : Optional[bool] = True, verbose : Optional[bool] = False):
#U UPPER
#R RIGHT
#L LEFT
#F FRONT
#B BACK
#D DOWN
rot_cmd = rot_cmd.upper()
assert rot_cmd in VALID_MOVES , f"Illegal move '{rot_cmd}' Move must be one of: {VALID_MOVES}"
rot_layer = rot_cmd[0]
if len(rot_cmd) == 2:
prime_cmd = "CW" if rot_cmd[1] == "'" else "ACW"
else:
prime_cmd = "ACW"
if rot_layer == "U": # OK!
# z coord must be 1
idx = 2
val = 1
rot_mat = ROT_Z_CW
rot_dir = 1 if prime_cmd == "CW" else -1
elif rot_layer == "D": # OK!
# z coord must be -1
idx = 2
val = -1
rot_mat = ROT_Z_CW
rot_dir = 1 if prime_cmd == "CW" else -1
elif rot_layer == "R": # OK!
# x coord must be 1
idx = 0
val = 1
rot_mat = ROT_X_CW
rot_dir = 1 if prime_cmd == "CW" else -1
elif rot_layer == "L": # OK!
# x coord must be -1
idx = 0
val = -1
rot_mat = ROT_X_CW
rot_dir = -1 if prime_cmd == "CW" else 1
elif rot_layer == "F": # OK!
# y coord must be 1
idx = 1
val = -1
rot_mat = ROT_Y_CW
rot_dir = -1 if prime_cmd == "CW" else 1
elif rot_layer == "B": # OK!
# y coord must be -1
idx = 1
val = 1
rot_mat = ROT_Y_CW
rot_dir = 1 if prime_cmd == "CW" else -1
else:
print('ILLEGAL MOVE!')
return
try:
eles_to_rotate = self.__filter_elements(idx,val)
self.pre_prev_rotated_eles = deepcopy(eles_to_rotate)
except NameError:
print('ILLEGAL MOVE!!')
for ele in eles_to_rotate:
ele.rotate_element(rot_mat(rot_dir*THETA))
for face in ele.faces:
glob_row , glob_col , loc_row , loc_col = face_to_graphic(face,ele.coords)
self.eles_graphics[glob_row][glob_col][int(loc_row),int(loc_col)] = face
self.post_prev_rotated_eles = eles_to_rotate
if show_cube: print(self)
# Add counting of each color as error check
def scramble(self,n_rotations=100):
for _ in range(n_rotations):
move = choice(VALID_MOVES)
self.rotate(move,show_cube=False)
print(f"Cube scrambled randomly {n_rotations} times!")
print(self)
def reset(self,show_cube=True):
self.__init__()
if show_cube: print(self)
def __filter_elements(self,coord_idx,coord_val):
eles_to_rotate = []
for ele in self.eles:
if ele.coords[coord_idx] == coord_val:
eles_to_rotate.append(ele)
return eles_to_rotate
def print_changed_eles(self):
for ele_pre , ele_post in zip(self.pre_prev_rotated_eles,self.post_prev_rotated_eles):
print(ele_pre,'|\n |\n V',ele_post)
def face_to_graphic(face,coord):
x , y , z = coord + 1
if face.z == 1: # Och pekar uppåt
if DEBUG: print(f'{face} {face.x,face.y,face.z} {coord} UPPÅT')
global_idx = 0
global_col = 0
local_row = y
local_col = x
elif face.z == -1: # Och pekar nedåt
if DEBUG: print(f'{face} {face.x,face.y,face.z} {coord} NEDÅT')
global_idx = 2
global_col = 1
local_row = 2-y
local_col = x
elif face.x == 1: # Och pekar åt höger
if DEBUG: print(f'{face} {face.x,face.y,face.z} {coord} HÖGER')
global_idx = 2
global_col = 2
local_row = 2-y
local_col = z
elif face.x == -1: # Och pekar åt vänster
if DEBUG: print(f'{face} {face.x,face.y,face.z} {coord} VÄNSTER')
global_idx = 2
global_col = 0
local_row = 2-y
local_col = 2-z
elif face.y == 1: # Och pekar bakåt
if DEBUG: print(f'{face} {face.x,face.y,face.z} {coord} BAKÅT')
global_idx = 1
global_col = 0
local_row = 2-z
local_col = x
elif face.y == -1: # Och pekar framåt
if DEBUG: print(f'{face} {face.x,face.y,face.z} {coord} FRAMÅT')
global_idx = 3
global_col = 0
local_row = z
local_col = x
else:
print('INGET!')
raise ValueError(f'Incorrect coordinate, {coord}')
return global_idx , global_col , local_row , local_col
if __name__ == "__main__":
all_cols = [[r for _ in range(3)] for r in [["R"]*3,["B"]*3,["Y"]*3,["W"]*3,["O"]*3,["G"]*3]]
#face1 = CubeFace([0,0,-1],0)
#print(face_to_graphic(face1,np.array([-1,-1,-1])))
c = Cube()
#c.print_cube()
c.rotate("L")
c.rotate("L")
#print(c)