-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcube.py
234 lines (199 loc) · 7.83 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
This module contains the Cube class.
It is used to represent a Rubik's Cube and supports manipulation of the cube.
Copyright (C) 2021-2024 Felix D.
Created by felix on 09.03.2021 (%d.%m.%Y)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""
import numpy as np
from copy import deepcopy
"""
List of all the possible moves
"""
POSSIBLE_MOVES = [f"{x}{y}" for x in "FBUDLR" for y in ["", "'", "2"]]
"""
Translate rubik's cube notation to method name and number of rotation
"""
TRANSLATE_MOVE = {
move: (
{"F": "front", "B": "back", "U": "up", "D": "down", "L": "left", "R": "right"}[move[0]],
(lambda x: ["", "2", "'"].index(x[1:]) + 1)(move),
)
for move in POSSIBLE_MOVES
}
"""
w = white (Top face)
r = red (Left face)
b = blue (Front face)
o = orange (Right face)
g = green (Back face)
y = yellow (Bottom face)
"""
COLOR_BOARD = [[[e] * 3 for _ in range(3)] for e in "wrbogy"]
"""
The middle Letter of each face represents the face's color (will not change).
A uppercase letter is a unique identifier for a corner piece.
A lowercase letter is a unique identifier for an edge piece.
"""
LETTER_BOARD = [
[["A", "a", "B"], ["d", "W", "b"], ["D", "c", "C"]], # TOP SIDE
[["E", "e", "F"], ["h", "R", "f"], ["H", "g", "G"]], # LEFT SIDE
[["I", "i", "J"], ["l", "B", "j"], ["L", "k", "K"]], # FRONT SIDE
[["M", "m", "N"], ["p", "O", "n"], ["P", "o", "O"]], # RIGHT SIDE
[["Q", "q", "R"], ["t", "G", "r"], ["T", "s", "S"]], # BACK SIDE
[["U", "u", "V"], ["x", "Y", "v"], ["X", "w", "W"]], # BOTTOM SIDE
]
class CubeObj:
"""
Representation of a Rubik's cube with the ability to rotate the faces.
"""
colour: bool
def __init__(self, color=True) -> None:
"""
Initialize the cube
:param color: if True, the cube will be colored, otherwise it will be represented by old-pochman letters
"""
self._board = COLOR_BOARD if color else LETTER_BOARD
def __str__(self) -> str:
return str(np.array(self._board))
def __eq__(self, other):
return hash(self) == hash(other)
def __hash__(self):
return hash(str(self))
def __copy__(self):
return deepcopy(self)
@property
def board(self) -> list:
return self._board
@board.setter
def board(self, input_board: list) -> None:
# TODO(all) check if the given board is valid
self._board = input_board
def translate(self, moves: list[str]) -> None:
"""
Translate a list of moves in method name and execute them on the cube.
:param moves: list of moves
:return: None
"""
for move in moves:
# check if the move is valid
if move not in POSSIBLE_MOVES:
raise ValueError(f"Move {move} is not a valid move.")
# translate the move into a function name and call it
method_name, count = TRANSLATE_MOVE[move]
for _ in range(count):
getattr(self, method_name)()
def front(self) -> None:
"""
Rotating the front side by 90 degrees clockwise. (blue side)
"""
# get the current state of the cube
white_row = self._board[0][2]
orange_row = [item[0] for item in self._board[3]]
yellow_row = self._board[5][0]
red_row = [item[2] for item in self._board[1]]
# update the cube
self._board[0][2] = red_row[::-1]
for index in range(3):
self._board[3][index][0] = white_row[index]
self._board[5][0] = orange_row[::-1]
for index in range(3):
self._board[1][index][2] = yellow_row[index]
self._board[2] = np.rot90(np.array(self._board[2]), 3).tolist()
def back(self) -> None:
"""
Rotation of the back side by 90 degrees clockwise. (green side)
"""
# get the current state of the cube
white_row = self._board[0][0][::-1]
orange_row = [item[2] for item in self._board[3]]
yellow_row = self._board[5][2][::-1]
red_row = [item[0] for item in self._board[1]]
# update the cube
self._board[0][0] = orange_row
for index in range(3):
self._board[3][index][2] = yellow_row[index]
self._board[5][2] = red_row
for index in range(3):
self._board[1][index][0] = white_row[index]
self._board[4] = np.rot90(np.array(self._board[4]), 1).tolist()
def right(self) -> None:
"""
Rotation of the right side by 90 degrees clockwise. (orange side)
"""
# get the current state of the cube
white_row = [item[2] for item in self._board[0]][::-1]
green_row = [item[2] for item in self._board[4]][::-1]
yellow_row = [item[2] for item in self._board[5]]
blue_row = [item[2] for item in self._board[2]]
# update the cube
for index in range(3):
self._board[0][index][2] = blue_row[index]
for index in range(3):
self._board[4][index][2] = white_row[index]
for index in range(3):
self._board[5][index][2] = green_row[index]
for index in range(3):
self._board[2][index][2] = yellow_row[index]
self._board[3] = np.rot90(np.array(self._board[3]), 3).tolist()
def left(self) -> None:
"""
Rotating the left side by 90 degrees clockwise. (red side)
"""
# get the current state of the cube
white_row = [item[0] for item in self._board[0]]
blue_row = [item[0] for item in self._board[2]]
yellow_row = [item[0] for item in self._board[5]][::-1]
green_row = [item[0] for item in self._board[4]][::-1]
# update the cube
for index in range(3):
self._board[0][index][0] = green_row[index]
for index in range(3):
self._board[2][index][0] = white_row[index]
for index in range(3):
self._board[5][index][0] = blue_row[index]
for index in range(3):
self._board[4][index][0] = yellow_row[index]
self._board[1] = np.rot90(np.array(self._board[1]), 3).tolist()
def up(self) -> None:
"""
Rotating the top side by 90 degrees clockwise. (white side)
"""
# get the current state of the cube
blue_row = self._board[2][0]
red_row = self._board[1][0]
green_row = self._board[4][0]
orange_row = self._board[3][0]
# update the cube
self._board[2][0] = orange_row
self._board[1][0] = blue_row
self._board[4][0] = red_row[::-1]
self._board[3][0] = green_row[::-1]
self._board[0] = np.rot90(np.array(self._board[0]), 3).tolist()
def down(self) -> None:
"""
Rotating the downside by 90 degrees clockwise. (yellow side)
"""
# get the current state of the cube
blue_row = self._board[2][2]
red_row = self._board[1][2]
green_row = self._board[4][2]
orange_row = self._board[3][2]
# update the cube
self._board[2][2] = red_row
self._board[3][2] = blue_row
self._board[4][2] = orange_row[::-1]
self._board[1][2] = green_row[::-1]
self._board[5] = np.rot90(np.array(self._board[5]), 3).tolist()