|
1 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
2 |
// Copyright 2023 Leszek Koltunski //
|
|
3 |
// //
|
|
4 |
// This file is part of Magic Cube. //
|
|
5 |
// //
|
|
6 |
// Magic Cube is proprietary software licensed under an EULA which you should have received //
|
|
7 |
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html //
|
|
8 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
9 |
|
|
10 |
package org.distorted.solvers;
|
|
11 |
|
|
12 |
import android.content.res.Resources;
|
|
13 |
|
|
14 |
import org.distorted.main.R;
|
|
15 |
import org.distorted.objectlib.main.ObjectType;
|
|
16 |
import org.distorted.objectlib.main.TwistyObject;
|
|
17 |
import org.distorted.objectlib.tablebases.ImplementedTablebasesList;
|
|
18 |
import org.distorted.objectlib.tablebases.TablebaseHelpers;
|
|
19 |
import org.distorted.objectlib.tablebases.TablebasesAbstract;
|
|
20 |
|
|
21 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
22 |
|
|
23 |
public class SolverCuboid232 extends SolverTablebase
|
|
24 |
{
|
|
25 |
private static final int ERROR_CORNER_135_MISSING = -1;
|
|
26 |
private static final int ERROR_CORNER_134_MISSING = -2;
|
|
27 |
private static final int ERROR_CORNER_125_MISSING = -3;
|
|
28 |
private static final int ERROR_CORNER_124_MISSING = -4;
|
|
29 |
private static final int ERROR_CORNER_035_MISSING = -5;
|
|
30 |
private static final int ERROR_CORNER_034_MISSING = -6;
|
|
31 |
private static final int ERROR_CORNER_025_MISSING = -7;
|
|
32 |
private static final int ERROR_CORNER_024_MISSING = -8;
|
|
33 |
|
|
34 |
private static final int ERROR_EDGE_15_MISSING = -9;
|
|
35 |
private static final int ERROR_EDGE_14_MISSING = -10;
|
|
36 |
private static final int ERROR_EDGE_05_MISSING = -11;
|
|
37 |
private static final int ERROR_EDGE_04_MISSING = -12;
|
|
38 |
|
|
39 |
private static final int ERROR_CORNERS_CANNOT = -13;
|
|
40 |
private static final int ERROR_EDGE_TWISTED = -14;
|
|
41 |
private static final int ERROR_CORNER_TWISTED = -15;
|
|
42 |
|
|
43 |
TablebasesAbstract mSolver;
|
|
44 |
private final int[] mFaceColors;
|
|
45 |
|
|
46 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
47 |
|
|
48 |
private int edgeIs(int[] edge, int i0, int i1)
|
|
49 |
{
|
|
50 |
int c0 = mFaceColors[i0];
|
|
51 |
int c1 = mFaceColors[i1];
|
|
52 |
|
|
53 |
if( edge[0]==c0 && edge[1]==c1 ) return 0;
|
|
54 |
if( edge[0]==c1 && edge[1]==c0 ) return 1;
|
|
55 |
return 2;
|
|
56 |
}
|
|
57 |
|
|
58 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
59 |
|
|
60 |
private int retEdgePermutation(int[] output, int[][] edges)
|
|
61 |
{
|
|
62 |
for(int i=0; i<4; i++) output[i] = -1;
|
|
63 |
|
|
64 |
for(int i=0; i<4; i++)
|
|
65 |
{
|
|
66 |
int edge0 = edgeIs(edges[i],1,5);
|
|
67 |
if( edge0==0 ) output[0]=i;
|
|
68 |
else if( edge0==1 ) return ERROR_EDGE_TWISTED;
|
|
69 |
|
|
70 |
int edge1 = edgeIs(edges[i],1,4);
|
|
71 |
if( edge1==0 ) output[1]=i;
|
|
72 |
else if( edge1==1 ) return ERROR_EDGE_TWISTED;
|
|
73 |
|
|
74 |
int edge2 = edgeIs(edges[i],0,5);
|
|
75 |
if( edge2==0 ) output[2]=i;
|
|
76 |
else if( edge2==1 ) return ERROR_EDGE_TWISTED;
|
|
77 |
|
|
78 |
int edge3 = edgeIs(edges[i],0,4);
|
|
79 |
if( edge3==0 ) output[3]=i;
|
|
80 |
else if( edge3==1 ) return ERROR_EDGE_TWISTED;
|
|
81 |
}
|
|
82 |
|
|
83 |
if( output[0]==-1 ) return ERROR_EDGE_15_MISSING;
|
|
84 |
if( output[1]==-1 ) return ERROR_EDGE_14_MISSING;
|
|
85 |
if( output[2]==-1 ) return ERROR_EDGE_05_MISSING;
|
|
86 |
if( output[3]==-1 ) return ERROR_EDGE_04_MISSING;
|
|
87 |
|
|
88 |
return 0;
|
|
89 |
}
|
|
90 |
|
|
91 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
92 |
|
|
93 |
private int cornerIs(int[] corner, int i0, int i1, int i2)
|
|
94 |
{
|
|
95 |
int c0 = mFaceColors[i0];
|
|
96 |
int c1 = mFaceColors[i1];
|
|
97 |
int c2 = mFaceColors[i2];
|
|
98 |
|
|
99 |
if( corner[0]==c0 && corner[1]==c1 && corner[2]==c2 ) return 0;
|
|
100 |
|
|
101 |
if( corner[0]==c0 && corner[1]==c2 && corner[2]==c1 ||
|
|
102 |
corner[0]==c1 && corner[1]==c0 && corner[2]==c2 ||
|
|
103 |
corner[0]==c1 && corner[1]==c2 && corner[2]==c0 ||
|
|
104 |
corner[0]==c2 && corner[1]==c0 && corner[2]==c1 ||
|
|
105 |
corner[0]==c2 && corner[1]==c1 && corner[2]==c0 ) return 1;
|
|
106 |
|
|
107 |
return 2;
|
|
108 |
}
|
|
109 |
|
|
110 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
111 |
|
|
112 |
private int retCornerPermutation(int[] output, int[][] corners)
|
|
113 |
{
|
|
114 |
for(int i=0; i<8; i++) output[i] = -1;
|
|
115 |
|
|
116 |
for(int i=0; i<8; i++)
|
|
117 |
{
|
|
118 |
int corner7 = cornerIs(corners[i],2,4,0);
|
|
119 |
if( corner7==0 ) output[7]=i;
|
|
120 |
else if( corner7==1 ) return ERROR_CORNER_TWISTED;
|
|
121 |
|
|
122 |
int corner6 = cornerIs(corners[i],2,0,5);
|
|
123 |
if( corner6==0 ) output[6]=i;
|
|
124 |
else if( corner6==1 ) return ERROR_CORNER_TWISTED;
|
|
125 |
|
|
126 |
int corner5 = cornerIs(corners[i],3,0,4);
|
|
127 |
if( corner5==0 ) output[5]=i;
|
|
128 |
else if( corner5==1 ) return ERROR_CORNER_TWISTED;
|
|
129 |
|
|
130 |
int corner4 = cornerIs(corners[i],3,5,0);
|
|
131 |
if( corner4==0 ) output[4]=i;
|
|
132 |
else if( corner4==1 ) return ERROR_CORNER_TWISTED;
|
|
133 |
|
|
134 |
int corner3 = cornerIs(corners[i],2,1,4);
|
|
135 |
if( corner3==0 ) output[3]=i;
|
|
136 |
else if( corner3==1 ) return ERROR_CORNER_TWISTED;
|
|
137 |
|
|
138 |
int corner2 = cornerIs(corners[i],2,5,1);
|
|
139 |
if( corner2==0 ) output[2]=i;
|
|
140 |
else if( corner2==1 ) return ERROR_CORNER_TWISTED;
|
|
141 |
|
|
142 |
int corner1 = cornerIs(corners[i],3,4,1);
|
|
143 |
if( corner1==0 ) output[1]=i;
|
|
144 |
else if( corner1==1 ) return ERROR_CORNER_TWISTED;
|
|
145 |
|
|
146 |
int corner0 = cornerIs(corners[i],3,1,5);
|
|
147 |
if( corner0==0 ) output[0]=i;
|
|
148 |
else if( corner0==1 ) return ERROR_CORNER_TWISTED;
|
|
149 |
}
|
|
150 |
|
|
151 |
if( output[0]==-1 ) return ERROR_CORNER_135_MISSING;
|
|
152 |
if( output[1]==-1 ) return ERROR_CORNER_134_MISSING;
|
|
153 |
if( output[2]==-1 ) return ERROR_CORNER_125_MISSING;
|
|
154 |
if( output[3]==-1 ) return ERROR_CORNER_124_MISSING;
|
|
155 |
if( output[4]==-1 ) return ERROR_CORNER_035_MISSING;
|
|
156 |
if( output[5]==-1 ) return ERROR_CORNER_034_MISSING;
|
|
157 |
if( output[6]==-1 ) return ERROR_CORNER_025_MISSING;
|
|
158 |
if( output[7]==-1 ) return ERROR_CORNER_024_MISSING;
|
|
159 |
|
|
160 |
return 0;
|
|
161 |
}
|
|
162 |
|
|
163 |
////////////////////////////////////////////////////////////////////////////////////////
|
|
164 |
|
|
165 |
private int computeFaceColors(int[][] corners, int[][] edges)
|
|
166 |
{
|
|
167 |
mFaceColors[1] = edges[1][0];
|
|
168 |
mFaceColors[4] = edges[1][1];
|
|
169 |
|
|
170 |
if( edges[0][0]==mFaceColors[1] ) mFaceColors[5] = edges[0][1];
|
|
171 |
else if( edges[2][0]==mFaceColors[1] ) mFaceColors[5] = edges[2][1];
|
|
172 |
else if( edges[3][0]==mFaceColors[1] ) mFaceColors[5] = edges[3][1];
|
|
173 |
else return ERROR_EDGE_TWISTED;
|
|
174 |
|
|
175 |
if( edges[0][1]==mFaceColors[4] ) mFaceColors[0] = edges[0][0];
|
|
176 |
else if( edges[2][1]==mFaceColors[4] ) mFaceColors[0] = edges[2][0];
|
|
177 |
else if( edges[3][1]==mFaceColors[4] ) mFaceColors[0] = edges[3][0];
|
|
178 |
else return ERROR_EDGE_TWISTED;
|
|
179 |
|
|
180 |
boolean found2 = false;
|
|
181 |
boolean found3 = false;
|
|
182 |
|
|
183 |
for(int c=0; c<8; c++)
|
|
184 |
{
|
|
185 |
if( !found3 && corners[c][1]==mFaceColors[4] )
|
|
186 |
{
|
|
187 |
found3=true;
|
|
188 |
mFaceColors[3] = corners[c][0];
|
|
189 |
}
|
|
190 |
if( !found2 && corners[c][2]==mFaceColors[4] )
|
|
191 |
{
|
|
192 |
found2=true;
|
|
193 |
mFaceColors[2] = corners[c][0];
|
|
194 |
}
|
|
195 |
}
|
|
196 |
|
|
197 |
if( !found2 || !found3 ) return ERROR_CORNERS_CANNOT;
|
|
198 |
|
|
199 |
for(int i=0; i<6; i++)
|
|
200 |
for(int j=i+1; j<6; j++)
|
|
201 |
if( mFaceColors[i]==mFaceColors[j] ) return ERROR_CORNERS_CANNOT;
|
|
202 |
|
|
203 |
return 0;
|
|
204 |
}
|
|
205 |
|
|
206 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
207 |
|
|
208 |
public SolverCuboid232(Resources res, TwistyObject object)
|
|
209 |
{
|
|
210 |
super(res,object);
|
|
211 |
mFaceColors = new int[6];
|
|
212 |
}
|
|
213 |
|
|
214 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
215 |
|
|
216 |
public int tablebaseIndex(TwistyObject object)
|
|
217 |
{
|
|
218 |
int[][] corners= new int[8][3];
|
|
219 |
int[][] edges = new int[4][2];
|
|
220 |
|
|
221 |
corners[0][0] = object.getCubitFaceStickerIndex(0,3);
|
|
222 |
corners[0][1] = object.getCubitFaceStickerIndex(0,1);
|
|
223 |
corners[0][2] = object.getCubitFaceStickerIndex(0,5);
|
|
224 |
|
|
225 |
corners[1][0] = object.getCubitFaceStickerIndex(1,1);
|
|
226 |
corners[1][1] = object.getCubitFaceStickerIndex(1,5);
|
|
227 |
corners[1][2] = object.getCubitFaceStickerIndex(1,3);
|
|
228 |
|
|
229 |
corners[2][0] = object.getCubitFaceStickerIndex(2,3);
|
|
230 |
corners[2][1] = object.getCubitFaceStickerIndex(2,1);
|
|
231 |
corners[2][2] = object.getCubitFaceStickerIndex(2,5);
|
|
232 |
|
|
233 |
corners[3][0] = object.getCubitFaceStickerIndex(3,3);
|
|
234 |
corners[3][1] = object.getCubitFaceStickerIndex(3,1);
|
|
235 |
corners[3][2] = object.getCubitFaceStickerIndex(3,5);
|
|
236 |
|
|
237 |
corners[4][0] = object.getCubitFaceStickerIndex(4,5);
|
|
238 |
corners[4][1] = object.getCubitFaceStickerIndex(4,3);
|
|
239 |
corners[4][2] = object.getCubitFaceStickerIndex(4,1);
|
|
240 |
|
|
241 |
corners[5][0] = object.getCubitFaceStickerIndex(5,3);
|
|
242 |
corners[5][1] = object.getCubitFaceStickerIndex(5,1);
|
|
243 |
corners[5][2] = object.getCubitFaceStickerIndex(5,5);
|
|
244 |
|
|
245 |
corners[6][0] = object.getCubitFaceStickerIndex(6,3);
|
|
246 |
corners[6][1] = object.getCubitFaceStickerIndex(6,1);
|
|
247 |
corners[6][2] = object.getCubitFaceStickerIndex(6,5);
|
|
248 |
|
|
249 |
corners[7][0] = object.getCubitFaceStickerIndex(7,5);
|
|
250 |
corners[7][1] = object.getCubitFaceStickerIndex(7,3);
|
|
251 |
corners[7][2] = object.getCubitFaceStickerIndex(7,1);
|
|
252 |
|
|
253 |
edges[0][0] = object.getCubitFaceStickerIndex(8,5);
|
|
254 |
edges[0][1] = object.getCubitFaceStickerIndex(8,3);
|
|
255 |
edges[1][0] = object.getCubitFaceStickerIndex(9,3);
|
|
256 |
edges[1][1] = object.getCubitFaceStickerIndex(9,5);
|
|
257 |
edges[2][0] = object.getCubitFaceStickerIndex(10,5);
|
|
258 |
edges[2][1] = object.getCubitFaceStickerIndex(10,3);
|
|
259 |
edges[3][0] = object.getCubitFaceStickerIndex(11,3);
|
|
260 |
edges[3][1] = object.getCubitFaceStickerIndex(11,5);
|
|
261 |
|
|
262 |
int result0 = computeFaceColors(corners, edges);
|
|
263 |
if( result0<0 ) return result0;
|
|
264 |
|
|
265 |
int[] corner_perm = new int[8];
|
|
266 |
int result1 = retCornerPermutation(corner_perm,corners);
|
|
267 |
if( result1<0 ) return result1;
|
|
268 |
|
|
269 |
int[] edge_perm = new int[8];
|
|
270 |
int result2 = retEdgePermutation(edge_perm,edges);
|
|
271 |
if( result2<0 ) return result2;
|
|
272 |
|
|
273 |
int corner_perm_num = TablebaseHelpers.computePermutationNum(corner_perm);
|
|
274 |
int edge_perm_num = TablebaseHelpers.computePermutationNum(edge_perm);
|
|
275 |
|
|
276 |
return edge_perm_num + 6*corner_perm_num;
|
|
277 |
}
|
|
278 |
|
|
279 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
280 |
|
|
281 |
private int getColorIndex4(int face)
|
|
282 |
{
|
|
283 |
switch(mFaceColors[face])
|
|
284 |
{
|
|
285 |
case 0: return R.string.color_yellow4;
|
|
286 |
case 1: return R.string.color_white4;
|
|
287 |
case 2: return R.string.color_blue4;
|
|
288 |
case 3: return R.string.color_green4;
|
|
289 |
case 4: return R.string.color_red4;
|
|
290 |
case 5: return R.string.color_orange4;
|
|
291 |
}
|
|
292 |
|
|
293 |
return -1;
|
|
294 |
}
|
|
295 |
|
|
296 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
297 |
|
|
298 |
private int getColorIndex3(int face)
|
|
299 |
{
|
|
300 |
switch(mFaceColors[face])
|
|
301 |
{
|
|
302 |
case 0: return R.string.color_yellow3;
|
|
303 |
case 1: return R.string.color_white3;
|
|
304 |
case 2: return R.string.color_blue3;
|
|
305 |
case 3: return R.string.color_green3;
|
|
306 |
case 4: return R.string.color_red3;
|
|
307 |
case 5: return R.string.color_orange3;
|
|
308 |
}
|
|
309 |
|
|
310 |
return -1;
|
|
311 |
}
|
|
312 |
|
|
313 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
314 |
|
|
315 |
private int getColorIndex6(int face)
|
|
316 |
{
|
|
317 |
switch(mFaceColors[face])
|
|
318 |
{
|
|
319 |
case 0: return R.string.color_yellow6;
|
|
320 |
case 1: return R.string.color_white6;
|
|
321 |
case 2: return R.string.color_blue6;
|
|
322 |
case 3: return R.string.color_green6;
|
|
323 |
case 4: return R.string.color_red6;
|
|
324 |
case 5: return R.string.color_orange6;
|
|
325 |
}
|
|
326 |
|
|
327 |
return -1;
|
|
328 |
}
|
|
329 |
|
|
330 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
331 |
|
|
332 |
private String edgeError(Resources res, int face0, int face1)
|
|
333 |
{
|
|
334 |
int j0 = getColorIndex3(face0);
|
|
335 |
int j1 = getColorIndex6(face1);
|
|
336 |
|
|
337 |
String c0 = res.getString(j0);
|
|
338 |
String c1 = res.getString(j1);
|
|
339 |
|
|
340 |
return res.getString(R.string.solver_generic_missing_edge,c0,c1);
|
|
341 |
}
|
|
342 |
|
|
343 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
344 |
|
|
345 |
private String cornerError(Resources res, int face0, int face1, int face2)
|
|
346 |
{
|
|
347 |
int j0 = getColorIndex3(face0);
|
|
348 |
int j1 = getColorIndex3(face1);
|
|
349 |
int j2 = getColorIndex4(face2);
|
|
350 |
|
|
351 |
String c0 = res.getString(j0);
|
|
352 |
String c1 = res.getString(j1);
|
|
353 |
String c2 = res.getString(j2);
|
|
354 |
|
|
355 |
return res.getString(R.string.solver_generic_missing_corner,c0,c1,c2);
|
|
356 |
}
|
|
357 |
|
|
358 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
359 |
|
|
360 |
public String error(int index, Resources res)
|
|
361 |
{
|
|
362 |
switch(index)
|
|
363 |
{
|
|
364 |
case ERROR_CORNER_135_MISSING: return cornerError(res,1,3,5);
|
|
365 |
case ERROR_CORNER_134_MISSING: return cornerError(res,1,3,4);
|
|
366 |
case ERROR_CORNER_125_MISSING: return cornerError(res,1,2,5);
|
|
367 |
case ERROR_CORNER_124_MISSING: return cornerError(res,1,2,4);
|
|
368 |
case ERROR_CORNER_035_MISSING: return cornerError(res,0,3,5);
|
|
369 |
case ERROR_CORNER_034_MISSING: return cornerError(res,0,3,4);
|
|
370 |
case ERROR_CORNER_025_MISSING: return cornerError(res,0,2,5);
|
|
371 |
case ERROR_CORNER_024_MISSING: return cornerError(res,0,2,4);
|
|
372 |
|
|
373 |
case ERROR_EDGE_15_MISSING : return edgeError(res,1,5);
|
|
374 |
case ERROR_EDGE_14_MISSING : return edgeError(res,1,4);
|
|
375 |
case ERROR_EDGE_05_MISSING : return edgeError(res,0,5);
|
|
376 |
case ERROR_EDGE_04_MISSING : return edgeError(res,0,4);
|
|
377 |
|
|
378 |
case ERROR_CORNERS_CANNOT : return res.getString(R.string.solver_generic_corners_cannot);
|
|
379 |
case ERROR_EDGE_TWISTED : return res.getString(R.string.solver_generic_edge_twist);
|
|
380 |
case ERROR_CORNER_TWISTED : return res.getString(R.string.solver_generic_corner_twist);
|
|
381 |
}
|
|
382 |
|
|
383 |
return null;
|
|
384 |
}
|
|
385 |
|
|
386 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
387 |
|
|
388 |
public int[][] solution(int index, Resources res)
|
|
389 |
{
|
|
390 |
if( mSolver==null )
|
|
391 |
{
|
|
392 |
mSolver = ImplementedTablebasesList.createPacked(res,ObjectType.CU_232);
|
|
393 |
}
|
|
394 |
|
|
395 |
return mSolver!=null ? mSolver.solution(index) : null;
|
|
396 |
}
|
|
397 |
}
|
|
398 |
|
Beginnings of the 2x2x3 solver.