forked from hkociemba/RubiksCube-TwophaseSolver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
moves.py
210 lines (195 loc) · 9.35 KB
/
moves.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
# ################### Movetables describe the transformation of the coordinates by cube moves. #########################
from os import path
import array as ar
from . import cubie as cb
from . import enums
from .defs import N_TWIST, N_FLIP, N_SLICE_SORTED, N_CORNERS, N_UD_EDGES, N_MOVE
a = cb.CubieCube()
# ######################################### Move table for the twists of the corners. ##################################
# The twist coordinate describes the 3^7 = 2187 possible orientations of the 8 corners
# 0 <= twist < 2187 in phase 1, twist = 0 in phase 2
fname = "move_twist"
if not path.isfile(fname):
print("creating " + fname + " table...")
twist_move = ar.array('H', [0 for i in range(N_TWIST * N_MOVE)])
for i in range(N_TWIST):
a.set_twist(i)
for j in enums.Color: # six faces U, R, F, D, L, B
for k in range(3): # three moves for each face, for example U, U2, U3 = U'
a.corner_multiply(cb.basicMoveCube[j])
twist_move[N_MOVE * i + 3 * j + k] = a.get_twist()
a.corner_multiply(cb.basicMoveCube[j]) # 4. move restores face
fh = open(fname, "wb")
twist_move.tofile(fh)
else:
print("loading " + fname + " table...")
fh = open(fname, "rb")
twist_move = ar.array('H')
twist_move.fromfile(fh, N_TWIST * N_MOVE)
fh.close()
########################################################################################################################
# #################################### Move table for the flip of the edges. ##########################################
# The flip coordinate describes the 2^11 = 2048 possible orientations of the 12 edges
# 0 <= flip < 2048 in phase 1, flip = 0 in phase 2
fname = "move_flip"
if not path.isfile(fname):
print("creating " + fname + " table...")
flip_move = ar.array('H', [0 for i in range(N_FLIP * N_MOVE)])
for i in range(N_FLIP):
a.set_flip(i)
for j in enums.Color:
for k in range(3):
a.edge_multiply(cb.basicMoveCube[j])
flip_move[N_MOVE * i + 3 * j + k] = a.get_flip()
a.edge_multiply(cb.basicMoveCube[j])
fh = open(fname, "wb")
flip_move.tofile(fh)
else:
print("loading " + fname + " table...")
fh = open(fname, "rb")
flip_move = ar.array('H')
flip_move.fromfile(fh, N_FLIP * N_MOVE)
fh.close()
########################################################################################################################
# ###################### Move table for the four UD-slice edges FR, FL, Bl and BR. #####################################
# The slice_sorted coordinate describes the 12!/8! = 11880 possible positions of the FR, FL, BL and BR edges.
# Though for phase 1 only the "unsorted" slice coordinate with Binomial(12,4) = 495 positions is relevant, using the
# slice_sorted coordinate gives us the permutation of the FR, FL, BL and BR edges at the beginning of phase 2 for free.
# 0 <= slice_sorted < 11880 in phase 1, 0 <= slice_sorted < 24 in phase 2, slice_sorted = 0 for solved cube
fname = "move_slice_sorted"
if not path.isfile(fname):
print("creating " + fname + " table...")
slice_sorted_move = ar.array('H', [0 for i in range(N_SLICE_SORTED * N_MOVE)])
for i in range(N_SLICE_SORTED):
if i % 200 == 0:
print('.', end='', flush=True)
a.set_slice_sorted(i)
for j in enums.Color:
for k in range(3):
a.edge_multiply(cb.basicMoveCube[j])
slice_sorted_move[N_MOVE * i + 3 * j + k] = a.get_slice_sorted()
a.edge_multiply(cb.basicMoveCube[j])
fh = open(fname, "wb")
slice_sorted_move.tofile(fh)
print()
else:
print("loading " + fname + " table...")
fh = open(fname, "rb")
slice_sorted_move = ar.array('H')
slice_sorted_move.fromfile(fh, N_SLICE_SORTED * N_MOVE)
fh.close()
########################################################################################################################
# ################# Move table for the u_edges coordinate for transition phase 1 -> phase 2 ############################
# The u_edges coordinate describes the 12!/8! = 11880 possible positions of the UR, UF, UL and UB edges. It is needed at
# the end of phase 1 to set up the coordinates of phase 2
# 0 <= u_edges < 11880 in phase 1, 0 <= u_edges < 1680 in phase 2, u_edges = 1656 for solved cube."""
fname = "move_u_edges"
if not path.isfile(fname):
print("creating " + fname + " table...")
u_edges_move = ar.array('H', [0 for i in range(N_SLICE_SORTED * N_MOVE)])
for i in range(N_SLICE_SORTED):
if i % 200 == 0:
print('.', end='', flush=True)
a.set_u_edges(i)
for j in enums.Color:
for k in range(3):
a.edge_multiply(cb.basicMoveCube[j])
u_edges_move[N_MOVE * i + 3 * j + k] = a.get_u_edges()
a.edge_multiply(cb.basicMoveCube[j])
fh = open(fname, "wb")
u_edges_move.tofile(fh)
print()
else:
print("loading " + fname + " table...")
fh = open(fname, "rb")
u_edges_move = ar.array('H')
u_edges_move.fromfile(fh, N_SLICE_SORTED * N_MOVE)
fh.close()
########################################################################################################################
# ################# Move table for the d_edges coordinate for transition phase 1 -> phase 2 ############################
# The d_edges coordinate describes the 12!/8! = 11880 possible positions of the DR, DF, DL and DB edges. It is needed at
# the end of phase 1 to set up the coordinates of phase 2
# 0 <= d_edges < 11880 in phase 1, 0 <= d_edges < 1680 in phase 2, d_edges = 0 for solved cube.
fname = "move_d_edges"
if not path.isfile(fname):
print("creating " + fname + " table...")
d_edges_move = ar.array('H', [0 for i in range(N_SLICE_SORTED * N_MOVE)])
for i in range(N_SLICE_SORTED):
if i % 200 == 0:
print('.', end='', flush=True)
a.set_d_edges(i)
for j in enums.Color:
for k in range(3):
a.edge_multiply(cb.basicMoveCube[j])
d_edges_move[N_MOVE * i + 3 * j + k] = a.get_d_edges()
a.edge_multiply(cb.basicMoveCube[j])
fh = open(fname, "wb")
d_edges_move.tofile(fh)
print()
else:
print("loading " + fname + " table...")
fh = open(fname, "rb")
d_edges_move = ar.array('H')
d_edges_move.fromfile(fh, N_SLICE_SORTED * N_MOVE)
fh.close()
########################################################################################################################
# ######################### # Move table for the edges in the U-face and D-face. #######################################
# The ud_edges coordinate describes the 40320 permutations of the edges UR, UF, UL, UB, DR, DF, DL and DB in phase 2
# ud_edges undefined in phase 1, 0 <= ud_edges < 40320 in phase 2, ud_edges = 0 for solved cube.
fname = "move_ud_edges"
if not path.isfile(fname):
print("creating " + fname + " table...")
ud_edges_move = ar.array('H', [0 for i in range(N_UD_EDGES * N_MOVE)])
for i in range(N_UD_EDGES):
if (i+1) % 600 == 0:
print('.', end='', flush=True)
if (i+1) % 48000 == 0:
print('')
a.set_ud_edges(i)
for j in enums.Color:
for k in range(3):
a.edge_multiply(cb.basicMoveCube[j])
# only R2, F2, L2 and B2 in phase 2
if j in [enums.Color.R, enums.Color.F, enums.Color.L, enums.Color.B] and k != 1:
continue
ud_edges_move[N_MOVE * i + 3 * j + k] = a.get_ud_edges()
a.edge_multiply(cb.basicMoveCube[j])
fh = open(fname, "wb")
ud_edges_move.tofile(fh)
print()
else:
print("loading " + fname + " table...")
fh = open(fname, "rb")
ud_edges_move = ar.array('H')
ud_edges_move.fromfile(fh, N_UD_EDGES * N_MOVE)
fh.close()
########################################################################################################################
# ############################ Move table for the corners coordinate in phase 2 ########################################
# The corners coordinate describes the 8! = 40320 permutations of the corners.
# 0 <= corners < 40320 defined but unused in phase 1, 0 <= corners < 40320 in phase 2, corners = 0 for solved cube
fname = "move_corners"
if not path.isfile(fname):
print("creating " + fname + " table...")
corners_move = ar.array('H', [0 for i in range(N_CORNERS * N_MOVE)])
for i in range(N_CORNERS):
if (i+1) % 200 == 0:
print('.', end='', flush=True)
if(i+1) % 16000 == 0:
print('')
a.set_corners(i)
for j in enums.Color:
for k in range(3):
a.corner_multiply(cb.basicMoveCube[j])
corners_move[N_MOVE * i + 3 * j + k] = a.get_corners()
a.corner_multiply(cb.basicMoveCube[j])
fh = open(fname, "wb")
corners_move.tofile(fh)
fh.close()
print()
else:
print("loading " + fname + " table...")
fh = open(fname, "rb")
corners_move = ar.array('H')
corners_move.fromfile(fh, N_CORNERS * N_MOVE)
fh.close()
########################################################################################################################