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.metadata.ListObjects;
|
17
|
import org.distorted.objectlib.main.TwistyObject;
|
18
|
import org.distorted.objectlib.tablebases.ImplementedTablebasesList;
|
19
|
import org.distorted.objectlib.tablebases.TBCuboid323;
|
20
|
import org.distorted.objectlib.tablebases.TablebaseHelpers;
|
21
|
import org.distorted.objectlib.tablebases.TablebasesAbstract;
|
22
|
|
23
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
24
|
|
25
|
public class SolverCuboid323 extends SolverTablebase
|
26
|
{
|
27
|
private static final int ERROR_CORNER_MISSING = -1;
|
28
|
private static final int ERROR_EDGE_MISSING = -2;
|
29
|
private static final int ERROR_CORNERS_CANNOT = -3;
|
30
|
private static final int ERROR_EDGE_TWISTED = -4;
|
31
|
private static final int ERROR_CORNER_TWISTED = -5;
|
32
|
|
33
|
TablebasesAbstract mSolver;
|
34
|
private final int[] mFaceColors;
|
35
|
private int mErrorColor1, mErrorColor2, mErrorColor3;
|
36
|
|
37
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
38
|
|
39
|
public SolverCuboid323(OperatingSystemInterface os, Resources res, TwistyObject object)
|
40
|
{
|
41
|
super(os,res,object);
|
42
|
mFaceColors = new int[6];
|
43
|
}
|
44
|
|
45
|
////////////////////////////////////////////////////////////////////////////////////////
|
46
|
|
47
|
private int findCorner(int[][] corners, int c1, int c2)
|
48
|
{
|
49
|
for(int i=0; i<8; i++)
|
50
|
{
|
51
|
int[] c = corners[i];
|
52
|
|
53
|
if( c[0]==c1 && c[1]==c2 ) return c[2];
|
54
|
if( c[1]==c1 && c[2]==c2 ) return c[0];
|
55
|
if( c[2]==c1 && c[0]==c2 ) return c[1];
|
56
|
}
|
57
|
|
58
|
return ERROR_CORNERS_CANNOT;
|
59
|
}
|
60
|
|
61
|
////////////////////////////////////////////////////////////////////////////////////////
|
62
|
|
63
|
private int missingColor()
|
64
|
{
|
65
|
boolean[] present = new boolean[6];
|
66
|
for(int i=0; i<5; i++) present[mFaceColors[i]] = true;
|
67
|
|
68
|
int indexFalse = -1;
|
69
|
|
70
|
for(int i=0; i<6; i++)
|
71
|
if( !present[i] )
|
72
|
{
|
73
|
if( indexFalse<0 ) indexFalse=i;
|
74
|
else return ERROR_CORNERS_CANNOT;
|
75
|
}
|
76
|
|
77
|
return indexFalse;
|
78
|
}
|
79
|
|
80
|
////////////////////////////////////////////////////////////////////////////////////////
|
81
|
|
82
|
private int edgePresent(int[][] edges, int f0, int f1)
|
83
|
{
|
84
|
int c0 = mFaceColors[f0];
|
85
|
int c1 = mFaceColors[f1];
|
86
|
|
87
|
for(int i=0; i<8; i++ )
|
88
|
{
|
89
|
int[] edge = edges[i];
|
90
|
|
91
|
if( edge[0]==c0 && edge[1]==c1 ) return i;
|
92
|
if( edge[0]==c1 && edge[1]==c0 )
|
93
|
{
|
94
|
mErrorColor1 = c0;
|
95
|
mErrorColor2 = c1;
|
96
|
return ERROR_EDGE_TWISTED;
|
97
|
}
|
98
|
}
|
99
|
|
100
|
mErrorColor1 = c0;
|
101
|
mErrorColor2 = c1;
|
102
|
return ERROR_EDGE_MISSING;
|
103
|
}
|
104
|
|
105
|
////////////////////////////////////////////////////////////////////////////////////////
|
106
|
|
107
|
private int cornerPresent(int[][] corners, int f0, int f1, int f2)
|
108
|
{
|
109
|
int c0 = mFaceColors[f0];
|
110
|
int c1 = mFaceColors[f1];
|
111
|
int c2 = mFaceColors[f2];
|
112
|
|
113
|
for(int i=0; i<8; i++)
|
114
|
{
|
115
|
int[] corner = corners[i];
|
116
|
|
117
|
if( corner[0]==c0 && corner[1]==c1 && corner[2]==c2 ) return i;
|
118
|
if( (corner[0]==c1 && corner[1]==c2 && corner[2]==c0 ) ||
|
119
|
(corner[0]==c2 && corner[1]==c0 && corner[2]==c1 ) )
|
120
|
{
|
121
|
mErrorColor1 = c0;
|
122
|
mErrorColor2 = c1;
|
123
|
mErrorColor3 = c2;
|
124
|
return ERROR_CORNER_TWISTED;
|
125
|
}
|
126
|
}
|
127
|
|
128
|
mErrorColor1 = c0;
|
129
|
mErrorColor2 = c1;
|
130
|
mErrorColor3 = c2;
|
131
|
return ERROR_CORNER_MISSING;
|
132
|
}
|
133
|
|
134
|
////////////////////////////////////////////////////////////////////////////////////////
|
135
|
|
136
|
private int retEdgePermutation(int[] output, int[][] edges)
|
137
|
{
|
138
|
int[][] e = { {5,3}, {4,3}, {5,2}, {4,2}, {1,3}, {1,2}, {0,3}, {0,2} };
|
139
|
|
140
|
for(int i=0; i<8; i++)
|
141
|
{
|
142
|
int[] ee = e[i];
|
143
|
output[i] = edgePresent(edges,ee[0],ee[1]);
|
144
|
if( output[i]<0 ) return output[i];
|
145
|
}
|
146
|
|
147
|
return 0;
|
148
|
}
|
149
|
|
150
|
////////////////////////////////////////////////////////////////////////////////////////
|
151
|
|
152
|
private int retCornerPermutation(int[] output, int[][] corners)
|
153
|
{
|
154
|
int[][] c = { {5,1,3}, {1,4,3}, {1,5,2}, {4,1,2}, {0,5,3}, {4,0,3}, {5,0,2}, {0,4,2} };
|
155
|
|
156
|
for(int i=0; i<8; i++)
|
157
|
{
|
158
|
int[] cc = c[i];
|
159
|
output[i] = cornerPresent(corners,cc[0],cc[1],cc[2]);
|
160
|
if( output[i]<0 ) return output[i];
|
161
|
}
|
162
|
|
163
|
return 0;
|
164
|
}
|
165
|
|
166
|
////////////////////////////////////////////////////////////////////////////////////////
|
167
|
|
168
|
private int computeFaceColors(int[][] corners, int[][] edges, int[] centers)
|
169
|
{
|
170
|
mFaceColors[4] = edges[1][0];
|
171
|
mFaceColors[3] = edges[1][1];
|
172
|
|
173
|
if( centers[0]!=mFaceColors[3] ) mFaceColors[2] = centers[0];
|
174
|
else if( centers[1]!=mFaceColors[3] ) mFaceColors[2] = centers[1];
|
175
|
else return ERROR_CORNERS_CANNOT;
|
176
|
|
177
|
mFaceColors[0] = findCorner(corners,mFaceColors[4],mFaceColors[2]);
|
178
|
if( mFaceColors[0]<0 ) return mFaceColors[0];
|
179
|
|
180
|
mFaceColors[1] = findCorner(corners,mFaceColors[2],mFaceColors[4]);
|
181
|
if( mFaceColors[1]<0 ) return mFaceColors[1];
|
182
|
|
183
|
mFaceColors[5] = missingColor();
|
184
|
if( mFaceColors[5]<0 ) return mFaceColors[5];
|
185
|
|
186
|
return 0;
|
187
|
}
|
188
|
|
189
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
190
|
|
191
|
private void getCorners(TwistyObject object, int[][] corners)
|
192
|
{
|
193
|
corners[0][0] = object.getCubitFaceStickerIndex(0,5);
|
194
|
corners[0][1] = object.getCubitFaceStickerIndex(0,1);
|
195
|
corners[0][2] = object.getCubitFaceStickerIndex(0,3);
|
196
|
|
197
|
corners[1][0] = object.getCubitFaceStickerIndex(1,3);
|
198
|
corners[1][1] = object.getCubitFaceStickerIndex(1,5);
|
199
|
corners[1][2] = object.getCubitFaceStickerIndex(1,1);
|
200
|
|
201
|
corners[2][0] = object.getCubitFaceStickerIndex(2,5);
|
202
|
corners[2][1] = object.getCubitFaceStickerIndex(2,1);
|
203
|
corners[2][2] = object.getCubitFaceStickerIndex(2,3);
|
204
|
|
205
|
corners[3][0] = object.getCubitFaceStickerIndex(3,5);
|
206
|
corners[3][1] = object.getCubitFaceStickerIndex(3,1);
|
207
|
corners[3][2] = object.getCubitFaceStickerIndex(3,3);
|
208
|
|
209
|
corners[4][0] = object.getCubitFaceStickerIndex(4,1);
|
210
|
corners[4][1] = object.getCubitFaceStickerIndex(4,3);
|
211
|
corners[4][2] = object.getCubitFaceStickerIndex(4,5);
|
212
|
|
213
|
corners[5][0] = object.getCubitFaceStickerIndex(5,5);
|
214
|
corners[5][1] = object.getCubitFaceStickerIndex(5,1);
|
215
|
corners[5][2] = object.getCubitFaceStickerIndex(5,3);
|
216
|
|
217
|
corners[6][0] = object.getCubitFaceStickerIndex(6,5);
|
218
|
corners[6][1] = object.getCubitFaceStickerIndex(6,1);
|
219
|
corners[6][2] = object.getCubitFaceStickerIndex(6,3);
|
220
|
|
221
|
corners[7][0] = object.getCubitFaceStickerIndex(7,1);
|
222
|
corners[7][1] = object.getCubitFaceStickerIndex(7,3);
|
223
|
corners[7][2] = object.getCubitFaceStickerIndex(7,5);
|
224
|
}
|
225
|
|
226
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
227
|
|
228
|
private void getEdges(TwistyObject object, int[][] edges)
|
229
|
{
|
230
|
edges[0][0] = object.getCubitFaceStickerIndex(8,5);
|
231
|
edges[0][1] = object.getCubitFaceStickerIndex(8,3);
|
232
|
edges[1][0] = object.getCubitFaceStickerIndex(9,3);
|
233
|
edges[1][1] = object.getCubitFaceStickerIndex(9,5);
|
234
|
edges[2][0] = object.getCubitFaceStickerIndex(10,3);
|
235
|
edges[2][1] = object.getCubitFaceStickerIndex(10,5);
|
236
|
edges[3][0] = object.getCubitFaceStickerIndex(11,5);
|
237
|
edges[3][1] = object.getCubitFaceStickerIndex(11,3);
|
238
|
edges[4][0] = object.getCubitFaceStickerIndex(12,3);
|
239
|
edges[4][1] = object.getCubitFaceStickerIndex(12,5);
|
240
|
edges[5][0] = object.getCubitFaceStickerIndex(13,5);
|
241
|
edges[5][1] = object.getCubitFaceStickerIndex(13,3);
|
242
|
edges[6][0] = object.getCubitFaceStickerIndex(14,3);
|
243
|
edges[6][1] = object.getCubitFaceStickerIndex(14,5);
|
244
|
edges[7][0] = object.getCubitFaceStickerIndex(15,5);
|
245
|
edges[7][1] = object.getCubitFaceStickerIndex(15,3);
|
246
|
}
|
247
|
|
248
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
249
|
|
250
|
private void getCenters(TwistyObject object, int[] centers)
|
251
|
{
|
252
|
centers[0] = object.getCubitFaceStickerIndex(16,4);
|
253
|
centers[1] = object.getCubitFaceStickerIndex(17,4);
|
254
|
}
|
255
|
|
256
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
257
|
|
258
|
public int tablebaseIndex(TwistyObject object)
|
259
|
{
|
260
|
int[][] corners= new int[8][3];
|
261
|
int[][] edges = new int[8][2];
|
262
|
int[] centers = new int[2];
|
263
|
|
264
|
getCorners(object,corners);
|
265
|
getEdges(object,edges);
|
266
|
getCenters(object,centers);
|
267
|
|
268
|
//for(int i=0; i<8; i++) android.util.Log.e("D", "corner: "+i+" : "+corners[i][0]+" "+corners[i][1]+" "+corners[i][2]);
|
269
|
//for(int i=0; i<8; i++) android.util.Log.e("D", "edge: "+i+" : "+edges[i][0]+" "+edges[i][1]);
|
270
|
|
271
|
int result0 = computeFaceColors(corners, edges, centers);
|
272
|
if( result0<0 ) return result0;
|
273
|
|
274
|
int[] corner_perm = new int[8];
|
275
|
int result1 = retCornerPermutation(corner_perm,corners);
|
276
|
if( result1<0 ) return result1;
|
277
|
|
278
|
//android.util.Log.e("D", "upper: "+mUpper);
|
279
|
//for(int i=0; i<6; i++) android.util.Log.e("D", "face color: "+mFaceColors[i]);
|
280
|
|
281
|
int[] edge_perm8 = new int[8];
|
282
|
int result2 = retEdgePermutation(edge_perm8,edges);
|
283
|
if( result2<0 ) return result2;
|
284
|
|
285
|
int[] edge_perm7 = TBCuboid323.edgePermTo7(edge_perm8);
|
286
|
/*
|
287
|
TablebaseHelpers.displayTable(corner_perm, "CORNER PERM");
|
288
|
TablebaseHelpers.displayTable(edge_perm8, "EDGE PERM8");
|
289
|
TablebaseHelpers.displayTable(edge_perm7, "EDGE PERM7");
|
290
|
*/
|
291
|
int corner_perm_num = TablebaseHelpers.computePermutationNum(corner_perm);
|
292
|
int edge_perm_num = TablebaseHelpers.computePermutationNum(edge_perm7);
|
293
|
int centersInPlace = (centers[0]==mFaceColors[2]) ? 0 : 1;
|
294
|
|
295
|
//android.util.Log.e("D", "corner_perm_num: "+corner_perm_num+" edge_perm_num: "+edge_perm_num+" inPlace: "+centersInPlace);
|
296
|
//android.util.Log.e("D", "index="+(corner_perm_num + 40320*( centersInPlace + 2*edge_perm_num)));
|
297
|
|
298
|
return corner_perm_num + 40320*( centersInPlace + 2*edge_perm_num);
|
299
|
}
|
300
|
|
301
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
302
|
|
303
|
private int getColorIndex4(int color)
|
304
|
{
|
305
|
switch(color)
|
306
|
{
|
307
|
case 0: return R.string.color_yellow4;
|
308
|
case 1: return R.string.color_white4;
|
309
|
case 2: return R.string.color_blue4;
|
310
|
case 3: return R.string.color_green4;
|
311
|
case 4: return R.string.color_red4;
|
312
|
case 5: return R.string.color_orange4;
|
313
|
}
|
314
|
|
315
|
return -1;
|
316
|
}
|
317
|
|
318
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
319
|
|
320
|
private int getColorIndex3(int color)
|
321
|
{
|
322
|
switch(color)
|
323
|
{
|
324
|
case 0: return R.string.color_yellow3;
|
325
|
case 1: return R.string.color_white3;
|
326
|
case 2: return R.string.color_blue3;
|
327
|
case 3: return R.string.color_green3;
|
328
|
case 4: return R.string.color_red3;
|
329
|
case 5: return R.string.color_orange3;
|
330
|
}
|
331
|
|
332
|
return -1;
|
333
|
}
|
334
|
|
335
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
336
|
|
337
|
private int getColorIndex5(int color)
|
338
|
{
|
339
|
switch(color)
|
340
|
{
|
341
|
case 0: return R.string.color_yellow5;
|
342
|
case 1: return R.string.color_white5;
|
343
|
case 2: return R.string.color_blue5;
|
344
|
case 3: return R.string.color_green5;
|
345
|
case 4: return R.string.color_red5;
|
346
|
case 5: return R.string.color_orange5;
|
347
|
}
|
348
|
|
349
|
return -1;
|
350
|
}
|
351
|
|
352
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
353
|
|
354
|
private int getColorIndex6(int color)
|
355
|
{
|
356
|
switch(color)
|
357
|
{
|
358
|
case 0: return R.string.color_yellow6;
|
359
|
case 1: return R.string.color_white6;
|
360
|
case 2: return R.string.color_blue6;
|
361
|
case 3: return R.string.color_green6;
|
362
|
case 4: return R.string.color_red6;
|
363
|
case 5: return R.string.color_orange6;
|
364
|
}
|
365
|
|
366
|
return -1;
|
367
|
}
|
368
|
|
369
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
370
|
|
371
|
private String edgeTwistedError(Resources res, int color0, int color1)
|
372
|
{
|
373
|
int j0 = getColorIndex3(color0);
|
374
|
int j1 = getColorIndex6(color1);
|
375
|
|
376
|
String c0 = res.getString(j0);
|
377
|
String c1 = res.getString(j1);
|
378
|
|
379
|
return res.getString(R.string.solver_generic_twisted_edge,c0,c1);
|
380
|
}
|
381
|
|
382
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
383
|
|
384
|
private String cornerTwistedError(Resources res, int color0, int color1, int color2)
|
385
|
{
|
386
|
int j0 = getColorIndex3(color0);
|
387
|
int j1 = getColorIndex3(color1);
|
388
|
int j2 = getColorIndex5(color2);
|
389
|
|
390
|
String c0 = res.getString(j0);
|
391
|
String c1 = res.getString(j1);
|
392
|
String c2 = res.getString(j2);
|
393
|
|
394
|
return res.getString(R.string.solver_generic_twisted_corner,c0,c1,c2);
|
395
|
}
|
396
|
|
397
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
398
|
|
399
|
private String edgeMissingError(Resources res, int color0, int color1)
|
400
|
{
|
401
|
int j0 = getColorIndex3(color0);
|
402
|
int j1 = getColorIndex6(color1);
|
403
|
|
404
|
String c0 = res.getString(j0);
|
405
|
String c1 = res.getString(j1);
|
406
|
|
407
|
return res.getString(R.string.solver_generic_missing_edge,c0,c1);
|
408
|
}
|
409
|
|
410
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
411
|
|
412
|
private String cornerMissingError(Resources res, int color0, int color1, int color2)
|
413
|
{
|
414
|
int j0 = getColorIndex3(color0);
|
415
|
int j1 = getColorIndex3(color1);
|
416
|
int j2 = getColorIndex4(color2);
|
417
|
|
418
|
String c0 = res.getString(j0);
|
419
|
String c1 = res.getString(j1);
|
420
|
String c2 = res.getString(j2);
|
421
|
|
422
|
return res.getString(R.string.solver_generic_missing_corner,c0,c1,c2);
|
423
|
}
|
424
|
|
425
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
426
|
|
427
|
public String error(int index, Resources res)
|
428
|
{
|
429
|
switch(index)
|
430
|
{
|
431
|
case ERROR_CORNER_MISSING : return cornerMissingError(res,mErrorColor1,mErrorColor2,mErrorColor3);
|
432
|
case ERROR_EDGE_MISSING : return edgeMissingError(res,mErrorColor1,mErrorColor2);
|
433
|
case ERROR_CORNERS_CANNOT : return res.getString(R.string.solver_generic_corners_cannot);
|
434
|
case ERROR_EDGE_TWISTED : return edgeTwistedError(res,mErrorColor1,mErrorColor2);
|
435
|
case ERROR_CORNER_TWISTED : return cornerTwistedError(res,mErrorColor1,mErrorColor2,mErrorColor3);
|
436
|
}
|
437
|
|
438
|
return null;
|
439
|
}
|
440
|
|
441
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
442
|
// a few cu_323 max (depth 18) indices:
|
443
|
// 1180633, 1180642, 1182044, 1190482, 128151851, 128190028
|
444
|
|
445
|
public int[][] solution(int index, OperatingSystemInterface os)
|
446
|
{
|
447
|
if( mSolver==null )
|
448
|
{
|
449
|
mSolver = ImplementedTablebasesList.createPacked(os, ListObjects.CU_323.name() );
|
450
|
}
|
451
|
|
452
|
((TBCuboid323)mSolver).initialize();
|
453
|
return mSolver!=null ? mSolver.solution(index,null,os) : null;
|
454
|
}
|
455
|
}
|
456
|
|