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
|
#define _move(M, c) compose(c, _move_cube_ ## M)
#define _premove(M, c) compose(_move_cube_ ## M, c)
_static cube_t solvedcube(void);
_static bool isconsistent(cube_t);
_static bool issolvable(cube_t);
_static bool issolved(cube_t);
_static bool iserror(cube_t);
_static cube_t applymoves(cube_t, const char *);
_static cube_t applytrans(cube_t, const char *);
_static cube_t frommoves(const char *);
_static int permsign(uint8_t *, int);
_static cube_t move(cube_t, uint8_t);
_static cube_t transform_edges(cube_t, uint8_t);
_static cube_t transform_corners(cube_t, uint8_t);
_static cube_t transform(cube_t, uint8_t);
_static cube_t
solvedcube(void)
{
return solved;
}
_static bool
isconsistent(cube_t cube)
{
uint8_t i, p, e, piece, corner[8], edge[12];
bool found[12];
pieces(&cube, corner, edge);
for (i = 0; i < 12; i++)
found[i] = false;
for (i = 0; i < 12; i++) {
piece = edge[i];
p = piece & _pbits;
e = piece & _eobit;
if (p >= 12)
goto inconsistent_ep;
if (e != 0 && e != _eobit)
goto inconsistent_eo;
found[p] = true;
}
for (i = 0; i < 12; i++)
if (!found[i])
goto inconsistent_ep;
for (i = 0; i < 8; i++)
found[i] = false;
for (i = 0; i < 8; i++) {
piece = corner[i];
p = piece & _pbits;
e = piece & _cobits;
if (p >= 8)
goto inconsistent_cp;
if (e != 0 && e != _ctwist_cw && e != _ctwist_ccw)
goto inconsistent_co;
found[p] = true;
}
for (i = 0; i < 8; i++)
if (!found[i])
goto inconsistent_co;
return true;
inconsistent_ep:
_log("Inconsistent EP\n");
return false;
inconsistent_cp:
_log("Inconsistent CP\n");
return false;
inconsistent_eo:
_log("Inconsistent EO\n");
return false;
inconsistent_co:
_log("Inconsistent CO\n");
return false;
}
_static bool
issolvable(cube_t cube)
{
uint8_t i, eo, co, piece, edge[12], corner[8], ep[12], cp[8];
DBG_ASSERT(isconsistent(cube), false,
"issolvable: cube is inconsistent\n");
pieces(&cube, corner, edge);
for (i = 0; i < 12; i++)
ep[i] = edge[i] & _pbits;
for (i = 0; i < 8; i++)
cp[i] = corner[i] & _pbits;
if (permsign(ep, 12) != permsign(cp, 8))
goto issolvable_parity;
eo = 0;
for (i = 0; i < 12; i++) {
piece = edge[i];
eo += (piece & _eobit) >> _eoshift;
}
if (eo % 2 != 0)
goto issolvable_eo;
co = 0;
for (i = 0; i < 8; i++) {
piece = corner[i];
co += (piece & _cobits) >> _coshift;
}
if (co % 3 != 0)
goto issolvable_co;
return true;
issolvable_parity:
_log("EP and CP parities are different\n");
return false;
issolvable_eo:
_log("Odd number of flipped edges\n");
return false;
issolvable_co:
_log("Sum of corner orientation is not multiple of 3\n");
return false;
}
bool
issolved(cube_t cube)
{
return equal(cube, solved);
}
bool
iserror(cube_t cube)
{
return equal(cube, zero);
}
_static cube_t
applymoves(cube_t cube, const char *buf)
{
uint8_t r, m;
const char *b;
DBG_ASSERT(isconsistent(cube), zero,
"move error: inconsistent cube\n");
for (b = buf; *b != '\0'; b++) {
while (*b == ' ' || *b == '\t' || *b == '\n')
b++;
if (*b == '\0')
goto applymoves_finish;
if ((r = readmove(*b)) == _error)
goto applymoves_error;
if ((m = readmodifier(*(b+1))) != 0)
b++;
cube = move(cube, r + m);
}
applymoves_finish:
return cube;
applymoves_error:
_log("applymoves error\n");
return zero;
}
_static cube_t
frommoves(const char *buf)
{
return applymoves(solved, buf);
}
_static cube_t
applytrans(cube_t cube, const char *buf)
{
uint8_t t;
DBG_ASSERT(isconsistent(cube), zero,
"transformation error: inconsistent cube\n");
t = readtrans(buf);
return transform(cube, t);
}
_static int
permsign(uint8_t *a, int n)
{
int i, j;
uint8_t ret = 0;
for (i = 0; i < n; i++)
for (j = i+1; j < n; j++)
ret += a[i] > a[j] ? 1 : 0;
return ret % 2;
}
_static cube_t
move(cube_t c, uint8_t m)
{
switch (m) {
case _move_U:
return _move(U, c);
case _move_U2:
return _move(U2, c);
case _move_U3:
return _move(U3, c);
case _move_D:
return _move(D, c);
case _move_D2:
return _move(D2, c);
case _move_D3:
return _move(D3, c);
case _move_R:
return _move(R, c);
case _move_R2:
return _move(R2, c);
case _move_R3:
return _move(R3, c);
case _move_L:
return _move(L, c);
case _move_L2:
return _move(L2, c);
case _move_L3:
return _move(L3, c);
case _move_F:
return _move(F, c);
case _move_F2:
return _move(F2, c);
case _move_F3:
return _move(F3, c);
case _move_B:
return _move(B, c);
case _move_B2:
return _move(B2, c);
case _move_B3:
return _move(B3, c);
default:
_log("move error, unknown move\n");
return zero;
}
}
/*
TODO transform is now relegated to a separated file because it is too long.
It would be nice to make it shorter without loosing performance.
*/
|