-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcube.py
242 lines (204 loc) · 10.8 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
"""
Rubix Cube Solver - Cube Object(s)
v0.11 (alpha)
History available at github.com/OliverMatthews/rubiksCube/
Oli Matthews 2019
"""
# Imports relevant libraries.
from random import randint
import rotations as rt
import dev
import copy
# Contains functions for the Rubik's cube object.
class rubikCube:
def __init__(self, cubeSize):
# Initialises the sides of the cube
self.side1 = [] # Green
self.side2 = [] # White
self.side3 = [] # Red
self.side4 = [] # Yellow
self.side5 = [] # Orange
self.side6 = [] # Blue
# Initialises other parameters for the cube.
self.cubeSize = cubeSize
self.sequenceLog = ""
self.layout = []
# Fills the cube sides with the correct default colours.
for i in range(cubeSize):
self.side1.append([])
self.side2.append([])
self.side3.append([])
self.side4.append([])
self.side5.append([])
self.side6.append([])
for j in range(cubeSize):
self.side1[i].append("G")
self.side2[i].append("W")
self.side3[i].append("R")
self.side4[i].append("Y")
self.side5[i].append("O")
self.side6[i].append("B")
def saveCurrentLayout(self):
self.layout.append(copy.deepcopy(self.side1))
self.layout.append(copy.deepcopy(self.side2))
self.layout.append(copy.deepcopy(self.side3))
self.layout.append(copy.deepcopy(self.side4))
self.layout.append(copy.deepcopy(self.side5))
self.layout.append(copy.deepcopy(self.side6))
# Displays the current state of the cube to the console. Very useful for
# debugging!
def displayState(self):
print("Side 1")
for i in range(self.cubeSize):
print(self.side1[i])
print("Side 2")
for i in range(self.cubeSize):
print(self.side2[i])
print("Side 3")
for i in range(self.cubeSize):
print(self.side3[i])
print("Side 4")
for i in range(self.cubeSize):
print(self.side4[i])
print("Side 5")
for i in range(self.cubeSize):
print(self.side5[i])
print("Side 6")
for i in range(self.cubeSize):
print(self.side6[i])
# Uses the functions from the rotations library to rotate the game cube
# given the face to be rotated and the number of turns to rotate it by.
def rotateSide(self, face, turns):
# Given 'L' as input, rotates the left face by a given number of turns.
if face == "L":
for i in range(turns):
self.side1, self.side2, self.side6, self.side4, self.side5 = rt.rotateLeft(self.side1, self.side2, self.side6, self.side4, self.side5, False)
# Given 'R' as input, rotates the right face by a given number of turns.
elif face == "R":
for i in range(turns):
self.side1, self.side2, self.side6, self.side4, self.side3 = rt.rotateRight(self.side1, self.side2, self.side6, self.side4, self.side3, False)
# Given 'U' as input, rotates the up face by a given number of turns.
elif face == "U":
for i in range(turns):
self.side2, self.side3, self.side4, self.side5, self.side6 = rt.rotateUp(self.side2, self.side3, self.side4, self.side5, self.side6, False)
# Given 'D' as input, rotates the down face by a given number of turns.
elif face == "D":
for i in range(turns):
self.side2, self.side3, self.side4, self.side5, self.side1 = rt.rotateDown(self.side2, self.side3, self.side4, self.side5, self.side1, False)
# Given 'F' as input, rotates the front face by a given number of turns.
elif face == "F":
for i in range(turns):
self.side1, self.side3, self.side6, self.side5, self.side2 = rt.rotateFront(self.side1, self.side3, self.side6, self.side5, self.side2, False)
# Given 'B' as input, rotates the back face by a given number of turns.
elif face == "B":
for i in range(turns):
self.side1, self.side5, self.side6, self.side3, self.side4 = rt.rotateBack(self.side1, self.side5, self.side6, self.side3, self.side4, False)
# Given 'LI' as input, rotates the left face inverse by 1 turn.
elif face == "LI":
self.side1, self.side2, self.side6, self.side4, self.side5 = rt.rotateLeft(self.side1, self.side2, self.side6, self.side4, self.side5, True)
# Given 'RI' as input, rotates the right face inverse by 1 turn.
elif face == "RI":
self.side1, self.side2, self.side6, self.side4, self.side3 = rt.rotateRight(self.side1, self.side2, self.side6, self.side4, self.side3, True)
# Given 'UI' as input, rotates the up face inverse by 1 turn.
elif face == "UI":
self.side2, self.side3, self.side4, self.side5, self.side6 = rt.rotateUp(self.side2, self.side3, self.side4, self.side5, self.side6, True)
# Given 'DI' as input, rotates the down face inverse by 1 turn.
elif face == "DI":
self.side2, self.side3, self.side4, self.side5, self.side1 = rt.rotateDown(self.side2, self.side3, self.side4, self.side5, self.side1, True)
# Given 'FI' as input, rotates the front face inverse by 1 turn.
elif face == "FI":
self.side1, self.side3, self.side6, self.side5, self.side2 = rt.rotateFront(self.side1, self.side3, self.side6, self.side5, self.side2, True)
# Given 'BI' as input, rotates the back face inverse by 1 turn.
elif face == "BI":
self.side1, self.side5, self.side6, self.side3, self.side4 = rt.rotateBack(self.side1, self.side5, self.side6, self.side3, self.side4, True)
# Takes a sequence of moves of unlimited length, decodes the instructions,
# and follows them sequentially. 'inputSequence' must be a string with
# pairs consisting of a letter and a number - denoting the face to rotate,
# and the number of turns to rotate that face.
def followSequence(self, inputSequence):
# Part of dev functionality, default is off.
if dev.devSettings.sequencePrinting == True:
print(inputSequence)
for i in range(int(len(inputSequence)/2)):
if inputSequence[1] == "I":
if inputSequence[0] == "L":
self.rotateSide("LI", 1)
elif inputSequence[0] == "R":
self.rotateSide("RI", 1)
elif inputSequence[0] == "U":
self.rotateSide("UI", 1)
elif inputSequence[0] == "D":
self.rotateSide("DI", 1)
elif inputSequence[0] == "F":
self.rotateSide("FI", 1)
elif inputSequence[0] == "B":
self.rotateSide("BI", 1)
else:
if inputSequence[0] == "L":
self.rotateSide("L", int(inputSequence[1]))
elif inputSequence[0] == "R":
self.rotateSide("R", int(inputSequence[1]))
elif inputSequence[0] == "U":
self.rotateSide("U", int(inputSequence[1]))
elif inputSequence[0] == "D":
self.rotateSide("D", int(inputSequence[1]))
elif inputSequence[0] == "F":
self.rotateSide("F", int(inputSequence[1]))
elif inputSequence[0] == "B":
self.rotateSide("B", int(inputSequence[1]))
# Part of dev functionality, default is off.
if dev.devSettings.saveSolutions == True:
self.sequenceLog = self.sequenceLog + str(inputSequence[:2])
# Removes the last instruction from the sequence once complete.
inputSequence = inputSequence[2:]
# Randomly shuffles the cube a given number of times to ensure a completely
# random shuffle. 25 is recommended as the minimum to ensure a decent
# shuffle, but anything more than 100 or so is probably overkill.
def randomShuffle(self, numberOfShuffles):
# Blank string which will later hold the instructions for randomly
# shuffling the cube.
shufflingSequence = ""
for i in range(numberOfShuffles):
# Gets a random integer.
turnDirection = randint(1,18)
# Uses the random integer to pick a direction to rotate the cube,
# then adds that instruction to the sequence.
if turnDirection == 1:
shufflingSequence = shufflingSequence + "L1" # Left 1 turn
elif turnDirection == 2:
shufflingSequence = shufflingSequence + "L2" # Left 2 turns
elif turnDirection == 3:
shufflingSequence = shufflingSequence + "L3" # Left 3 turns
elif turnDirection == 4:
shufflingSequence = shufflingSequence + "R1" # Right 1 turn
elif turnDirection == 5:
shufflingSequence = shufflingSequence + "R2" # Right 2 turns
elif turnDirection == 6:
shufflingSequence = shufflingSequence + "R3" # Right 3 turns
elif turnDirection == 7:
shufflingSequence = shufflingSequence + "U1" # Up 1 turn
elif turnDirection == 8:
shufflingSequence = shufflingSequence + "U2" # Up 2 turns
elif turnDirection == 9:
shufflingSequence = shufflingSequence + "U3" # Up 3 turns
elif turnDirection == 10:
shufflingSequence = shufflingSequence + "D1" # Down 1 turn
elif turnDirection == 11:
shufflingSequence = shufflingSequence + "D2" # Down 2 turns
elif turnDirection == 12:
shufflingSequence = shufflingSequence + "D3" # Down 3 turns
elif turnDirection == 13:
shufflingSequence = shufflingSequence + "F1" # Front 1 turn
elif turnDirection == 14:
shufflingSequence = shufflingSequence + "F2" # Front 2 turns
elif turnDirection == 15:
shufflingSequence = shufflingSequence + "F3" # Front 3 turns
elif turnDirection == 16:
shufflingSequence = shufflingSequence + "B1" # Back 1 turn
elif turnDirection == 17:
shufflingSequence = shufflingSequence + "B2" # Back 2 turns
elif turnDirection == 18:
shufflingSequence = shufflingSequence + "B3" # Back 3 turns
# Uses the followSequence function to follow the randomly generated
# sequence.
self.followSequence(shufflingSequence)