forked from clementgallet/FiveStage444
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CubePack.java
393 lines (337 loc) · 10.7 KB
/
CubePack.java
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package cg.fivestage444;
import cg.fivestage444.Coordinates.Edge2;
import cg.fivestage444.Coordinates.Center2;
import cg.fivestage444.Coordinates.Edge3;
import cg.fivestage444.Coordinates.Center3;
import cg.fivestage444.Coordinates.Corner4;
import cg.fivestage444.Coordinates.Edge4;
import cg.fivestage444.Coordinates.Center4;
import cg.fivestage444.Coordinates.Corner5;
import cg.fivestage444.Coordinates.Edge5;
import cg.fivestage444.Coordinates.Center5;
import cg.fivestage444.Stages.Stage2;
import cg.fivestage444.Stages.Stage3;
import cg.fivestage444.Stages.Stage4;
import cg.fivestage444.Stages.Stage5;
import cg.fivestage444.Cubies.EdgeCubies;
import cg.fivestage444.Cubies.CenterCubies;
import cg.fivestage444.Cubies.CornerCubies;
import java.util.Arrays;
/* CubePack structure: a (almost) full representation of the cube using a set of coordinates.
* Some code and ideas are taken from cube20 */
final class CubePack{
/** Coordinates **/
/* Corners */
private byte corner_top_loc; // Location of the top four corners (70).
private byte corner_top_perm;
private byte corner_bottom_perm; // Permutation of top and bottom corners (24).
/* Centers */
private final short[] centers = new short[6]; // Location of each center (10626).
private final short[] edges_loc = new short[6]; // Location of each group of 4 edges (10626).
private final byte[] edges_perm = new byte[6]; // Permutation of each group of 4 edges (24).
/** Tables **/
private static final int N_ROT = 3;
private static final int ROTATE_U = Moves.N_STAGE_MOVES + 0;
private static final int ROTATE_UR3 = Moves.N_STAGE_MOVES + 1;
private static final int ROTATE_RU3 = Moves.N_STAGE_MOVES + 2;
private static final int[] rotations = {8, 16, 32};
private static final int[][] move_cperm = new int[Util.C8_4][Moves.N_STAGE_MOVES+N_ROT];
private static final int[][] move_edges = new int[Util.C24_4][Moves.N_STAGE_MOVES+N_ROT];
private static final short[][] move_centers = new short[Util.C24_4][Moves.N_STAGE_MOVES+N_ROT];
/** Packing and Unpacking **/
void pack( CubeState cube ){
packCorners( cube.corners );
for( int i=0; i<6; i++){
packCenters( cube.centers, i );
packEdges( cube.edges, i );
}
}
void packCorners( CornerCubies cube ){
int ctl = 0;
byte[] ctp = new byte[4];
byte[] cbp = new byte[4];
int itp = 3;
int ibp = 3;
int r = 4;
for (int i=7; i>=0; i--) {
int perm = cube.cubies[i] & 0x7;
if (( perm & 0x4 ) != 0) { // bottom layer
cbp[ibp--] = (byte)( perm & 0x3 );
} else {
ctl += Util.Cnk[i][r--];
ctp[itp--] = (byte)( perm & 0x3 );
}
}
corner_top_loc = (byte) ctl;
corner_top_perm = (byte) Util.get4Perm(ctp, 0);
corner_bottom_perm = (byte) Util.get4Perm(cbp, 0);
}
void unpackCorners( CornerCubies cube ){
int c8_4_bits = corner_top_loc;
int r = 4;
byte[] ct_perm = new byte[4];
byte[] cb_perm = new byte[4];
int itp = 3;
int ibp = 3;
Util.set4Perm(ct_perm, corner_top_perm);
Util.set4Perm(cb_perm, corner_bottom_perm);
for (int i = 7; i >=0; i--)
if ( c8_4_bits >= Util.Cnk[i][r] ) { // top layer
c8_4_bits -= Util.Cnk[i][r--];
cube.cubies[i] = ct_perm[itp--];
} else {
cube.cubies[i] = (byte)( cb_perm[ibp--] + 4 );
}
}
void packCenters (CenterCubies cube, int c_idx){
this.centers[c_idx] = 0;
int r = 4;
for (int i=23; i>=0; i--) {
if (cube.cubies[i] == c_idx) {
this.centers[c_idx] += Util.Cnk[i][r--];
}
}
}
void unpackCenters (CenterCubies cube, int c_idx){
int center = this.centers[c_idx];
int r = 4;
for (int i=23; i>=0; i--) {
if (center >= Util.Cnk[i][r]) {
center -= Util.Cnk[i][r--];
cube.cubies[i] = (byte) c_idx;
}
else
cube.cubies[i] = -1;
}
}
void packEdges( EdgeCubies cube, int e_idx ){
int r = 4;
this.edges_loc[e_idx] = 0;
byte[] t = new byte[4];
int it = 3;
for (int i=23; i>=0; i--) {
if (( cube.cubies[i] / 4 ) == e_idx ){
this.edges_loc[e_idx] += Util.Cnk[i][r--];
t[it--] = (byte)( cube.cubies[i] & 0x3 );
}
}
this.edges_perm[e_idx] = (byte) Util.get4Perm( t, 0 );
}
void unpackEdges( EdgeCubies cube, int e_idx ){
byte[] t = new byte[4];
Util.set4Perm( t, this.edges_perm[e_idx] );
int it = 3;
int loc = this.edges_loc[e_idx];
int r = 4;
for (int i=23; i>=0; i--)
if (loc >= Util.Cnk[i][r]) {
loc -= Util.Cnk[i][r--];
cube.cubies[i] = (byte)( 4*e_idx + t[it--] );
}
else
cube.cubies[i] = -1;
}
/** Init tables **/
static void init(){
init_moveCorners();
init_moveEdges();
init_moveCenters();
}
private static void init_moveCorners(){
CubePack cp1 = new CubePack();
CubePack cp2 = new CubePack();
CornerCubies cube1 = new CornerCubies();
CornerCubies cube2 = new CornerCubies();
for (int i=0; i<Util.C8_4; i++) {
cp1.corner_top_loc = (byte)i;
cp1.unpackCorners( cube1 );
for (int mv=0; mv<Moves.N_STAGE_MOVES + N_ROT; mv++) {
if( mv >= Moves.N_STAGE_MOVES)
cube1.rightMult( rotations[mv - Moves.N_STAGE_MOVES], cube2 );
else
cube1.move( Moves.stage2moves[mv], cube2 );
cp2.packCorners( cube2 );
move_cperm[i][mv] = (cp2.corner_top_loc << 10) + (cp2.corner_top_perm << 5) + cp2.corner_bottom_perm;
}
}
}
private static void init_moveEdges(){
CubePack cp1 = new CubePack();
CubePack cp2 = new CubePack();
EdgeCubies cube1 = new EdgeCubies();
EdgeCubies cube2 = new EdgeCubies();
for (int i=0; i<Util.C24_4; i++) {
cp1.edges_loc[2] = (short)i;
cp1.unpackEdges( cube1, 2 );
for (int mv=0; mv<Moves.N_STAGE_MOVES + N_ROT; mv++) {
if( mv >= Moves.N_STAGE_MOVES)
cube1.rightMult( rotations[mv - Moves.N_STAGE_MOVES], cube2 );
else
cube1.move( Moves.stage2moves[mv], cube2 );
cp2.packEdges( cube2, 2 );
move_edges[i][mv] = (cp2.edges_loc[2] << 5) + cp2.edges_perm[2];
}
}
}
private static void init_moveCenters(){
CubePack cp1 = new CubePack();
CubePack cp2 = new CubePack();
CenterCubies cube1 = new CenterCubies();
CenterCubies cube2 = new CenterCubies();
for (int i=0; i<Util.C24_4; i++) {
cp1.centers[3] = (short)i;
cp1.unpackCenters( cube1, 3 );
for (int mv=0; mv<Moves.N_STAGE_MOVES + N_ROT; mv++) {
if( mv >= Moves.N_STAGE_MOVES)
cube1.rightMult( rotations[mv - Moves.N_STAGE_MOVES], cube2 );
else
cube1.move( Moves.stage2moves[mv], cube2 );
cp2.packCenters( cube2, 3 );
move_centers[i][mv] = cp2.centers[3];
}
}
}
/** Convert to Coordinates **/
void toEdge2( Edge2 e ){
e.coord = 6 * Util.C24to8[edges_loc[4]] + Util.perms_to_6[edges_perm[4]][edges_perm[5]];
}
void toCenter2( Center2 c, int c_idx ){
c.raw_coord = centers[c_idx];
c.computeSym();
}
public void toStage2( Stage2 s ){
toEdge2( s.edge );
toCenter2( s.centerF, 4 );
toCenter2( s.centerB, 5 );
}
void toCenter3( Center3 c ){
if( centers[2] > centers[3] )
c.raw_coord = Util.C16to16[centers[2]][centers[3]];
else
c.raw_coord = Util.C16to16[centers[3]][centers[2]];
c.computeSym();
}
void toEdge3( Edge3 e ){
if( edges_loc[0] > edges_loc[3] )
e.coord = Util.C16to16[edges_loc[0]][edges_loc[3]]/35;
else
e.coord = Util.C16to16[edges_loc[3]][edges_loc[0]]/35;
}
public void toStage3( Stage3 s ){
toEdge3( s.edge );
toCenter3( s.center );
/* Parity. Yes, it is not simple... */
boolean parity = false;
for( int i=0; i<4; i++ )
parity ^= Util.get1bit( Util.parity_s4, edges_perm[i] );
if( edges_loc[0] < edges_loc[1] ){
parity ^= Util.get1bit( Util.parityC16_4, edges_loc[1] * Util.C15_4 + edges_loc[0] );
parity ^= Util.get1bit( Util.parityC16_8, edges_loc[1] * Util.C15_4 + edges_loc[0] );
}
else {
parity ^= Util.get1bit( Util.paritySwapC16_4, edges_loc[0] * Util.C15_4 + edges_loc[1] );
parity ^= Util.get1bit( Util.parityC16_8, edges_loc[0] * Util.C15_4 + edges_loc[1] );
}
if( edges_loc[2] < edges_loc[3] )
parity ^= Util.get1bit( Util.parityC16_4, edges_loc[3] * Util.C15_4 + edges_loc[2] );
else
parity ^= Util.get1bit( Util.paritySwapC16_4, edges_loc[2] * Util.C15_4 + edges_loc[3] );
s.parity = (byte)( parity ? 1 : 0 );
}
void toCorner4(Corner4 c){
c.coord = 6 * corner_top_loc + Util.perms_to_6[corner_top_perm][corner_bottom_perm];
}
void toCenter4(Center4 c){
if( centers[0] < centers[1] )
c.coord = centers[0];
else
c.coord = centers[1];
}
void toEdge4(Edge4 e){
e.raw_coord = (( Util.perms_to_6[edges_perm[0]][edges_perm[1]] * 6 +
Util.perms_to_6[edges_perm[2]][edges_perm[3]] ) * 70 +
Util.shiftC16[edges_loc[0]] ) * 70 +
Util.shiftC16[edges_loc[1]];
e.computeSym();
}
public void toStage4( Stage4 s ){
toEdge4( s.edge );
toCenter4( s.center );
toCorner4( s.corner );
}
void toEdge5(Edge5 e){
e.raw_coord = (( 4 * edges_perm[4] + edges_perm[5] / 6 ) * 96 +
4 * edges_perm[2] + edges_perm[3] / 6 ) * 96 +
4 * edges_perm[0] + edges_perm[1] / 6;
e.computeSym();
}
void toCorner5(Corner5 c){
c.coord = 4 * corner_top_perm + corner_bottom_perm / 6;
}
void toCenter5(Center5 c){
c.coord = ( Util.C24_4to12[centers[5]] * 12 + Util.C24_4to12[centers[3]] ) * 12 + Util.C24_4to12[centers[1]];
}
public void toStage5( Stage5 s ){
toEdge5( s.edge );
toCenter5( s.center );
toCorner5( s.corner );
}
/** Move functions **/
public final void moveTo(int m, CubePack cp){
moveCorners(m, cp);
for (int i=0; i<6; i++){
moveEdges(m, i, cp);
moveCenters(m, i, cp);
}
}
final void moveCorners(int m, CubePack cp){
int t = move_cperm[corner_top_loc][m];
cp.corner_top_loc = (byte)(t >> 10);
cp.corner_top_perm = Util.s4mul[corner_top_perm][(t >> 5) & 31];
cp.corner_bottom_perm = Util.s4mul[corner_bottom_perm][t & 31];
}
final void moveEdges(int m, int e_idx, CubePack cp){
int t = move_edges[this.edges_loc[e_idx]][m];
cp.edges_loc[e_idx] = (short)(t >> 5);
cp.edges_perm[e_idx] = Util.s4mul[this.edges_perm[e_idx]][t & 31];
}
final void moveCenters(int m, int c_idx, CubePack cp){
cp.centers[c_idx] = move_centers[this.centers[c_idx]][m];
}
/** Orientation **/
public final int rotateStage2(){
if( edges_loc[5] > Util.Cnk[17][4] )
return 0;
if( edges_loc[5] > Util.Cnk[9][4] ){
moveTo( ROTATE_RU3, this );
return 32;
}
moveTo( ROTATE_UR3, this );
return 16;
}
public final int rotateStage3(){
if( centers[5] > Util.Cnk[17][4] )
return 0;
moveTo( ROTATE_U, this );
return 8;
}
/** Debug **/
public void print(){
System.out.println("c_top_loc:"+corner_top_loc+" - c_top_perm:"+corner_top_perm+" - c_bot_perm:"+corner_bottom_perm);
System.out.print("centers: ");
for (int i=0; i<6; i++){
System.out.print(centers[i]+" - ");
}
System.out.println();
System.out.print("edges_loc: ");
for (int i=0; i<6; i++){
System.out.print(edges_loc[i]+" - ");
}
System.out.println();
System.out.print("edges_perm: ");
for (int i=0; i<6; i++){
System.out.print(edges_perm[i]+" - ");
}
System.out.println();
}
}