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.objectlib.main.ObjectType;
|
15
|
import org.distorted.objectlib.main.TwistyObject;
|
16
|
import org.distorted.objectlib.tablebases.ImplementedTablebasesList;
|
17
|
import org.distorted.objectlib.tablebases.TablebaseHelpers;
|
18
|
import org.distorted.objectlib.tablebases.TablebasesAbstract;
|
19
|
|
20
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
21
|
|
22
|
public class SolverSkewbDiamond extends SolverTablebase
|
23
|
{
|
24
|
private static final int ERROR_CORNER_FR_MISSING = -1;
|
25
|
private static final int ERROR_CORNER_BR_MISSING = -2;
|
26
|
private static final int ERROR_CORNER_BL_MISSING = -3;
|
27
|
private static final int ERROR_CORNER_FL_MISSING = -4;
|
28
|
private static final int ERROR_CORNER_TO_MISSING = -5;
|
29
|
private static final int ERROR_CORNER_BO_MISSING = -6;
|
30
|
|
31
|
private static final int ERROR_CENTER_0_MISSING = -7;
|
32
|
private static final int ERROR_CENTER_1_MISSING = -8;
|
33
|
private static final int ERROR_CENTER_2_MISSING = -9;
|
34
|
private static final int ERROR_CENTER_3_MISSING = -10;
|
35
|
private static final int ERROR_CENTER_4_MISSING = -11;
|
36
|
private static final int ERROR_CENTER_5_MISSING = -12;
|
37
|
private static final int ERROR_CENTER_6_MISSING = -13;
|
38
|
private static final int ERROR_CENTER_7_MISSING = -14;
|
39
|
|
40
|
private static final int ERROR_TWO_CORNERS = -15;
|
41
|
private static final int ERROR_TWO_CENTERS = -16;
|
42
|
private static final int ERROR_CORNER_TWIST = -17;
|
43
|
private static final int ERROR_CORNERS_CANNOT = -18;
|
44
|
|
45
|
private TablebasesAbstract mSolver;
|
46
|
|
47
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
48
|
|
49
|
public SolverSkewbDiamond(Resources res, TwistyObject object)
|
50
|
{
|
51
|
super(res,object);
|
52
|
}
|
53
|
|
54
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
55
|
|
56
|
private int[] getCornersPermutation(int[] colors, int[][] corners)
|
57
|
{
|
58
|
|
59
|
}
|
60
|
|
61
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
62
|
|
63
|
private int[] getCornersTwist(int[] colors, int[][] corners)
|
64
|
{
|
65
|
|
66
|
}
|
67
|
|
68
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
69
|
|
70
|
private int[] getFreeCentersPermutation(int[] colors, int[] centers)
|
71
|
{
|
72
|
|
73
|
}
|
74
|
|
75
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
76
|
|
77
|
private int getTotalTwist(int[] twist)
|
78
|
{
|
79
|
int total = 0;
|
80
|
|
81
|
for(int i=0; i<6; i++)
|
82
|
{
|
83
|
int t= twist[i];
|
84
|
if( t==1 || t==3 ) return -1;
|
85
|
if( t==2 ) total++;
|
86
|
total *= 2;
|
87
|
}
|
88
|
|
89
|
return total;
|
90
|
}
|
91
|
|
92
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
93
|
|
94
|
private int figureOutColor(int[][] corners, int c1, int c2)
|
95
|
{
|
96
|
for(int i=0; i<6; i++)
|
97
|
{
|
98
|
int[] cor = corners[i];
|
99
|
|
100
|
if( cor[0]==c1 && cor[2]==c2 ) return cor[1];
|
101
|
if( cor[1]==c1 && cor[3]==c2 ) return cor[2];
|
102
|
if( cor[2]==c1 && cor[0]==c2 ) return cor[3];
|
103
|
if( cor[3]==c1 && cor[1]==c2 ) return cor[0];
|
104
|
}
|
105
|
|
106
|
return -1;
|
107
|
}
|
108
|
|
109
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
110
|
// We only move the UR, UR, DR & DB faces --> those centers are fixed and determine the colors of
|
111
|
// the faces.
|
112
|
|
113
|
private int figureOutFaceColors(int[] output, int[] centers, int[][] corners)
|
114
|
{
|
115
|
output[1] = centers[1];
|
116
|
output[3] = centers[3];
|
117
|
output[4] = centers[4];
|
118
|
output[6] = centers[6];
|
119
|
|
120
|
int color01 = figureOutColor(corners,output[4],output[1]);
|
121
|
if( color01<0 ) return ERROR_CORNER_FR_MISSING;
|
122
|
int color02 = figureOutColor(corners,output[3],output[4]);
|
123
|
if( color02<0 ) return ERROR_CORNER_FL_MISSING;
|
124
|
int color03 = figureOutColor(corners,output[1],output[3]);
|
125
|
if( color03<0 ) return ERROR_CORNER_TO_MISSING;
|
126
|
if( color01!=color02 || color01!=color03 ) return ERROR_CORNERS_CANNOT;
|
127
|
output[0] = color01;
|
128
|
|
129
|
int color21 = figureOutColor(corners,output[1],output[6]);
|
130
|
if( color21<0 ) return ERROR_CORNER_BR_MISSING;
|
131
|
int color22 = figureOutColor(corners,output[6],output[3]);
|
132
|
if( color22<0 ) return ERROR_CORNER_BL_MISSING;
|
133
|
int color23 = figureOutColor(corners,output[3],output[1]);
|
134
|
if( color23<0 ) return ERROR_CORNER_TO_MISSING;
|
135
|
if( color21!=color22 || color21!=color23 ) return ERROR_CORNERS_CANNOT;
|
136
|
output[2] = color21;
|
137
|
|
138
|
int color51 = figureOutColor(corners,output[1],output[4]);
|
139
|
if( color51<0 ) return ERROR_CORNER_FR_MISSING;
|
140
|
int color52 = figureOutColor(corners,output[6],output[1]);
|
141
|
if( color52<0 ) return ERROR_CORNER_BR_MISSING;
|
142
|
int color53 = figureOutColor(corners,output[4],output[6]);
|
143
|
if( color53<0 ) return ERROR_CORNER_BO_MISSING;
|
144
|
if( color51!=color52 || color51!=color53 ) return ERROR_CORNERS_CANNOT;
|
145
|
output[5] = color51;
|
146
|
|
147
|
int color71 = figureOutColor(corners,output[3],output[6]);
|
148
|
if( color71<0 ) return ERROR_CORNER_BL_MISSING;
|
149
|
int color72 = figureOutColor(corners,output[4],output[3]);
|
150
|
if( color72<0 ) return ERROR_CORNER_FL_MISSING;
|
151
|
int color73 = figureOutColor(corners,output[6],output[4]);
|
152
|
if( color73<0 ) return ERROR_CORNER_BO_MISSING;
|
153
|
if( color71!=color72 || color71!=color73 ) return ERROR_CORNERS_CANNOT;
|
154
|
output[7] = color71;
|
155
|
|
156
|
return 0;
|
157
|
}
|
158
|
|
159
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
160
|
|
161
|
private int checkAllColorsDifferent(int[] colors)
|
162
|
{
|
163
|
for(int i=0; i<8; i++)
|
164
|
{
|
165
|
boolean present = false;
|
166
|
|
167
|
for(int j=0; j<8; j++)
|
168
|
if( colors[j]==i )
|
169
|
{
|
170
|
present=true;
|
171
|
break;
|
172
|
}
|
173
|
|
174
|
if( !present ) return ERROR_CORNERS_CANNOT;
|
175
|
}
|
176
|
|
177
|
return 0;
|
178
|
}
|
179
|
|
180
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
181
|
|
182
|
private int checkAllCentersPresent(int[] centers)
|
183
|
{
|
184
|
for(int i=0; i<8; i++)
|
185
|
{
|
186
|
boolean present = false;
|
187
|
|
188
|
for(int j=0; j<8; j++)
|
189
|
if( centers[j]==i )
|
190
|
{
|
191
|
present=true;
|
192
|
break;
|
193
|
}
|
194
|
|
195
|
if( !present )
|
196
|
{
|
197
|
switch(i)
|
198
|
{
|
199
|
case 0: return ERROR_CENTER_0_MISSING;
|
200
|
case 1: return ERROR_CENTER_1_MISSING;
|
201
|
case 2: return ERROR_CENTER_2_MISSING;
|
202
|
case 3: return ERROR_CENTER_3_MISSING;
|
203
|
case 4: return ERROR_CENTER_4_MISSING;
|
204
|
case 5: return ERROR_CENTER_5_MISSING;
|
205
|
case 6: return ERROR_CENTER_6_MISSING;
|
206
|
case 7: return ERROR_CENTER_7_MISSING;
|
207
|
}
|
208
|
}
|
209
|
}
|
210
|
|
211
|
return 0;
|
212
|
}
|
213
|
|
214
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
215
|
|
216
|
private void getCorners(TwistyObject object, int[][] corners)
|
217
|
{
|
218
|
corners[0][0] = object.getCubitFaceStickerIndex( 0,0); // FR
|
219
|
corners[0][1] = object.getCubitFaceStickerIndex( 0,1);
|
220
|
corners[0][2] = object.getCubitFaceStickerIndex( 0,5);
|
221
|
corners[0][3] = object.getCubitFaceStickerIndex( 0,4);
|
222
|
|
223
|
corners[1][0] = object.getCubitFaceStickerIndex( 1,1); // BR
|
224
|
corners[1][1] = object.getCubitFaceStickerIndex( 1,2);
|
225
|
corners[1][2] = object.getCubitFaceStickerIndex( 1,6);
|
226
|
corners[1][3] = object.getCubitFaceStickerIndex( 1,5);
|
227
|
|
228
|
corners[2][0] = object.getCubitFaceStickerIndex( 2,2); // BL
|
229
|
corners[2][1] = object.getCubitFaceStickerIndex( 2,3);
|
230
|
corners[2][2] = object.getCubitFaceStickerIndex( 2,7);
|
231
|
corners[2][3] = object.getCubitFaceStickerIndex( 2,6);
|
232
|
|
233
|
corners[3][0] = object.getCubitFaceStickerIndex( 3,3); // FL
|
234
|
corners[3][1] = object.getCubitFaceStickerIndex( 3,0);
|
235
|
corners[3][2] = object.getCubitFaceStickerIndex( 3,4);
|
236
|
corners[3][3] = object.getCubitFaceStickerIndex( 3,7);
|
237
|
|
238
|
corners[4][0] = object.getCubitFaceStickerIndex( 4,0); // U
|
239
|
corners[4][1] = object.getCubitFaceStickerIndex( 4,3);
|
240
|
corners[4][2] = object.getCubitFaceStickerIndex( 4,2);
|
241
|
corners[4][3] = object.getCubitFaceStickerIndex( 4,1);
|
242
|
|
243
|
corners[5][0] = object.getCubitFaceStickerIndex( 5,6); // D
|
244
|
corners[5][1] = object.getCubitFaceStickerIndex( 5,7);
|
245
|
corners[5][2] = object.getCubitFaceStickerIndex( 5,4);
|
246
|
corners[5][3] = object.getCubitFaceStickerIndex( 5,5);
|
247
|
}
|
248
|
|
249
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
250
|
|
251
|
private void getCenters(TwistyObject object, int[] centers)
|
252
|
{
|
253
|
centers[0] = object.getCubitFaceStickerIndex( 9,0); // UF
|
254
|
centers[1] = object.getCubitFaceStickerIndex( 6,0); // UR
|
255
|
centers[2] = object.getCubitFaceStickerIndex( 7,0); // UB
|
256
|
centers[3] = object.getCubitFaceStickerIndex( 8,0); // UL
|
257
|
centers[4] = object.getCubitFaceStickerIndex(13,0); // DF
|
258
|
centers[5] = object.getCubitFaceStickerIndex(10,1); // DR
|
259
|
centers[6] = object.getCubitFaceStickerIndex(11,0); // DB
|
260
|
centers[7] = object.getCubitFaceStickerIndex(12,0); // DL
|
261
|
}
|
262
|
|
263
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
264
|
|
265
|
public int tablebaseIndex(TwistyObject object)
|
266
|
{
|
267
|
int[][] corners = new int[6][4];
|
268
|
int[] centers = new int[8];
|
269
|
int[] face_colors = new int[8];
|
270
|
|
271
|
getCorners(object,corners);
|
272
|
getCenters(object,centers);
|
273
|
|
274
|
int result1 = checkAllCentersPresent(centers);
|
275
|
if( result1<0 ) return result1;
|
276
|
|
277
|
int result2 = figureOutFaceColors(face_colors,centers,corners);
|
278
|
if( result2<0 ) return result2;
|
279
|
|
280
|
int result3 = checkAllColorsDifferent(face_colors);
|
281
|
if( result3<0 ) return result3;
|
282
|
|
283
|
int[] corners_perm = getCornersPermutation(face_colors, corners);
|
284
|
boolean even1 = TablebaseHelpers.permutationIsEven(corners_perm);
|
285
|
if( !even1 ) return ERROR_TWO_CORNERS;
|
286
|
|
287
|
int[] free_centers_perm = getFreeCentersPermutation(face_colors, centers);
|
288
|
boolean even2 = TablebaseHelpers.permutationIsEven(free_centers_perm);
|
289
|
if( !even2 ) return ERROR_TWO_CENTERS;
|
290
|
|
291
|
int[] corners_twist = getCornersTwist(face_colors, corners);
|
292
|
int totalTwist = getTotalTwist(corners_twist);
|
293
|
if( totalTwist<0 ) return ERROR_CORNER_TWIST;
|
294
|
|
295
|
int corners_perm_num = TablebaseHelpers.computeEvenPermutationNum(corners_perm);
|
296
|
int centers_perm_num = TablebaseHelpers.computeEvenPermutationNum(free_centers_perm);
|
297
|
|
298
|
return centers_perm_num + 12*(totalTwist + 128*corners_perm_num);
|
299
|
}
|
300
|
|
301
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
302
|
|
303
|
public String error(int index, Resources res)
|
304
|
{
|
305
|
switch(index)
|
306
|
{
|
307
|
case ERROR_CORNER_FR_MISSING:
|
308
|
case ERROR_CORNER_BR_MISSING:
|
309
|
case ERROR_CORNER_BL_MISSING:
|
310
|
case ERROR_CORNER_FL_MISSING:
|
311
|
case ERROR_CORNER_TO_MISSING:
|
312
|
case ERROR_CORNER_BO_MISSING:
|
313
|
|
314
|
case ERROR_CENTER_0_MISSING :
|
315
|
case ERROR_CENTER_1_MISSING :
|
316
|
case ERROR_CENTER_2_MISSING :
|
317
|
case ERROR_CENTER_3_MISSING :
|
318
|
case ERROR_CENTER_4_MISSING :
|
319
|
case ERROR_CENTER_5_MISSING :
|
320
|
case ERROR_CENTER_6_MISSING :
|
321
|
case ERROR_CENTER_7_MISSING :
|
322
|
|
323
|
case ERROR_TWO_CORNERS :
|
324
|
case ERROR_TWO_CENTERS :
|
325
|
case ERROR_CORNER_TWIST :
|
326
|
case ERROR_CORNERS_CANNOT :
|
327
|
}
|
328
|
|
329
|
return null;
|
330
|
}
|
331
|
|
332
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
333
|
|
334
|
public int[][] solution(int index, Resources res)
|
335
|
{
|
336
|
if( mSolver==null )
|
337
|
{
|
338
|
mSolver = ImplementedTablebasesList.createUnpacked(ObjectType.DIAM_2);
|
339
|
if( mSolver!=null ) mSolver.createTablebase();
|
340
|
}
|
341
|
|
342
|
return mSolver!=null ? mSolver.solution(index) : null;
|
343
|
}
|
344
|
}
|
345
|
|