-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrubix.py
290 lines (235 loc) · 8.68 KB
/
rubix.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
import ctypes
import sys
import re
librubix = ctypes.CDLL("./librubix.so")
# seed = librubix.rubix_cube_generate_seed()
# cuberef = librubix.rubix_cube_allocate_scrambled(seed)
# librubix.rubix_cube_print_ascii_double_stdout(cuberef)
# librubix.rubix_cube_solve_scrambled_from_seed(cuberef,seed)
# librubix.rubix_cube_print_ascii_double_stdout(cuberef)
librubix.rubix_cube_print_ascii_double_stdout.argtypes = [ctypes.c_void_p]
librubix.rubix_cube_allocate_scrambled.restype = ctypes.c_void_p
librubix.rubix_cube_allocate_solved.restype = ctypes.c_void_p
librubix.rubix_cube_free.argtypes = [ctypes.c_void_p]
librubix.rubix_cube_scramble_free.argtypes = [ctypes.c_void_p]
librubix.rubix_cube_scramble_allocate.restype = ctypes.c_void_p
librubix.rubix_cube_scramble_free.argtypes = [ctypes.c_void_p]
librubix.rubix_cube_apply_scramble.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
librubix.rubix_cube_unapply_scramble.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
# librubix.rubix_cube_free(cuberef)
class RubixCube:
FACES = [
"top",
"front",
"right",
"left",
"back",
"bottom"
]
ROTATIONS = [
"clockwise",
"counterclockwise",
"double"
]
DEFAULT_SCRAMBLE_INTENSITY = librubix.rubix_cube_get_default_scramble_intensity()
def __init__(self,seed):
if seed == 0 or seed == None:
self.ptr = librubix.rubix_cube_allocate_solved()
else:
self.ptr = librubix.rubix_cube_allocate_scrambled(int(seed))
self.scramble_ptrs = []
def __del__(self):
librubix.rubix_cube_free(self.ptr)
for ptr in self.scramble_ptrs:
librubix.rubix_cube_scramble_free(ptr)
def __eq__(self, other):
return librubix.rubix_cube_equivelence_check(self.ptr,other.ptr) == True
def is_solved(self):
return librubix.rubix_cube_is_solved(self.ptr) == True
def print3D(self):
librubix.rubix_cube_print_ascii_double_stdout(self.ptr)
def rotate(self,face,rotation):
if not face in self.FACES or not rotation in self.ROTATIONS:
return None
librubix.rubix_cube_rotate_face(self.ptr, self.FACES.index(face), self.ROTATIONS.index(rotation))
def scramble(self,intensity = DEFAULT_SCRAMBLE_INTENSITY, seed = 0):
self.scramble_ptrs.append(librubix.rubix_cube_scramble_allocate(seed,intensity))
librubix.rubix_cube_apply_scramble(self.ptr, self.scramble_ptrs[-1])
def unscramble(self, history_depth = 1):
if len(self.scramble_ptrs) == 0:
print("No scrambles in scramble history. Doing nothing.")
return
librubix.rubix_cube_unapply_scramble(self.ptr, self.scramble_ptrs[-1 * history_depth])
librubix.rubix_cube_scramble_free(self.scramble_ptrs.pop())
def reset(self):
librubix.rubix_cube_free(self.ptr)
self.ptr = librubix.rubix_cube_allocate_solved()
class CubeShell:
def exit(self,tokens):
self.do_exit = True
def show_help(self,tokens):
print("\
exit: quit the program\n\
help: show this message\n\
new: start a new cube, discarding the old one\n\
rotate: rotate a side by a rotation value\n\
sides = top, front, right, left, back, bottom\n\
rotations = clockwise (c), counterclockwise (cc), double(d)\n\
print: display a 3D representation of the cube\n\
scramble: scramble the cube and save to scramble history\n\
unscramble: unscramble the cube based on most recent scramble\n\
check: check if the cube is solved\n\
history: execute previous commands, defaults to previous\n\
n = steps back in history (max 50)\n\
! alias for history\n"
)
return
def new(self,tokens):
self.cube.reset()
self.cube.print3D()
return
ROTATION_ALIASES = {
"c" :"clockwise",
"cw" :"clockwise",
"cl" :"clockwise",
"cc" :"counterclockwise",
"ccl" :"counterclockwise",
"ccw" :"counterclockwise",
"d" :"double",
"dd" :"double",
"ddd" :"double"
}
def disambiguate_query(self,query,options):
new_options = options
for i in range(len(query)):
for j in range(len(options)):
if query[i] != options[j][i]:
new_options.remove(options[i][i])
if len(new_options) == 1:
return new_options[0]
elif len(new_options) == 0:
return None
def rotate(self,tokens):
face = "invalid"
rotation = "invalid"
if len(tokens) < 3:
print("Rotation failed: not enough arguments")
if tokens[1] not in self.cube.FACES:
print("Rotation failed: invalid face")
return
else:
face = tokens[1]
if tokens[2] not in self.cube.ROTATIONS:
try:
rotation = self.ROTATION_ALIASES[tokens[2]]
except:
print("Rotation failed: invalid rotation")
return
else:
rotation = tokens[2]
self.cube.rotate(face,rotation)
self.cube.print3D()
def print3D(self,tokens):
self.cube.print3D()
return
def scramble(self,tokens):
self.cube.scramble()
self.cube.print3D()
return
def unscramble(self,tokens):
self.cube.unscramble()
self.cube.print3D()
return
def check(self,tokens):
if self.cube.is_solved():
print("The cube is solved")
else:
print("The cube is scrambled")
MAXIMUM_ARCHEOLOGY = 50 # arbitrary
def historic_execution(self,tokens):
#print("depth: %d" % self.historic_execution_depth)
if self.historic_execution_depth >= self.MAXIMUM_ARCHEOLOGY:
print("Cannot execute hisorical command: out of our depths")
return
self.historic_execution_depth += 1
scalar = 1
if len(tokens) > 1:
try:
scalar = int(tokens[1])
except:
None
#print(len(self.history))
if scalar >= len(self.history):
print("Cannot go %d steps back: not enough history" % scalar)
return
offset = (scalar * -1) - self.historic_execution_depth
#print(offset)
print("\"%s\"" % self.history[offset])
self.parse(self.history[offset])
self.historic_execution_depth -= 1
def default(self):
if len(self.history) > 1 + self.historic_execution_depth:
self.historic_execution([])
COMMANDS = {
"exit" :exit,
"help" :show_help,
"new" :new,
"rotate" :rotate,
"print" :print3D,
"scramble" :scramble,
"unscramble":unscramble,
"check" :check,
"history" :historic_execution,
"!" :historic_execution
}
PS1 = ">> "
def parse(self,raw_command):
if not raw_command:
self.default()
else:
try:
tokens = raw_command.lower().split()
fun = self.COMMANDS[tokens[0]]
except:
self.show_help(None)
else:
fun(self,tokens)
def __init__(self):
self.do_exit = False
self.historic_execution_depth = 0
self.cube = RubixCube(0)
self.history = []
self.cube.print3D()
self.input_loop()
def input_loop(self):
while self.do_exit == False:
sys.stdout.write(self.PS1)
self.history.append(input())
self.parse(self.history[-1])
return
@classmethod
def hello(shell):
print("[[CUBE SHELL VERSION 0.1]]")
print("Generating a new, solved cube...")
@classmethod
def launch(shell):
shell.hello()
return CubeShell()
if __name__ == "__main__":
CubeShell.launch()
# TODO
#def disambiguate_querry(query,options):
# new_options = options
# for i in range(len(query)):
# for j in range(len(options)):
# if i >= len(options[j]):
# continue
# elif query[i] != options[j][i]:
# new_options.remove(options[j])
# #print(options[j][i])
# print(new_options)
# if len(new_options) == 1:
# return new_options[0]
# elif len(new_options) == 0:
# return None
#print(disambiguate_querry("to",RubixCube.FACES))