-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcube.py
254 lines (223 loc) · 10.7 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
import copy
import random
from termcolor import colored
from colorama import init
# colorama init
init()
class Cube:
def __init__(self, size=3):
self.size = size
self.cube = self.generate(size)
@staticmethod
def generate(size: int) -> list:
"""
Generates cube with specific size.
:param size: Size of the cube.
:return: Cube list.
"""
cube = list()
for i in range(size):
layer = []
for j in range(size):
row = []
for k in range(size):
cubie = [1, 2, 3, 4, 5, 6]
#INSIDE
if 0 < i < size - 1 and 0 < j < size - 1 and 0 < k < size - 1:
cubie = [0 for _ in range(6)]
#CORNERS
#up
elif i == 0 and j == 0 and k == 0:
cubie[2], cubie[3], cubie[4] = 0, 0, 0
elif i == 0 and j == 0 and k == size - 1:
cubie[0], cubie[2], cubie[4] = 0, 0, 0
elif i == 0 and j == size - 1 and k == 0:
cubie[1], cubie[2], cubie[3] = 0, 0, 0
elif i == 0 and j == size - 1 and k == size - 1:
cubie[0], cubie[1], cubie[2] = 0, 0, 0
#down
elif i == size - 1 and j == 0 and k == 0:
cubie[3], cubie[4], cubie[5] = 0, 0, 0
elif i == size - 1 and j == 0 and k == size - 1:
cubie[0], cubie[4], cubie[5] = 0, 0, 0
elif i == size - 1 and j == size - 1 and k == 0:
cubie[1], cubie[3], cubie[5] = 0, 0, 0
elif i == size - 1 and j == size - 1 and k == size - 1:
cubie[0], cubie[1], cubie[5] = 0, 0, 0
#EDGES
#up
elif i == 0 and j == 0 and 0 < k < size - 1:
cubie[0], cubie[2], cubie[3], cubie[4] = 0, 0, 0, 0
elif i == 0 and j == size - 1 and 0 < k < size - 1:
cubie[0], cubie[1], cubie[2], cubie[3] = 0, 0, 0, 0
elif i == 0 and 0 < j < size - 1 and k == 0:
cubie[1], cubie[2], cubie[3], cubie[4] = 0, 0, 0, 0
elif i == 0 and 0 < j < size - 1 and k == size - 1:
cubie[0], cubie[1], cubie[2], cubie[4] = 0, 0, 0, 0
#middle
elif 0 < i < size - 1 and j == 0 and k == 0:
cubie[2], cubie[3], cubie[4], cubie[5] = 0, 0, 0, 0
elif 0 < i < size -1 and j == 0 and k == size - 1:
cubie[0], cubie[2], cubie[4], cubie[5] = 0, 0, 0, 0
elif 0 < i < size - 1 and j == size - 1 and k == 0:
cubie[1], cubie[2], cubie[3], cubie[5] = 0, 0, 0, 0
elif 0 < i < size -1 and j == size - 1 and k == size - 1:
cubie[0], cubie[1], cubie[2], cubie[5] = 0, 0, 0, 0
#down
elif i == size - 1 and j == 0 and 0 < k < size - 1:
cubie[0], cubie[3], cubie[4], cubie[5] = 0, 0, 0, 0
elif i == size - 1 and j == size - 1 and 0 < k < size - 1:
cubie[0], cubie[1], cubie[3], cubie[5] = 0, 0, 0, 0
elif i == size - 1 and 0 < j < size - 1 and k == 0:
cubie[1], cubie[3], cubie[4], cubie[5] = 0, 0, 0, 0
elif i == size - 1 and 0 < j < size - 1 and k == size - 1:
cubie[0], cubie[1], cubie[4], cubie[5] = 0, 0, 0, 0
#CENTERS
#up
elif i == 0 and 0 < j < size - 1 and 0 < k < size - 1:
cubie[0], cubie[1], cubie[2], cubie[3], cubie[4] = 0, 0, 0, 0, 0
#middle
elif 0 < i < size - 1 and 0 < j < size - 1 and k == 0:
cubie[1], cubie[2], cubie[3], cubie[4], cubie[5] = 0, 0, 0, 0, 0
elif 0 < i < size - 1 and 0 < j < size - 1 and k == size - 1:
cubie[0], cubie[1], cubie[2], cubie[4], cubie[5] = 0, 0, 0, 0, 0
elif 0 < i < size - 1 and j == 0 and 0 < k < size - 1:
cubie[0], cubie[2], cubie[3], cubie[4], cubie[5] = 0, 0, 0, 0, 0
elif 0 < i < size - 1 and j == size - 1 and 0 < k < size - 1:
cubie[0], cubie[1], cubie[2], cubie[3], cubie[5] = 0, 0, 0, 0, 0
# down
elif i == size - 1 and 0 < j < size - 1 and 0 < k < size - 1:
cubie[0], cubie[1], cubie[3], cubie[4], cubie[5] = 0, 0, 0, 0, 0
row.append(cubie)
layer.append(row)
cube.append(layer)
return cube
def move(self, axis: str, layer: int, direction: int, qty: int = 1, show: bool = False) -> None:
"""
Moves cube in specific axis, layer and direction.
:param axis: X / Y / Z.
:param layer: int 1-self.size.
:param direction: 0: Clockwise / 1: counterclockwise.
:param qty: Quantity of moves.
:param show: True: Display cube after move.
:return: None
"""
for _ in range(qty):
buffer = copy.deepcopy(self.cube)
if axis == 'Y':
for i, p in enumerate(buffer[layer]):
for j, e in enumerate(p):
if direction == 0:
e[0], e[1], e[3], e[4] = e[4], e[0], e[1], e[3]
self.cube[layer][j][self.size - 1 - i] = e
elif direction == 1:
e[4], e[0], e[1], e[3] = e[0], e[1], e[3], e[4]
self.cube[layer][self.size - 1 - j][i] = e
elif axis == 'X':
for i, p in enumerate(buffer):
for j, e in enumerate(p):
e = e[layer]
if direction == 0:
e[1], e[2], e[4], e[5] = e[2], e[4], e[5], e[1]
self.cube[j][self.size - 1 - i][layer] = e
elif direction == 1:
e[2], e[4], e[5], e[1] = e[1], e[2], e[4], e[5]
self.cube[self.size - 1 - j][i][layer] = e
elif axis == 'Z':
for i, p in enumerate(buffer):
for j, e in enumerate(p[layer]):
if direction == 0:
e[0], e[2], e[3], e[5] = e[2], e[3], e[5], e[0]
self.cube[j][layer][self.size - 1 - i] = e
elif direction == 1:
e[2], e[3], e[5], e[0] = e[0], e[2], e[3], e[5]
self.cube[self.size - 1 - j][layer][i] = e
if show:
self.show()
def show(self) -> None:
"""
Displays cube in console.
:return: None
"""
colors = [None, 'blue', 'red', 'white', 'green', 'magenta', 'yellow']
left_side = [[i[0][0] for i in n] for n in self.cube]
front_side = [[i[1] for i in n[0]] for n in self.cube]
down_side = [[i[2] for i in n] for n in self.cube[self.size - 1]]
right_side = [[i[self.size - 1][3] for i in n] for n in self.cube]
up_side = [[i[5] for i in n] for n in self.cube[0]]
back_side = [[i[4] for i in n[self.size - 1]] for n in self.cube]
sign = '■'
cubie_separator = ' '
tab = ' ' * (2 * self.size)
for up_row in reversed(up_side):
args_up = [[sign, colors[color]] for color in up_row]
self.print_row(tab, *args_up, separator=cubie_separator)
for left_row, front__row, right_row, back_row in zip(left_side, front_side, right_side, back_side):
args_left = [[sign, colors[color]] for color in reversed(left_row)]
args_front = [[sign, colors[color]] for color in front__row]
args_right = [[sign, colors[color]] for color in right_row]
args_back = [[sign, colors[color]] for color in reversed(back_row)]
self.print_row(*args_left, '', *args_front, '', *args_right, '', *args_back, separator=cubie_separator)
for down_row in down_side:
args_down = [[sign, colors[color]] for color in down_row]
self.print_row(tab, *args_down, separator=cubie_separator)
@property
def is_solved(self) -> bool:
"""
Returns if cube is solved or not.
:return: True: cube is solved, False: cube is not solved.
"""
solved = True
counter = [0, 0, 0, 0, 0, 0]
grid = [0, 0, 0, 0, 0, 0]
for i in self.cube:
for j in i:
for k in j:
for l, m in enumerate(k):
if m != 0:
if grid[l] == 0:
grid[l] = m
counter[l] += 1
elif grid[l] == m:
counter[l] += 1
else:
solved = False
return solved
def random_moves(self, num: int, show: bool = False) -> list:
"""
Executes specified number of random moves on cube.
:param num: Number of moves to execute.
:param show: True: display cube in console every move.
:return: List of executed moves.
"""
moves = []
for _ in range(num):
move = [random.choice(['X', 'Y', 'Z']), random.randint(0, self.size - 1), random.randint(0, 1)]
self.move(*move)
if show:
self.show()
moves.append(move)
return moves
@staticmethod
def print_row(*args, **kwargs) -> None:
args_ = []
separator = ''
color = None
if 'separator' in kwargs:
separator = kwargs['separator']
if 'color' in kwargs:
color = kwargs['color']
for i, arg in enumerate(args):
if i == len(args) - 1:
separator = ''
if hasattr(arg, '__len__') and type(arg) != str:
if len(arg) > 1 and type(arg) != str:
args_.append([f'{arg[0]}{separator}', arg[1]])
elif len(arg) > 0 and type(arg) != str:
args_.append([f'{arg[0]}{separator}', color])
else:
args_.append([f'{arg}{separator}', color])
rv = ''
for arg in args_:
rv += colored((str(arg[0])), arg[1])
print(rv)