-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleMiniCube.py
More file actions
138 lines (109 loc) · 3.88 KB
/
SimpleMiniCube.py
File metadata and controls
138 lines (109 loc) · 3.88 KB
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
import copy
class SimpleMiniCube:
CLOCKWISE = False
COUNTER_CLOCKWISE = True
def __init__(self):
# 0 1 2 3 4 5
self._solved_state = [[' ',' ','R','R',' ',' '], #0
[' ',' ','R','R',' ',' '], #1
['B','B','W','W','G','G'], #2
['B','B','W','W','G','G'], #3
[' ',' ','O','O',' ',' '], #4
[' ',' ','O','O',' ',' '], #5
[' ',' ','Y','Y',' ',' '], #6
[' ',' ','Y','Y',' ',' ']] #7
self._solution = self.fullCubeString(self._solved_state)
self._state = copy.deepcopy(self._solved_state)
def __str__(self):
result = self.fullCubeString(self._state)
return result
def __repr__(self):
return self.__str__()
def fullCubeString(self, cube_data):
result = ""
for row in cube_data:
result += " ".join(row) + "\n\n"
result += "\n"
return result
def is_solved(self):
return self.fullCubeString(self._state) == self._solution
def right(self, is_counter_clockwise = False):
face_positions = ((2,4),(2,5),(3,5),(3,4))
self._transform(self._state, face_positions , is_counter_clockwise)
edge_positions1 = ((2,3),(0,3),(6,3),(4,3))
self._transform(self._state, edge_positions1, is_counter_clockwise)
edge_positions2 = ((3,3),(1,3),(7,3),(5,3))
self._transform(self._state, edge_positions2, is_counter_clockwise)
def back(self, is_counter_clockwise = False):
face_positions = ((6,2),(6,3),(7,3),(7,2))
self._transform(self._state, face_positions , is_counter_clockwise)
edge_positions1 = ((5,2),(3,5),(0,3),(2,0))
self._transform(self._state, edge_positions1, is_counter_clockwise)
edge_positions2 = ((5,3),(2,5),(0,2),(3,0))
self._transform(self._state, edge_positions2, is_counter_clockwise)
def down(self, is_counter_clockwise = False):
face_positions = ((4,2),(4,3),(5,3),(5,2))
self._transform(self._state, face_positions , is_counter_clockwise)
edge_positions1 = ((3,2),(3,4),(6,3),(3,0))
self._transform(self._state, edge_positions1, is_counter_clockwise)
edge_positions2 = ((3,3),(3,5),(6,2),(3,1))
self._transform(self._state, edge_positions2, is_counter_clockwise)
def _transform(self, cube_data, face_positions, is_counter_clockwise):
current_position = face_positions[0]
prev_row = current_position[0]
prev_col = current_position[1]
temp = cube_data[prev_row][prev_col]
n = len(face_positions)
i = 1 if is_counter_clockwise else len(face_positions) - 1
while i < n if is_counter_clockwise else 0 < i:
current_position = face_positions[i]
current_row = current_position[0]
current_col = current_position[1]
cube_data[prev_row][prev_col] = cube_data[current_row][current_col]
prev_row = current_row
prev_col = current_col
i = i + 1 if is_counter_clockwise else i - 1
cube_data[prev_row][prev_col] = temp
def reset(self):
self._state = copy.deepcopy(self._solved_state)
def main():
cube = SimpleMiniCube()
print("TESTING RIGHT CLOCKWISE")
print(cube)
for i in range(4):
cube.right(SimpleMiniCube.CLOCKWISE)
print(cube)
print("TESTING DOWN CLOCKWISE")
print(cube)
for i in range(4):
cube.down(SimpleMiniCube.CLOCKWISE)
print(cube)
print("TESTING BACK CLOCKWISE")
print(cube)
for i in range(4):
cube.back(SimpleMiniCube.CLOCKWISE)
print(cube)
print("TESTING RIGHT COUNTER_CLOCKWISE")
print(cube)
for i in range(4):
cube.right(SimpleMiniCube.COUNTER_CLOCKWISE)
print(cube)
print("TESTING DOWN COUNTER_CLOCKWISE")
print(cube)
for i in range(4):
cube.down(SimpleMiniCube.COUNTER_CLOCKWISE)
print(cube)
print("TESTING BACK COUNTER_CLOCKWISE")
print(cube)
for i in range(4):
cube.back(SimpleMiniCube.COUNTER_CLOCKWISE)
print(cube)
print("TESTING RESET")
print(cube)
cube.right(SimpleMiniCube.COUNTER_CLOCKWISE)
cube.right(SimpleMiniCube.COUNTER_CLOCKWISE)
print(cube)
cube.reset()
print(cube)
if __name__ == "__main__":
main()