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