-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim.py
312 lines (258 loc) · 11.6 KB
/
sim.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
import kociemba
from ursina import *
from cube import RubiksCube
from solver import solve_cube
class Simulation(Ursina):
"""Main class for the simulation"""
cube_colors = [
color.blue, # right
color.green, # left
color.white, # top
color.yellow, # bottom
color.orange, # back
color.red, # front
]
faces = ("R", "L", "U", "D", "F", "B")
faces_to_normal = {
"R": Vec3(1, 0, 0),
"L": Vec3(-1, 0, 0),
"U": Vec3(0, 1, 0),
"D": Vec3(0, -1, 0),
"F": Vec3(0, 0, -1),
"B": Vec3(0, 0, 1),
}
stages = ["White Cross", "White Corners", "Middle Layer",
"Yellow Cross", "Yellow Edges", "Yellow Corner Position",
"Yellow Corner Orientation"]
def __init__(self):
super().__init__()
self.internal_cube = RubiksCube()
self.controller = Entity(
model='cube', scale=3, collider='box', visible=False)
self.reverse_dir = False
self.controller.input = self.controller_input
self.rotation_helper = Entity()
self.win_text_entity = Text(
y=.35, text='', color=color.green, origin=(0, 0), scale=3)
self.generate_cube()
# Commented buttons are for debugging purposes
# self.print_cube_button = Button(
# text='print cube', color=color.azure, position=(.7, .1), on_click=self.internal_cube.print_cube)
# self.print_cube_button.fit_to_text()
# self.check_solved_button = Button(
# text='check solved', color=color.azure, position=(.7, 0), on_click=self.check_for_win)
# self.check_solved_button.fit_to_text()
self.randomize_button = Button(
text='randomize', color=color.brown, position=(.7, -.1), on_click=self.randomize_cube)
self.randomize_button.fit_to_text()
self.solve_beginners_button = Button(
text='solve (beginner\'s)', color=color.azure, position=(.7, -.2), on_click=self.solve_beginners)
self.solve_beginners_button.fit_to_text()
self.solve_button = Button(
text='solve (kociemba)', color=color.olive, position=(.7, -.3), on_click=self.solve_kociemba)
self.solve_button.fit_to_text()
self.reset_button = Button(
text='reset', color=color.salmon, position=(.7, -.4), on_click=self.reset_cube)
self.reset_button.fit_to_text()
self.markers = None
self.stage_idx = 0
window.color = color._16
EditorCamera(rotation=(20, -45, 0))
# display instructions at the top left
Text(text='Controls:\nRight click + drag to rotate cube\n\nR, U, B, L, D, F for CW rotations\nShift + R, U, B, L, D, F for CCW rotations',
position=(-.7, .4), origin=(-.5, 0), scale=0.8)
def generate_cube(self):
# make a model with a separate color on each face
self.combine_parent = Entity(enabled=False)
for i in range(3):
dir = Vec3(0, 0, 0)
dir[i] = 1
e = Entity(parent=self.combine_parent, model='plane', origin_y=-.5,
texture='white_cube', color=self.cube_colors[i*2])
e.look_at(dir, 'up')
e_flipped = Entity(parent=self.combine_parent, model='plane',
origin_y=-.5, texture='white_cube', color=self.cube_colors[(i*2)+1])
e_flipped.look_at(-dir, 'up')
self.combine_parent.combine()
# place 3x3x3 cubes
self.cubes = []
for x in range(3):
for y in range(3):
for z in range(3):
e = Entity(model=copy(self.combine_parent.model), position=Vec3(
x, y, z) - (Vec3(3, 3, 3)/3), texture='white_cube')
self.cubes.append(e)
def controller_input(self, key):
"""Handles input from the controller
Args:
key (_type_): The key that was pressed
"""
if self.controller.ignore_input:
return
if held_keys["shift"]:
self.reverse_dir = True
else:
self.reverse_dir = False
dir = -1 if self.reverse_dir else 1
modifier = "'" if self.reverse_dir else ""
if key == 'r':
self.rotate_side(Vec3(1, 0, 0), dir)
self.internal_cube.move("R" + modifier)
elif key == 'u':
self.rotate_side(Vec3(0, 1, 0), dir)
self.internal_cube.move("U" + modifier)
elif key == 'b':
self.rotate_side(Vec3(0, 0, 1), dir)
self.internal_cube.move("B" + modifier)
elif key == 'l':
self.rotate_side(Vec3(-1, 0, 0), dir)
self.internal_cube.move("L" + modifier)
elif key == 'd':
self.rotate_side(Vec3(0, -1, 0), dir)
self.internal_cube.move("D" + modifier)
elif key == 'f':
self.rotate_side(Vec3(0, 0, -1), dir)
self.internal_cube.move("F" + modifier)
def rotate_side(self, normal, direction=1, speed=1):
"""Rotates a side of the cube
Args:
normal (_type_): A vector representing the normal of the side to rotate
direction (int, optional): A direction of 1 is a clockwise move and -1 a counter-clockwise move. Defaults to 1.
speed (int, optional): The speed of the rotation. Defaults to 1.
"""
if normal == Vec3(1, 0, 0):
[setattr(e, 'world_parent', self.rotation_helper)
for e in self.cubes if e.x > 0]
self.rotation_helper.animate(
'rotation_x', 90 * direction, duration=.2*speed, curve=curve.linear)
elif normal == Vec3(-1, 0, 0):
[setattr(e, 'world_parent', self.rotation_helper)
for e in self.cubes if e.x < 0]
self.rotation_helper.animate(
'rotation_x', -90 * direction, duration=.2*speed, curve=curve.linear)
elif normal == Vec3(0, 1, 0):
[setattr(e, 'world_parent', self.rotation_helper)
for e in self.cubes if e.y > 0]
self.rotation_helper.animate(
'rotation_y', 90 * direction, duration=.2*speed, curve=curve.linear)
elif normal == Vec3(0, -1, 0):
[setattr(e, 'world_parent', self.rotation_helper)
for e in self.cubes if e.y < 0]
self.rotation_helper.animate(
'rotation_y', -90 * direction, duration=.2*speed, curve=curve.linear)
elif normal == Vec3(0, 0, 1):
[setattr(e, 'world_parent', self.rotation_helper)
for e in self.cubes if e.z > 0]
self.rotation_helper.animate(
'rotation_z', -90 * direction, duration=.2*speed, curve=curve.linear)
elif normal == Vec3(0, 0, -1):
[setattr(e, 'world_parent', self.rotation_helper)
for e in self.cubes if e.z < 0]
self.rotation_helper.animate(
'rotation_z', 90 * direction, duration=.2*speed, curve=curve.linear)
invoke(self.reset_rotation_helper, delay=.3*speed)
if speed:
self.controller.ignore_input = True
invoke(setattr, self.controller,
'ignore_input', False, delay=.24*speed)
def reset_rotation_helper(self):
[setattr(e, 'world_parent', scene) for e in self.cubes]
self.rotation_helper.rotation = (0, 0, 0)
def check_for_win(self):
"""Checks if the cube is solved and displays a message if it is"""
if self.internal_cube.check_solved():
print("SOLVED")
self.controller.ignore_input = False
self.win_text_entity.text = 'SOLVED!'
self.win_text_entity.appear()
else:
self.win_text_entity.text = ''
def randomize_cube(self):
for _ in range(40):
face = random.choice(self.faces)
dir = random.choice((-1, 1))
self.rotate_side(
normal=self.faces_to_normal[face], direction=dir, speed=0)
self.internal_cube.move(face + ("'" if dir == -1 else ""))
def reset_cube(self):
for e in self.cubes:
destroy(e)
self.cubes.clear()
for x in range(3):
for y in range(3):
for z in range(3):
e = Entity(model=copy(self.combine_parent.model), position=Vec3(
x, y, z) - (Vec3(3, 3, 3)/3), texture='white_cube')
self.cubes.append(e)
for e in self.cubes:
e.rotation = (0, 0, 0)
self.internal_cube = RubiksCube()
self.win_text_entity.text = ''
self.reset_rotation_helper()
def perform_move(self, move: str, move_speed: int, change_internal_cube: bool = True):
"""Performs a move on the cube
Args:
move (str): The move to perform
move_speed (int): The speed of the move
change_internal_cube (bool, optional): If the internal cube should be changed. Defaults to True.
"""
if move[-1] == "'":
self.rotate_side(
self.faces_to_normal[move[:-1]], -1, speed=move_speed)
else:
self.rotate_side(self.faces_to_normal[move], 1, speed=move_speed)
if change_internal_cube:
self.internal_cube.move(move)
def perform_moves(self, move_list: list[str], index: int, move_speed: int, change_internal_cube: bool = True):
"""Performs a list of moves on the cube
Args:
move_list (list[str]): List of moves to perform
index (int): The index of the move to perform
move_speed (int): The speed of the move
change_internal_cube (bool, optional): If the internal cube should be changed. Defaults to True.
"""
if index >= len(move_list):
invoke(self.check_for_win, delay=.25*move_speed)
return
move = move_list[index]
self.perform_move(move, move_speed,
change_internal_cube=change_internal_cube)
if self.markers is not None:
for i in range(len(self.markers)):
if self.markers[i] == index:
self.stage_idx += 1
self.win_text_entity.text = "Stage: " + self.stages[self.stage_idx]
break
invoke(self.perform_moves, move_list, index+1, move_speed,
change_internal_cube=False, delay=.5*move_speed)
def solve_kociemba(self):
"""Solves the cube using the kociemba library"""
self.controller.ignore_input = True
moves = kociemba.solve(
self.internal_cube.to_string_notation()).split(" ")
# break up moves with a 2 at the end into two moves
move_list = []
for move in moves:
if move[-1] == "2":
move_list.append(move[:-1])
move_list.append(move[:-1])
else:
move_list.append(move)
for move in move_list:
self.internal_cube.move(move)
self.perform_moves(move_list, 0, 0.6, change_internal_cube=False)
def solve_beginners(self):
"""Solves the cube using the beginner's method"""
self.controller.ignore_input = True
self.win_text_entity.text = "Stage: " + self.stages[self.stage_idx]
moves, self.markers = solve_cube(self.internal_cube)
# break up moves with a 2 at the end into two moves
move_list = []
for move in moves:
if move[-1] == "2":
move_list.append(move[:-1])
move_list.append(move[:-1])
self.markers = [x + 1 for x in self.markers]
else:
move_list.append(move)
self.perform_moves(move_list, 0, 0.6, change_internal_cube=False)