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