Project

General

Profile

Download (8.62 KB) Statistics
| Branch: | Revision:

distorted-objectlib / src / main / java / org / distorted / objectlib / kociemba / SolverCoordCube.java @ 6777e712

1
/*
2
 * Herbert Kociemba
3
 *
4
 * BSD-licensed. See https://opensource.org/licenses/BSD-3-Clause
5
 */
6

    
7
package org.distorted.objectlib.kociemba;
8

    
9
import java.io.InputStream;
10

    
11
import org.distorted.objectlib.R;
12
import org.distorted.objectlib.helpers.OperatingSystemInterface;
13

    
14
///////////////////////////////////////////////////////////////////////////////////////////////////
15

    
16
class SolverCoordCube
17
{
18
	static final short N_TWIST    = 2187;  // 3^7 possible corner orientations
19
	static final short N_FLIP     = 2048;  // 2^11 possible edge flips
20
	static final short N_SLICE1   = 495;   // 12 choose 4 possible positions of FR,FL,BL,BR edges
21
	static final short N_SLICE2   = 24;    // 4! permutations of FR,FL,BL,BR edges in phase2
22
	static final short N_PARITY   = 2;     // 2 possible corner parities
23
	static final short N_URFtoDLF = 20160; // 8!/(8-6)! permutation of URF,UFL,ULB,UBR,DFR,DLF corners
24
	static final short N_FRtoBR   = 11880; // 12!/(12-4)! permutation of FR,FL,BL,BR edges
25
	static final short N_URtoUL   = 1320;  // 12!/(12-3)! permutation of UR,UF,UL edges
26
	static final short N_UBtoDF   = 1320;  // 12!/(12-3)! permutation of UB,DR,DF edges
27
	static final short N_URtoDF   = 20160; // 8!/(8-6)! permutation of UR,UF,UL,UB,DR,DF edges in phase2
28
	static final short N_MOVE     = 18;
29

    
30
	// All coordinates are 0 for a solved cube except for UBtoDF, which is 114
31
	short twist;
32
	short flip;
33
	short parity;
34
	short FRtoBR;
35
	short URFtoDLF;
36
	short URtoUL;
37
	short UBtoDF;
38
	int URtoDF;
39

    
40
	private static final boolean[] init = new boolean[12];
41
	
42
	static 
43
	  {
44
	  for(int i=0; i<12; i++ ) init[i] = false;	
45
	  }
46

    
47
  // Phase 1 move tables
48
  // Move table for the twists of the corners. twist < 2187 in phase 2; twist = 0 in phase 2.
49
	private static final byte[] twistMove = new byte[2*N_TWIST*N_MOVE];
50

    
51
	// Move table for the flips of the edges. flip < 2048 in phase 1; flip = 0 in phase 2.
52
	private static final byte[] flipMove  = new byte[2*N_FLIP*N_MOVE];
53

    
54
  // Parity of the corner permutation. This is the same as the parity for the edge permutation of a valid cube.
55
  // parity has values 0 and 1
56
	static short[][] parityMove = {
57
	                                { 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1 },
58
			                            { 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0 }
59
			                          };
60

    
61
  // Phase 1 and 2 movetable
62
  // Move table for the four UD-slice edges FR, FL, Bl and BR
63
  // FRtoBRMove < 11880 in phase 1; FRtoBRMove < 24 in phase 2; FRtoBRMove = 0 for solved cube
64
	private static final byte[] FRtoBR_Move = new byte[2*N_FRtoBR*N_MOVE];
65

    
66
	// Phase 1 and 2 movetable
67
	// Move table for permutation of six corners. The positions of the DBL and DRB corners are determined by the parity.
68
	// URFtoDLF < 20160 in phase 1; URFtoDLF < 20160 in phase 2; URFtoDLF = 0 for solved cube.
69
	private static final byte[] URFtoDLF_Move = new byte[2*N_URFtoDLF*N_MOVE];
70

    
71
	// Move table for the permutation of six U-face and D-face edges in phase2. The positions of the DL and DB edges are
72
	// determined by the parity.
73
	// URtoDF < 665280 in phase 1; URtoDF < 20160 in phase 2; URtoDF = 0 for solved cube.
74
	private static final byte[] URtoDF_Move = new byte[2*N_URtoDF*N_MOVE];
75

    
76
	// Helper move tables to compute URtoDF for the beginning of phase2
77
	// Move table for the three edges UR,UF and UL in phase1.
78
	private static final byte[] URtoUL_Move = new byte[2*N_URtoUL*N_MOVE];
79

    
80
	// Move table for the three edges UB,DR and DF in phase1.
81
	private static final byte[] UBtoDF_Move = new byte[2*N_UBtoDF*N_MOVE];
82

    
83
	// Table to merge the coordinates of the UR,UF,UL and UB,DR,DF edges at the beginning of phase2
84
	private static final byte[] MergeURtoULandUBtoDF = new byte[2*336*336];
85

    
86
	// Pruning tables for the search
87
	// Pruning table for the permutation of the corners and the UD-slice edges in phase2.
88
	// The pruning table entries give a lower estimation for the number of moves to reach the solved cube.
89
	public static byte[] Slice_URFtoDLF_Parity_Prun = new byte[N_SLICE2*N_URFtoDLF*N_PARITY / 2];
90

    
91
	// Pruning table for the permutation of the edges in phase2.
92
	// The pruning table entries give a lower estimation for the number of moves to reach the solved cube.
93
	public static byte[] Slice_URtoDF_Parity_Prun = new byte[N_SLICE2*N_URtoDF*N_PARITY / 2];
94

    
95
	// Pruning table for the twist of the corners and the position (not permutation) of the UD-slice edges in phase1
96
	// The pruning table entries give a lower estimation for the number of moves to reach the H-subgroup.
97
	public static byte[] Slice_Twist_Prun = new byte[N_SLICE1*N_TWIST / 2 + 1];
98

    
99
	// Pruning table for the flip of the edges and the position (not permutation) of the UD-slice edges in phase1
100
	// The pruning table entries give a lower estimation for the number of moves to reach the H-subgroup.
101
	public static byte[] Slice_Flip_Prun = new byte[N_SLICE1*N_FLIP / 2];
102

    
103
  private static final int[] resourceTable = new int[]
104
			{
105
			R.raw.cube3solver_table1,
106
			R.raw.cube3solver_table2,
107
			R.raw.cube3solver_table3,
108
			R.raw.cube3solver_table4,
109
			R.raw.cube3solver_table5,
110
			R.raw.cube3solver_table6,
111
			R.raw.cube3solver_table7,
112
			R.raw.cube3solver_table8,
113
			R.raw.cube3solver_table9,
114
			R.raw.cube3solver_table10,
115
			R.raw.cube3solver_table11,
116
			R.raw.cube3solver_table12
117
			};
118

    
119
  private static final byte[][] tableOfTables = new byte[][]
120
			{
121
			twistMove,
122
			flipMove,
123
			FRtoBR_Move,
124
			URFtoDLF_Move,
125
			URtoDF_Move,
126
			URtoUL_Move,
127
			UBtoDF_Move,
128
			MergeURtoULandUBtoDF,
129
			Slice_URFtoDLF_Parity_Prun,
130
			Slice_URtoDF_Parity_Prun,
131
			Slice_Twist_Prun,
132
			Slice_Flip_Prun
133
			};
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136
// Generate a CoordCube from a CubieCube
137

    
138
	SolverCoordCube(SolverCubieCube c)
139
	  {
140
		twist    = c.getTwist();
141
		flip     = c.getFlip();
142
		parity   = c.cornerParity();
143
		FRtoBR   = c.getFRtoBR();
144
		URFtoDLF = c.getURFtoDLF();
145
		URtoUL   = c.getURtoUL();
146
		UBtoDF   = c.getUBtoDF();
147
		URtoDF   = c.getURtoDF();// only needed in phase2
148
	  }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

    
152
	static byte getPruning(byte[] table, int index)
153
	  {
154
	  byte ret = table[index/2];
155
	  return (byte) ( ((index&1)==0) ? (ret&0x0f) : ((ret&0xf0)>>>4) );
156
	  }
157

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159

    
160
	public static short getTwistMove(int a,int b)
161
	  {
162
	  return getTable(a,b,twistMove,N_MOVE);
163
	  }
164

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

    
167
	public static short getFlipMove(int a,int b)
168
	  {
169
	  return getTable(a,b,flipMove,N_MOVE);
170
	  }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

    
174
	public static short getFRtoBR_Move(int a,int b)
175
	  {
176
	  return getTable(a,b,FRtoBR_Move,N_MOVE);
177
	  }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

    
181
	public static short getURFtoDLF_Move(int a,int b)
182
	  {
183
	  return getTable(a,b,URFtoDLF_Move,N_MOVE);
184
	  }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

    
188
	public static short getURtoDF_Move(int a,int b)
189
	  {
190
	  return getTable(a,b,URtoDF_Move,N_MOVE);
191
	  }
192

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

    
195
	public static short getURtoUL_Move(int a,int b)
196
	  {
197
	  return getTable(a,b,URtoUL_Move,N_MOVE);
198
	  }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

    
202
	public static short getUBtoDF_Move(int a,int b)
203
	  {
204
	  return getTable(a,b,UBtoDF_Move,N_MOVE);
205
	  }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

    
209
	public static short getMergeURtoULandUBtoDF(int a,int b)
210
	  {
211
	  return getTable(a,b,MergeURtoULandUBtoDF,336);
212
	  }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
	private static short getTable(int a,int b, byte[] table, int size)
217
	  {
218
		short upperS = table[2*(a*size+b)];
219
		short lowerS = table[2*(a*size+b)+1];
220

    
221
		if( upperS<0 ) upperS+=256;
222
		if( lowerS<0 ) lowerS+=256;
223

    
224
		return  (short)( (upperS<<8) + lowerS );
225
	  }
226

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

    
229
	public static void initialize(OperatingSystemInterface os, int index)
230
		{
231
		if( !init[index] )
232
		  {
233
		  try
234
		    {
235
		    byte[] table = tableOfTables[index];
236
			  InputStream is = os.openLocalFile(resourceTable[index]);
237
		    is.read( table, 0, table.length);
238
		    }
239
		  catch(Exception ioe)
240
		    {
241
		    System.out.println("error reading table"+index);
242
		    }
243

    
244
		  init[index]=true;
245
		  }
246
		}
247
}
(2-2/8)