-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcube.py
347 lines (289 loc) · 13 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
import random
import kociemba
import copy
#function to change each face in a turn.
def turnSide(side, copy1, copy2, copy3, numMoves, isPrime,
falseSide1, falseSide2,
falseCopy1, falseCopy2,
trueSide1, trueSide2,
trueCopy1, trueCopy2,
twoSide1, twoSide2,
twoCopy1, twoCopy2,
):
"""
Changes each face when a turn is made
"""
if numMoves == 1:
if not isPrime:
side[falseSide1][falseSide2] = copy1[falseCopy1][falseCopy2]
elif isPrime:
side[trueSide1][trueSide2] = copy2[trueCopy1][trueCopy2]
elif numMoves == 2:
side[twoSide1][twoSide2] = copy3[twoCopy1][twoCopy2]
class Cube():
# All valid 3x3x3 moves in half-turn metric
notations = {"U", "D",
"R", "L",
"F", "B"}
u_side = [["U", "U", "U"], ["U", "U", "U"], ["U", "U", "U"]]
r_side = [["R", "R", "R"], ["R", "R", "R"], ["R", "R", "R"]]
f_side = [["F", "F", "F"], ["F", "F", "F"], ["F", "F", "F"]]
d_side = [["D", "D", "D"], ["D", "D", "D"], ["D", "D", "D"]]
l_side = [["L", "L", "L"], ["L", "L", "L"], ["L", "L", "L"]]
b_side = [["B", "B", "B"], ["B", "B", "B"], ["B", "B", "B"]]
def rotation_side(self, side, copy, numMoves, isPrime):
"""
Rotates a given side when
that same move is made.
"""
# print("side before rotation: ", side)
if numMoves == 1:
if isPrime == False:
side[0] = [copy[2][0], copy[1][0], copy[0][0]]
side[1] = [copy[2][1], copy[1][1], copy[0][1]]
side[2] = [copy[2][2], copy[1][2], copy[0][2]]
elif isPrime == True:
side[0] = [copy[0][2], copy[1][2], copy[2][2]]
side[1] = [copy[0][1], copy[1][1], copy[2][1]]
side[2] = [copy[0][0], copy[1][0], copy[2][0]]
elif numMoves == 2:
side[0] = [copy[2][2], copy[2][1], copy[2][0]]
side[1] = [copy[1][2], copy[1][1], copy[1][0]]
side[2] = [copy[0][2], copy[0][1], copy[0][0]]
# print("side after rotation: ", side)
def u_move(self, numMoves, isPrime=False):
"""Applies a certain type of 'U' move to self.u_side
numMoves: int; number of 'U' moves
isPrime: bool; is 'U' move prime or not?
"""
r_copy = copy.deepcopy(self.r_side)
f_copy = copy.deepcopy(self.f_side)
l_copy = copy.deepcopy(self.l_side)
b_copy = copy.deepcopy(self.b_side)
u_copy = copy.deepcopy(self.u_side)
for r in range(3):
turnSide(self.r_side, b_copy, f_copy, l_copy, numMoves, isPrime,
0, r, 0, r, 0, r, 0, r, 0, r, 0, r)
for f in range(3):
turnSide(self.f_side, r_copy, l_copy, b_copy, numMoves, isPrime,
0, f, 0, f, 0, f, 0, f, 0, f, 0, f)
for l in range(3):
turnSide(self.l_side, f_copy, b_copy, r_copy, numMoves, isPrime,
0, l, 0, l, 0, l, 0, l, 0, l, 0, l)
for b in range(3):
turnSide(self.b_side, l_copy, r_copy, f_copy, numMoves, isPrime,
0, b, 0, b, 0, b, 0, b, 0, b, 0, b)
self.rotation_side(self.u_side, u_copy, numMoves, isPrime)
def r_move(self, numMoves, isPrime=False):
"""Applies a certain type of 'R' move to self.r_side
numMoves: int; number of 'R' moves
isPrime: bool; is 'R' move prime or not?
"""
u_copy = copy.deepcopy(self.u_side)
f_copy = copy.deepcopy(self.f_side)
d_copy = copy.deepcopy(self.d_side)
b_copy = copy.deepcopy(self.b_side)
r_copy = copy.deepcopy(self.r_side)
for u in range(3):
turnSide(self.u_side, f_copy, b_copy, d_copy, numMoves, isPrime,
u, 2, u, 2, u, 2, 2-u, 0, u, 2, u, 2)
for f in range(3):
turnSide(self.f_side, d_copy, u_copy, b_copy, numMoves, isPrime,
f, 2, f, 2, f, 2, f, 2, f, 2, 2-f, 0)
for d in range(3):
turnSide(self.d_side, b_copy, f_copy, u_copy, numMoves, isPrime,
d, 2, 2-d, 0, d, 2, d, 2, d, 2, d, 2)
for b in range(3):
turnSide(self.b_side, u_copy, d_copy, f_copy, numMoves, isPrime,
b, 0, 2-b, 2, b, 0, 2-b, 2, b, 0, 2-b, 2)
self.rotation_side(self.r_side, r_copy, numMoves, isPrime)
def f_move(self, numMoves, isPrime=False):
"""Applies a certain type of 'F' move to self.u_side
numMoves: int; number of 'F' moves
isPrime: bool; is 'F' move prime or not?
"""
r_copy = copy.deepcopy(self.r_side)
u_copy = copy.deepcopy(self.u_side)
l_copy = copy.deepcopy(self.l_side)
d_copy = copy.deepcopy(self.d_side)
f_copy = copy.deepcopy(self.f_side)
for r in range(3):
turnSide(self.r_side, u_copy, d_copy, l_copy, numMoves, isPrime,
r, 0, 2, r, r, 0, 0, 2-r, r, 0, 2-r, 2)
for u in range(3):
turnSide(self.u_side, l_copy, r_copy, d_copy, numMoves, isPrime,
2, u, 2-u, 2, 2, u, u, 0, 2, u, 0, 2-u)
for l in range(3):
turnSide(self.l_side, d_copy, u_copy, r_copy, numMoves, isPrime,
l, 2, 0, l, l, 2, 2, 2-l, l, 2, 2-l, 0)
for d in range(3):
turnSide(self.d_side, r_copy, l_copy, u_copy, numMoves, isPrime,
0, d, 2-d, 0, 0, d, d, 2, 0, d, 2, 2-d)
self.rotation_side(self.f_side, f_copy, numMoves, isPrime)
def d_move(self, numMoves, isPrime=False):
"""Applies a certain type of 'D' move to self.u_side
numMoves: int; number of 'D' moves
isPrime: bool; is 'D' move prime or not?
"""
r_copy = copy.deepcopy(self.r_side)
f_copy = copy.deepcopy(self.f_side)
l_copy = copy.deepcopy(self.l_side)
b_copy = copy.deepcopy(self.b_side)
d_copy = copy.deepcopy(self.d_side)
for r in range(3):
turnSide(self.r_side, f_copy, b_copy, l_copy, numMoves, isPrime,
2, r, 2, r, 2, r, 2, r, 2, r, 2, r)
for f in range(3):
turnSide(self.f_side, l_copy, r_copy, b_copy, numMoves, isPrime,
2, f, 2, f, 2, f, 2, f, 2, f, 2, f)
for l in range(3):
turnSide(self.l_side, b_copy, f_copy, r_copy, numMoves, isPrime,
2, l, 2, l, 2, l, 2, l, 2, l, 2, l)
for b in range(3):
turnSide(self.b_side, r_copy, l_copy, f_copy, numMoves, isPrime,
2, b, 2, b, 2, b, 2, b, 2, b, 2, b)
self.rotation_side(self.d_side, d_copy, numMoves, isPrime)
def l_move(self, numMoves, isPrime=False):
"""Applies a certain type of 'L' move to self.r_side
numMoves: int; number of 'L' moves
isPrime: bool; is 'L' move prime or not?
"""
u_copy = copy.deepcopy(self.u_side)
f_copy = copy.deepcopy(self.f_side)
d_copy = copy.deepcopy(self.d_side)
b_copy = copy.deepcopy(self.b_side)
l_copy = copy.deepcopy(self.l_side)
for u in range(3):
turnSide(self.u_side, b_copy, f_copy, d_copy, numMoves, isPrime,
u, 0, 2-u, 2, u, 0, u, 0, u, 0, u, 0)
for f in range(3):
turnSide(self.f_side, u_copy, d_copy, b_copy, numMoves, isPrime,
f, 0, f, 0, f, 0, f, 0, f, 0, 2-f, 2)
for d in range(3):
turnSide(self.d_side, f_copy, b_copy, u_copy, numMoves, isPrime,
d, 0, d, 0, d, 0, 2-d, 2, d, 0, d, 0)
for b in range(3):
turnSide(self.b_side, d_copy, u_copy, f_copy, numMoves, isPrime,
b, 2, 2-b, 0, b, 0, 2-b, 0, b, 2, 2-b, 0)
self.rotation_side(self.l_side, l_copy, numMoves, isPrime)
def b_move(self, numMoves, isPrime=False):
"""Applies a certain type of 'B' move to self.u_side
numMoves: int; number of 'B' moves
isPrime: bool; is 'B' move prime or not?
"""
r_copy = copy.deepcopy(self.r_side)
u_copy = copy.deepcopy(self.u_side)
l_copy = copy.deepcopy(self.l_side)
d_copy = copy.deepcopy(self.d_side)
b_copy = copy.deepcopy(self.b_side)
for r in range(3):
turnSide(self.r_side, d_copy, u_copy, l_copy, numMoves, isPrime,
r, 2, 2, 2-r, r, 2, 0, r, r, 2, 2-r, 0)
for u in range(3):
turnSide(self.u_side, r_copy, l_copy, d_copy, numMoves, isPrime,
0, u, u, 2, 0, u, 2-u, 0, 0, u, 2, 2-u)
for l in range(3):
turnSide(self.l_side, u_copy, d_copy, r_copy, numMoves, isPrime,
l, 0, 0, 2-l, l, 0, 2, l, l, 0, 2-l, 2)
for d in range(3):
turnSide(self.d_side, l_copy, r_copy, u_copy, numMoves, isPrime,
2, d, d, 0, 2, d, 2-d, 2, 2, d, 0, 2-d)
self.rotation_side(self.b_side, b_copy, numMoves, isPrime)
def __str__(self):
"""Prints the current state
of the cube. Should look something
like this:
|************|
|*U1**U2**U3*|
|************|
|*U4**U5**U6*|
|************|
|*U7**U8**U9*|
|************|
************|************|************|************
*L1**L2**L3*|*F1**F2**F3*|*R1**R2**R3*|*B1**B2**B3*
************|************|************|************
*L4**L5**L6*|*F4**F5**F6*|*R4**R5**R6*|*B4**B5**B6*
************|************|************|************
*L7**L8**L9*|*F7**F8**F9*|*R7**R8**R9*|*B7**B8**B9*
************|************|************|************
|************|
|*D1**D2**D3*|
|************|
|*D4**D5**D6*|
|************|
|*D7**D8**D9*|
|************|
"""
with open('cube.txt') as f:
diagram = f.read()
u_state = [self.u_side[i][j] for i in range(3) for j in range(3)]
l_state = [self.l_side[i][j] for i in range(3) for j in range(3)]
f_state = [self.f_side[i][j] for i in range(3) for j in range(3)]
r_state = [self.r_side[i][j] for i in range(3) for j in range(3)]
b_state = [self.b_side[i][j] for i in range(3) for j in range(3)]
d_state = [self.d_side[i][j] for i in range(3) for j in range(3)]
cubeState = u_state + \
l_state[:3] + f_state[:3] + r_state[:3] + b_state[:3] + \
l_state[3:6] + f_state[3:6] + r_state[3:6] + b_state[3:6] + \
l_state[6:9] + f_state[6:9] + r_state[6:9] + b_state[6:9] + \
d_state
state = diagram.format(*cubeState)
return state
#switch statement to be used to execute moves
def executeSwitch(self, move, args):
"""
Replicates a switch-case statement
to be used to in self.execute
"""
#dictionary to assign each move to a function
switcher = {"U": self.u_move,
"R": self.r_move,
"F": self.f_move,
"D": self.d_move,
"L": self.l_move,
"B": self.b_move
}
#function to get the move, and thus, the function
func = switcher.get(move[0])
func(*args)
def execute(self, moves):
"""Given a list of moves,
executes them regardless of
current state of cube.
"""
for move in moves:
if len(move) == 1:
args = (1,)
elif move[1] == "2":
args = (2,)
elif move[1] == "'":
args = (1, True)
self.executeSwitch(move[0], args)
def get_scramble(self, lenscram):
"""Returns a list of moves
to scramble the cube.
"""
scramble = []
for i in range(lenscram):
if i == 0:
move = random.choice(list(self.notations)) + random.choice(["", "'", "2"])
else:
move = random.choice(list(self.notations - set(scramble[-1][0]))) + random.choice(["", "'", "2"])
scramble.append(move)
return scramble
def get_solve(self):
"""Returns the optimal
solution to the current
state of the cube.
"""
#urfdlb
cubeState = self.u_side + self.r_side + self.f_side + self.d_side + self.l_side + self.b_side
#goes through each list in cubeState, then each letter in each list
cubeState = [j for i in cubeState for j in i]
#joins together all the letters in cubeState
cubeState = ''.join(cubeState)
cubeState = kociemba.solve(cubeState)
#separates the solution between spaces
cubeState = cubeState.split(" ")
return cubeState