Project

General

Profile

Download (8.77 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / solvers / cube3 / SolverCoordCube.java @ 41ce784b

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

    
7
package org.distorted.solvers.cube3;
8

    
9
import java.io.InputStream;
10
import android.content.res.Resources;
11

    
12
///////////////////////////////////////////////////////////////////////////////////////////////////
13

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134
// Generate a CoordCube from a CubieCube
135

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

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

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

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

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

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164

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

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

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

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

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

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

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

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

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

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

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

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

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

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

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

    
219
		if( upperS<0 ) upperS+=256;
220
		if( lowerS<0 ) lowerS+=256;
221

    
222
		return  (short)( (upperS<<8) + lowerS );
223
	  }
224

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226

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

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