1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2008 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Magic Cube. //
|
5
|
// //
|
6
|
// Magic Cube is free software: you can redistribute it and/or modify //
|
7
|
// it under the terms of the GNU General Public License as published by //
|
8
|
// the Free Software Foundation, either version 2 of the License, or //
|
9
|
// (at your option) any later version. //
|
10
|
// //
|
11
|
// Magic Cube is distributed in the hope that it will be useful, //
|
12
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14
|
// GNU General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU General Public License //
|
17
|
// along with Magic Cube. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
package org.distorted.solvers.cube3;
|
21
|
|
22
|
import java.io.InputStream;
|
23
|
import android.content.res.Resources;
|
24
|
|
25
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
26
|
|
27
|
class SolverCoordCube
|
28
|
{
|
29
|
static final short N_TWIST = 2187; // 3^7 possible corner orientations
|
30
|
static final short N_FLIP = 2048; // 2^11 possible edge flips
|
31
|
static final short N_SLICE1 = 495; // 12 choose 4 possible positions of FR,FL,BL,BR edges
|
32
|
static final short N_SLICE2 = 24; // 4! permutations of FR,FL,BL,BR edges in phase2
|
33
|
static final short N_PARITY = 2; // 2 possible corner parities
|
34
|
static final short N_URFtoDLF = 20160; // 8!/(8-6)! permutation of URF,UFL,ULB,UBR,DFR,DLF corners
|
35
|
static final short N_FRtoBR = 11880; // 12!/(12-4)! permutation of FR,FL,BL,BR edges
|
36
|
static final short N_URtoUL = 1320; // 12!/(12-3)! permutation of UR,UF,UL edges
|
37
|
static final short N_UBtoDF = 1320; // 12!/(12-3)! permutation of UB,DR,DF edges
|
38
|
static final short N_URtoDF = 20160; // 8!/(8-6)! permutation of UR,UF,UL,UB,DR,DF edges in phase2
|
39
|
static final short N_MOVE = 18;
|
40
|
|
41
|
// All coordinates are 0 for a solved cube except for UBtoDF, which is 114
|
42
|
short twist;
|
43
|
short flip;
|
44
|
short parity;
|
45
|
short FRtoBR;
|
46
|
short URFtoDLF;
|
47
|
short URtoUL;
|
48
|
short UBtoDF;
|
49
|
int URtoDF;
|
50
|
|
51
|
private static final boolean[] init = new boolean[12];
|
52
|
|
53
|
static
|
54
|
{
|
55
|
for(int i=0; i<12; i++ ) init[i] = false;
|
56
|
}
|
57
|
|
58
|
// Phase 1 move tables
|
59
|
// Move table for the twists of the corners. twist < 2187 in phase 2; twist = 0 in phase 2.
|
60
|
private static final byte[] twistMove = new byte[2*N_TWIST*N_MOVE];
|
61
|
|
62
|
// Move table for the flips of the edges. flip < 2048 in phase 1; flip = 0 in phase 2.
|
63
|
private static final byte[] flipMove = new byte[2*N_FLIP*N_MOVE];
|
64
|
|
65
|
// Parity of the corner permutation. This is the same as the parity for the edge permutation of a valid cube.
|
66
|
// parity has values 0 and 1
|
67
|
static short[][] parityMove = {
|
68
|
{ 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1 },
|
69
|
{ 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0 }
|
70
|
};
|
71
|
|
72
|
// Phase 1 and 2 movetable
|
73
|
// Move table for the four UD-slice edges FR, FL, Bl and BR
|
74
|
// FRtoBRMove < 11880 in phase 1; FRtoBRMove < 24 in phase 2; FRtoBRMove = 0 for solved cube
|
75
|
private static final byte[] FRtoBR_Move = new byte[2*N_FRtoBR*N_MOVE];
|
76
|
|
77
|
// Phase 1 and 2 movetable
|
78
|
// Move table for permutation of six corners. The positions of the DBL and DRB corners are determined by the parity.
|
79
|
// URFtoDLF < 20160 in phase 1; URFtoDLF < 20160 in phase 2; URFtoDLF = 0 for solved cube.
|
80
|
private static final byte[] URFtoDLF_Move = new byte[2*N_URFtoDLF*N_MOVE];
|
81
|
|
82
|
// Move table for the permutation of six U-face and D-face edges in phase2. The positions of the DL and DB edges are
|
83
|
// determined by the parity.
|
84
|
// URtoDF < 665280 in phase 1; URtoDF < 20160 in phase 2; URtoDF = 0 for solved cube.
|
85
|
private static final byte[] URtoDF_Move = new byte[2*N_URtoDF*N_MOVE];
|
86
|
|
87
|
// Helper move tables to compute URtoDF for the beginning of phase2
|
88
|
// Move table for the three edges UR,UF and UL in phase1.
|
89
|
private static final byte[] URtoUL_Move = new byte[2*N_URtoUL*N_MOVE];
|
90
|
|
91
|
// Move table for the three edges UB,DR and DF in phase1.
|
92
|
private static final byte[] UBtoDF_Move = new byte[2*N_UBtoDF*N_MOVE];
|
93
|
|
94
|
// Table to merge the coordinates of the UR,UF,UL and UB,DR,DF edges at the beginning of phase2
|
95
|
private static final byte[] MergeURtoULandUBtoDF = new byte[2*336*336];
|
96
|
|
97
|
// Pruning tables for the search
|
98
|
// Pruning table for the permutation of the corners and the UD-slice edges in phase2.
|
99
|
// The pruning table entries give a lower estimation for the number of moves to reach the solved cube.
|
100
|
public static byte[] Slice_URFtoDLF_Parity_Prun = new byte[N_SLICE2*N_URFtoDLF*N_PARITY / 2];
|
101
|
|
102
|
// Pruning table for the permutation of the edges in phase2.
|
103
|
// The pruning table entries give a lower estimation for the number of moves to reach the solved cube.
|
104
|
public static byte[] Slice_URtoDF_Parity_Prun = new byte[N_SLICE2*N_URtoDF*N_PARITY / 2];
|
105
|
|
106
|
// Pruning table for the twist of the corners and the position (not permutation) of the UD-slice edges in phase1
|
107
|
// The pruning table entries give a lower estimation for the number of moves to reach the H-subgroup.
|
108
|
public static byte[] Slice_Twist_Prun = new byte[N_SLICE1*N_TWIST / 2 + 1];
|
109
|
|
110
|
// Pruning table for the flip of the edges and the position (not permutation) of the UD-slice edges in phase1
|
111
|
// The pruning table entries give a lower estimation for the number of moves to reach the H-subgroup.
|
112
|
public static byte[] Slice_Flip_Prun = new byte[N_SLICE1*N_FLIP / 2];
|
113
|
|
114
|
private static final int[] resourceTable = new int[]
|
115
|
{
|
116
|
org.distorted.main.R.raw.cube3solver_table1,
|
117
|
org.distorted.main.R.raw.cube3solver_table2,
|
118
|
org.distorted.main.R.raw.cube3solver_table3,
|
119
|
org.distorted.main.R.raw.cube3solver_table4,
|
120
|
org.distorted.main.R.raw.cube3solver_table5,
|
121
|
org.distorted.main.R.raw.cube3solver_table6,
|
122
|
org.distorted.main.R.raw.cube3solver_table7,
|
123
|
org.distorted.main.R.raw.cube3solver_table8,
|
124
|
org.distorted.main.R.raw.cube3solver_table9,
|
125
|
org.distorted.main.R.raw.cube3solver_table10,
|
126
|
org.distorted.main.R.raw.cube3solver_table11,
|
127
|
org.distorted.main.R.raw.cube3solver_table12
|
128
|
};
|
129
|
|
130
|
private static final byte[][] tableOfTables = new byte[][]
|
131
|
{
|
132
|
twistMove,
|
133
|
flipMove,
|
134
|
FRtoBR_Move,
|
135
|
URFtoDLF_Move,
|
136
|
URtoDF_Move,
|
137
|
URtoUL_Move,
|
138
|
UBtoDF_Move,
|
139
|
MergeURtoULandUBtoDF,
|
140
|
Slice_URFtoDLF_Parity_Prun,
|
141
|
Slice_URtoDF_Parity_Prun,
|
142
|
Slice_Twist_Prun,
|
143
|
Slice_Flip_Prun
|
144
|
};
|
145
|
|
146
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
147
|
// Generate a CoordCube from a CubieCube
|
148
|
|
149
|
SolverCoordCube(SolverCubieCube c)
|
150
|
{
|
151
|
twist = c.getTwist();
|
152
|
flip = c.getFlip();
|
153
|
parity = c.cornerParity();
|
154
|
FRtoBR = c.getFRtoBR();
|
155
|
URFtoDLF = c.getURFtoDLF();
|
156
|
URtoUL = c.getURtoUL();
|
157
|
UBtoDF = c.getUBtoDF();
|
158
|
URtoDF = c.getURtoDF();// only needed in phase2
|
159
|
}
|
160
|
|
161
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
162
|
|
163
|
static byte getPruning(byte[] table, int index)
|
164
|
{
|
165
|
byte ret = table[index/2];
|
166
|
return (byte) ( ((index&1)==0) ? (ret&0x0f) : ((ret&0xf0)>>>4) );
|
167
|
}
|
168
|
|
169
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
170
|
|
171
|
public static short getTwistMove(int a,int b)
|
172
|
{
|
173
|
return getTable(a,b,twistMove,N_MOVE);
|
174
|
}
|
175
|
|
176
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
177
|
|
178
|
public static short getFlipMove(int a,int b)
|
179
|
{
|
180
|
return getTable(a,b,flipMove,N_MOVE);
|
181
|
}
|
182
|
|
183
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
184
|
|
185
|
public static short getFRtoBR_Move(int a,int b)
|
186
|
{
|
187
|
return getTable(a,b,FRtoBR_Move,N_MOVE);
|
188
|
}
|
189
|
|
190
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
191
|
|
192
|
public static short getURFtoDLF_Move(int a,int b)
|
193
|
{
|
194
|
return getTable(a,b,URFtoDLF_Move,N_MOVE);
|
195
|
}
|
196
|
|
197
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
198
|
|
199
|
public static short getURtoDF_Move(int a,int b)
|
200
|
{
|
201
|
return getTable(a,b,URtoDF_Move,N_MOVE);
|
202
|
}
|
203
|
|
204
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
205
|
|
206
|
public static short getURtoUL_Move(int a,int b)
|
207
|
{
|
208
|
return getTable(a,b,URtoUL_Move,N_MOVE);
|
209
|
}
|
210
|
|
211
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
212
|
|
213
|
public static short getUBtoDF_Move(int a,int b)
|
214
|
{
|
215
|
return getTable(a,b,UBtoDF_Move,N_MOVE);
|
216
|
}
|
217
|
|
218
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
219
|
|
220
|
public static short getMergeURtoULandUBtoDF(int a,int b)
|
221
|
{
|
222
|
return getTable(a,b,MergeURtoULandUBtoDF,336);
|
223
|
}
|
224
|
|
225
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
226
|
|
227
|
private static short getTable(int a,int b, byte[] table, int size)
|
228
|
{
|
229
|
short upperS = table[2*(a*size+b)];
|
230
|
short lowerS = table[2*(a*size+b)+1];
|
231
|
|
232
|
if( upperS<0 ) upperS+=256;
|
233
|
if( lowerS<0 ) lowerS+=256;
|
234
|
|
235
|
return (short)( (upperS<<8) + lowerS );
|
236
|
}
|
237
|
|
238
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
239
|
|
240
|
public static void initialize(Resources res, int index)
|
241
|
{
|
242
|
if( !init[index] )
|
243
|
{
|
244
|
try
|
245
|
{
|
246
|
byte[] table = tableOfTables[index];
|
247
|
InputStream is = res.openRawResource(resourceTable[index]);
|
248
|
is.read( table, 0, table.length);
|
249
|
}
|
250
|
catch(Exception ioe)
|
251
|
{
|
252
|
System.out.println("error reading table"+index);
|
253
|
}
|
254
|
|
255
|
init[index]=true;
|
256
|
}
|
257
|
}
|
258
|
}
|