|
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.TwistyObject;
|
|
17
|
import org.distorted.objectlib.tablebases.TablebaseHelpers;
|
|
18
|
|
|
19
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
20
|
|
|
21
|
public class SolverTablebaseCU232 extends SolverTablebase
|
|
22
|
{
|
|
23
|
private static final int ERROR_CORNER_MISSING = -1;
|
|
24
|
private static final int ERROR_EDGE_MISSING = -2;
|
|
25
|
private static final int ERROR_CORNERS_CANNOT = -3;
|
|
26
|
private static final int ERROR_EDGE_TWISTED = -4;
|
|
27
|
private static final int ERROR_CORNER_TWISTED = -5;
|
|
28
|
|
|
29
|
private final int[] mFaceColors;
|
|
30
|
private int mErrColor1, mErrColor2, mErrColor3;
|
|
31
|
|
|
32
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
33
|
|
|
34
|
private int edgeIs(int[] edge, int i0, int i1)
|
|
35
|
{
|
|
36
|
int c0 = mFaceColors[i0];
|
|
37
|
int c1 = mFaceColors[i1];
|
|
38
|
|
|
39
|
if( edge[0]==c0 && edge[1]==c1 ) return 0;
|
|
40
|
if( edge[0]==c1 && edge[1]==c0 ) return 1;
|
|
41
|
return 2;
|
|
42
|
}
|
|
43
|
|
|
44
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
45
|
|
|
46
|
private int retEdgePermutation(int[] output, int[][] edges)
|
|
47
|
{
|
|
48
|
for(int i=0; i<4; i++) output[i] = -1;
|
|
49
|
|
|
50
|
for(int i=0; i<4; i++)
|
|
51
|
{
|
|
52
|
int edge0 = edgeIs(edges[i],1,5);
|
|
53
|
if( edge0==0 ) output[0]=i;
|
|
54
|
else if( edge0==1 ) return ERROR_EDGE_TWISTED;
|
|
55
|
|
|
56
|
int edge1 = edgeIs(edges[i],1,4);
|
|
57
|
if( edge1==0 ) output[1]=i;
|
|
58
|
else if( edge1==1 ) return ERROR_EDGE_TWISTED;
|
|
59
|
|
|
60
|
int edge2 = edgeIs(edges[i],0,5);
|
|
61
|
if( edge2==0 ) output[2]=i;
|
|
62
|
else if( edge2==1 ) return ERROR_EDGE_TWISTED;
|
|
63
|
|
|
64
|
int edge3 = edgeIs(edges[i],0,4);
|
|
65
|
if( edge3==0 ) output[3]=i;
|
|
66
|
else if( edge3==1 ) return ERROR_EDGE_TWISTED;
|
|
67
|
}
|
|
68
|
|
|
69
|
if( output[0]==-1 ) { mErrColor1=1; mErrColor2=5; return ERROR_EDGE_MISSING; }
|
|
70
|
if( output[1]==-1 ) { mErrColor1=1; mErrColor2=4; return ERROR_EDGE_MISSING; }
|
|
71
|
if( output[2]==-1 ) { mErrColor1=0; mErrColor2=5; return ERROR_EDGE_MISSING; }
|
|
72
|
if( output[3]==-1 ) { mErrColor1=0; mErrColor2=4; return ERROR_EDGE_MISSING; }
|
|
73
|
|
|
74
|
return 0;
|
|
75
|
}
|
|
76
|
|
|
77
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
78
|
|
|
79
|
private int cornerIs(int[] corner, int i0, int i1, int i2)
|
|
80
|
{
|
|
81
|
int c0 = mFaceColors[i0];
|
|
82
|
int c1 = mFaceColors[i1];
|
|
83
|
int c2 = mFaceColors[i2];
|
|
84
|
|
|
85
|
if( corner[0]==c0 && corner[1]==c1 && corner[2]==c2 ) return 0;
|
|
86
|
|
|
87
|
if( corner[0]==c0 && corner[1]==c2 && corner[2]==c1 ||
|
|
88
|
corner[0]==c1 && corner[1]==c0 && corner[2]==c2 ||
|
|
89
|
corner[0]==c1 && corner[1]==c2 && corner[2]==c0 ||
|
|
90
|
corner[0]==c2 && corner[1]==c0 && corner[2]==c1 ||
|
|
91
|
corner[0]==c2 && corner[1]==c1 && corner[2]==c0 ) return 1;
|
|
92
|
|
|
93
|
return 2;
|
|
94
|
}
|
|
95
|
|
|
96
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
97
|
|
|
98
|
private int retCornerPermutation(int[] output, int[][] corners)
|
|
99
|
{
|
|
100
|
for(int i=0; i<8; i++) output[i] = -1;
|
|
101
|
|
|
102
|
for(int i=0; i<8; i++)
|
|
103
|
{
|
|
104
|
int corner7 = cornerIs(corners[i],2,4,0);
|
|
105
|
if( corner7==0 ) output[7]=i;
|
|
106
|
else if( corner7==1 ) return ERROR_CORNER_TWISTED;
|
|
107
|
|
|
108
|
int corner6 = cornerIs(corners[i],2,0,5);
|
|
109
|
if( corner6==0 ) output[6]=i;
|
|
110
|
else if( corner6==1 ) return ERROR_CORNER_TWISTED;
|
|
111
|
|
|
112
|
int corner5 = cornerIs(corners[i],3,0,4);
|
|
113
|
if( corner5==0 ) output[5]=i;
|
|
114
|
else if( corner5==1 ) return ERROR_CORNER_TWISTED;
|
|
115
|
|
|
116
|
int corner4 = cornerIs(corners[i],3,5,0);
|
|
117
|
if( corner4==0 ) output[4]=i;
|
|
118
|
else if( corner4==1 ) return ERROR_CORNER_TWISTED;
|
|
119
|
|
|
120
|
int corner3 = cornerIs(corners[i],2,1,4);
|
|
121
|
if( corner3==0 ) output[3]=i;
|
|
122
|
else if( corner3==1 ) return ERROR_CORNER_TWISTED;
|
|
123
|
|
|
124
|
int corner2 = cornerIs(corners[i],2,5,1);
|
|
125
|
if( corner2==0 ) output[2]=i;
|
|
126
|
else if( corner2==1 ) return ERROR_CORNER_TWISTED;
|
|
127
|
|
|
128
|
int corner1 = cornerIs(corners[i],3,4,1);
|
|
129
|
if( corner1==0 ) output[1]=i;
|
|
130
|
else if( corner1==1 ) return ERROR_CORNER_TWISTED;
|
|
131
|
|
|
132
|
int corner0 = cornerIs(corners[i],3,1,5);
|
|
133
|
if( corner0==0 ) output[0]=i;
|
|
134
|
else if( corner0==1 ) return ERROR_CORNER_TWISTED;
|
|
135
|
}
|
|
136
|
|
|
137
|
if( output[0]==-1 ) { mErrColor1=1; mErrColor2=3; mErrColor3=5; return ERROR_CORNER_MISSING; }
|
|
138
|
if( output[1]==-1 ) { mErrColor1=1; mErrColor2=3; mErrColor3=4; return ERROR_CORNER_MISSING; }
|
|
139
|
if( output[2]==-1 ) { mErrColor1=1; mErrColor2=2; mErrColor3=5; return ERROR_CORNER_MISSING; }
|
|
140
|
if( output[3]==-1 ) { mErrColor1=1; mErrColor2=3; mErrColor3=4; return ERROR_CORNER_MISSING; }
|
|
141
|
if( output[4]==-1 ) { mErrColor1=0; mErrColor2=3; mErrColor3=5; return ERROR_CORNER_MISSING; }
|
|
142
|
if( output[5]==-1 ) { mErrColor1=0; mErrColor2=3; mErrColor3=4; return ERROR_CORNER_MISSING; }
|
|
143
|
if( output[6]==-1 ) { mErrColor1=0; mErrColor2=2; mErrColor3=5; return ERROR_CORNER_MISSING; }
|
|
144
|
if( output[7]==-1 ) { mErrColor1=0; mErrColor2=2; mErrColor3=4; return ERROR_CORNER_MISSING; }
|
|
145
|
|
|
146
|
return 0;
|
|
147
|
}
|
|
148
|
|
|
149
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
150
|
|
|
151
|
private int computeFaceColors(int[][] corners, int[][] edges)
|
|
152
|
{
|
|
153
|
mFaceColors[1] = edges[1][0];
|
|
154
|
mFaceColors[4] = edges[1][1];
|
|
155
|
|
|
156
|
if( edges[0][0]==mFaceColors[1] ) mFaceColors[5] = edges[0][1];
|
|
157
|
else if( edges[2][0]==mFaceColors[1] ) mFaceColors[5] = edges[2][1];
|
|
158
|
else if( edges[3][0]==mFaceColors[1] ) mFaceColors[5] = edges[3][1];
|
|
159
|
else return ERROR_EDGE_TWISTED;
|
|
160
|
|
|
161
|
if( edges[0][1]==mFaceColors[4] ) mFaceColors[0] = edges[0][0];
|
|
162
|
else if( edges[2][1]==mFaceColors[4] ) mFaceColors[0] = edges[2][0];
|
|
163
|
else if( edges[3][1]==mFaceColors[4] ) mFaceColors[0] = edges[3][0];
|
|
164
|
else return ERROR_EDGE_TWISTED;
|
|
165
|
|
|
166
|
boolean found2 = false;
|
|
167
|
boolean found3 = false;
|
|
168
|
|
|
169
|
for(int c=0; c<8; c++)
|
|
170
|
{
|
|
171
|
if( !found3 && corners[c][1]==mFaceColors[4] && corners[c][2]==mFaceColors[1] )
|
|
172
|
{
|
|
173
|
found3=true;
|
|
174
|
mFaceColors[3] = corners[c][0];
|
|
175
|
}
|
|
176
|
if( !found2 && corners[c][1]==mFaceColors[1] && corners[c][2]==mFaceColors[4] )
|
|
177
|
{
|
|
178
|
found2=true;
|
|
179
|
mFaceColors[2] = corners[c][0];
|
|
180
|
}
|
|
181
|
}
|
|
182
|
|
|
183
|
if( !found2 || !found3 ) return ERROR_CORNERS_CANNOT;
|
|
184
|
|
|
185
|
for(int i=0; i<6; i++)
|
|
186
|
for(int j=i+1; j<6; j++)
|
|
187
|
if( mFaceColors[i]==mFaceColors[j] ) return ERROR_CORNERS_CANNOT;
|
|
188
|
|
|
189
|
return 0;
|
|
190
|
}
|
|
191
|
|
|
192
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
193
|
|
|
194
|
private int[] correctEdgePerm(int[] perm)
|
|
195
|
{
|
|
196
|
int[] ret = new int[3];
|
|
197
|
|
|
198
|
ret[0] = perm[0];
|
|
199
|
ret[1] = perm[2];
|
|
200
|
ret[2] = perm[3];
|
|
201
|
|
|
202
|
if( ret[0]>1 ) ret[0]--;
|
|
203
|
if( ret[1]>1 ) ret[1]--;
|
|
204
|
if( ret[2]>1 ) ret[2]--;
|
|
205
|
|
|
206
|
return ret;
|
|
207
|
}
|
|
208
|
|
|
209
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
210
|
|
|
211
|
public SolverTablebaseCU232(OperatingSystemInterface os, Resources res, TwistyObject object)
|
|
212
|
{
|
|
213
|
super(os,res,object);
|
|
214
|
mFaceColors = new int[6];
|
|
215
|
}
|
|
216
|
|
|
217
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
218
|
|
|
219
|
public int tablebaseIndex(TwistyObject object)
|
|
220
|
{
|
|
221
|
int[][] corners= new int[8][3];
|
|
222
|
int[][] edges = new int[4][2];
|
|
223
|
|
|
224
|
corners[0][0] = object.getCubitFaceStickerIndex(0,3);
|
|
225
|
corners[0][1] = object.getCubitFaceStickerIndex(0,1);
|
|
226
|
corners[0][2] = object.getCubitFaceStickerIndex(0,5);
|
|
227
|
|
|
228
|
corners[1][0] = object.getCubitFaceStickerIndex(1,1);
|
|
229
|
corners[1][1] = object.getCubitFaceStickerIndex(1,5);
|
|
230
|
corners[1][2] = object.getCubitFaceStickerIndex(1,3);
|
|
231
|
|
|
232
|
corners[2][0] = object.getCubitFaceStickerIndex(2,3);
|
|
233
|
corners[2][1] = object.getCubitFaceStickerIndex(2,1);
|
|
234
|
corners[2][2] = object.getCubitFaceStickerIndex(2,5);
|
|
235
|
|
|
236
|
corners[3][0] = object.getCubitFaceStickerIndex(3,3);
|
|
237
|
corners[3][1] = object.getCubitFaceStickerIndex(3,1);
|
|
238
|
corners[3][2] = object.getCubitFaceStickerIndex(3,5);
|
|
239
|
|
|
240
|
corners[4][0] = object.getCubitFaceStickerIndex(4,5);
|
|
241
|
corners[4][1] = object.getCubitFaceStickerIndex(4,3);
|
|
242
|
corners[4][2] = object.getCubitFaceStickerIndex(4,1);
|
|
243
|
|
|
244
|
corners[5][0] = object.getCubitFaceStickerIndex(5,3);
|
|
245
|
corners[5][1] = object.getCubitFaceStickerIndex(5,1);
|
|
246
|
corners[5][2] = object.getCubitFaceStickerIndex(5,5);
|
|
247
|
|
|
248
|
corners[6][0] = object.getCubitFaceStickerIndex(6,3);
|
|
249
|
corners[6][1] = object.getCubitFaceStickerIndex(6,1);
|
|
250
|
corners[6][2] = object.getCubitFaceStickerIndex(6,5);
|
|
251
|
|
|
252
|
corners[7][0] = object.getCubitFaceStickerIndex(7,5);
|
|
253
|
corners[7][1] = object.getCubitFaceStickerIndex(7,3);
|
|
254
|
corners[7][2] = object.getCubitFaceStickerIndex(7,1);
|
|
255
|
|
|
256
|
edges[0][0] = object.getCubitFaceStickerIndex(8,5);
|
|
257
|
edges[0][1] = object.getCubitFaceStickerIndex(8,3);
|
|
258
|
edges[1][0] = object.getCubitFaceStickerIndex(9,3);
|
|
259
|
edges[1][1] = object.getCubitFaceStickerIndex(9,5);
|
|
260
|
edges[2][0] = object.getCubitFaceStickerIndex(10,5);
|
|
261
|
edges[2][1] = object.getCubitFaceStickerIndex(10,3);
|
|
262
|
edges[3][0] = object.getCubitFaceStickerIndex(11,3);
|
|
263
|
edges[3][1] = object.getCubitFaceStickerIndex(11,5);
|
|
264
|
|
|
265
|
int result0 = computeFaceColors(corners, edges);
|
|
266
|
if( result0<0 ) return result0;
|
|
267
|
|
|
268
|
int[] corner_perm = new int[8];
|
|
269
|
int result1 = retCornerPermutation(corner_perm,corners);
|
|
270
|
if( result1<0 ) return result1;
|
|
271
|
|
|
272
|
int[] edge_perm = new int[4];
|
|
273
|
int result2 = retEdgePermutation(edge_perm,edges);
|
|
274
|
if( result2<0 ) return result2;
|
|
275
|
|
|
276
|
int[] edge_perm2 = correctEdgePerm(edge_perm); // edge1 is fixed!
|
|
277
|
|
|
278
|
int corner_perm_num = TablebaseHelpers.computePermutationNum(corner_perm);
|
|
279
|
int edge_perm_num = TablebaseHelpers.computePermutationNum(edge_perm2);
|
|
280
|
|
|
281
|
return edge_perm_num + 6*corner_perm_num;
|
|
282
|
}
|
|
283
|
|
|
284
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
285
|
|
|
286
|
private String edgeError(Resources res, int face0, int face1)
|
|
287
|
{
|
|
288
|
int j0 = getHexColor(face0,3);
|
|
289
|
int j1 = getHexColor(face1,6);
|
|
290
|
|
|
291
|
String c0 = res.getString(j0);
|
|
292
|
String c1 = res.getString(j1);
|
|
293
|
|
|
294
|
return res.getString(R.string.solver_generic_missing_edge,c0,c1);
|
|
295
|
}
|
|
296
|
|
|
297
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
298
|
|
|
299
|
private String cornerError(Resources res, int face0, int face1, int face2)
|
|
300
|
{
|
|
301
|
int j0 = getHexColor(face0,3);
|
|
302
|
int j1 = getHexColor(face1,3);
|
|
303
|
int j2 = getHexColor(face2,4);
|
|
304
|
|
|
305
|
String c0 = res.getString(j0);
|
|
306
|
String c1 = res.getString(j1);
|
|
307
|
String c2 = res.getString(j2);
|
|
308
|
|
|
309
|
return res.getString(R.string.solver_generic_missing_corner,c0,c1,c2);
|
|
310
|
}
|
|
311
|
|
|
312
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
313
|
|
|
314
|
public String error(int index, Resources res)
|
|
315
|
{
|
|
316
|
switch(index)
|
|
317
|
{
|
|
318
|
case ERROR_CORNER_MISSING: return cornerError(res,mErrColor1,mErrColor2,mErrColor3);
|
|
319
|
case ERROR_EDGE_MISSING : return edgeError(res,mErrColor1,mErrColor2);
|
|
320
|
case ERROR_CORNERS_CANNOT: return res.getString(R.string.solver_generic_corners_cannot);
|
|
321
|
case ERROR_EDGE_TWISTED : return res.getString(R.string.solver_generic_edge_twist);
|
|
322
|
case ERROR_CORNER_TWISTED: return res.getString(R.string.solver_generic_corner_twist);
|
|
323
|
}
|
|
324
|
|
|
325
|
return null;
|
|
326
|
}
|
|
327
|
}
|
|
328
|
|