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.TablebaseHelpers;
|
20
|
import org.distorted.objectlib.tablebases.TablebasesAbstract;
|
21
|
import org.distorted.objectlib.tablebases.TBCube2;
|
22
|
|
23
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
24
|
|
25
|
public class SolverCube2 extends SolverTablebase
|
26
|
{
|
27
|
private static final int ERROR_CORNER_135_MISSING = -1;
|
28
|
private static final int ERROR_CORNER_134_MISSING = -2;
|
29
|
private static final int ERROR_CORNER_125_MISSING = -3;
|
30
|
private static final int ERROR_CORNER_124_MISSING = -4;
|
31
|
private static final int ERROR_CORNER_035_MISSING = -5;
|
32
|
private static final int ERROR_CORNER_034_MISSING = -6;
|
33
|
private static final int ERROR_CORNER_025_MISSING = -7;
|
34
|
private static final int ERROR_CORNER_024_MISSING = -8;
|
35
|
private static final int ERROR_CORNERS_CANNOT = -9;
|
36
|
private static final int ERROR_CORNER_TWISTED = -10;
|
37
|
|
38
|
TablebasesAbstract mSolver;
|
39
|
private final int[] mFaceColors;
|
40
|
|
41
|
////////////////////////////////////////////////////////////////////////////////////////
|
42
|
|
43
|
private void fillCornerTwists(int[] output, int[][] corners, int[] perm)
|
44
|
{
|
45
|
for(int i=0; i<8; i++)
|
46
|
{
|
47
|
int[] c = corners[perm[i]];
|
48
|
|
49
|
if( c[0]==mFaceColors[0] || c[0]==mFaceColors[1] ) output[i] = 0;
|
50
|
else if( c[1]==mFaceColors[0] || c[1]==mFaceColors[1] ) output[i] = 1;
|
51
|
else output[i] = 2;
|
52
|
}
|
53
|
}
|
54
|
|
55
|
////////////////////////////////////////////////////////////////////////////////////////
|
56
|
|
57
|
private int cornerIndex(int[][] corners, int i1, int i2, int i3)
|
58
|
{
|
59
|
int c1 = mFaceColors[i1];
|
60
|
int c2 = mFaceColors[i2];
|
61
|
int c3 = mFaceColors[i3];
|
62
|
|
63
|
for(int i=0; i<8; i++)
|
64
|
{
|
65
|
int[] cor = corners[i];
|
66
|
|
67
|
if( (cor[0]==c1 && cor[1]==c2 && cor[2]==c3) ||
|
68
|
(cor[0]==c2 && cor[1]==c3 && cor[2]==c1) ||
|
69
|
(cor[0]==c3 && cor[1]==c1 && cor[2]==c2) ) return i;
|
70
|
}
|
71
|
|
72
|
return -1;
|
73
|
}
|
74
|
|
75
|
////////////////////////////////////////////////////////////////////////////////////////
|
76
|
|
77
|
private int retCornerPermutation(int[] output, int[][] corners)
|
78
|
{
|
79
|
for(int i=0; i<8; i++) output[i] = -1;
|
80
|
|
81
|
output[0] = cornerIndex(corners, 1,2,5);
|
82
|
output[1] = cornerIndex(corners, 1,4,2);
|
83
|
output[2] = cornerIndex(corners, 1,5,3);
|
84
|
output[3] = cornerIndex(corners, 1,3,4);
|
85
|
output[4] = cornerIndex(corners, 0,5,2);
|
86
|
output[5] = cornerIndex(corners, 0,2,4);
|
87
|
output[6] = cornerIndex(corners, 0,3,5);
|
88
|
output[7] = cornerIndex(corners, 0,4,3);
|
89
|
|
90
|
if( output[0]==-1 ) return ERROR_CORNER_125_MISSING;
|
91
|
if( output[1]==-1 ) return ERROR_CORNER_124_MISSING;
|
92
|
if( output[2]==-1 ) return ERROR_CORNER_135_MISSING;
|
93
|
if( output[3]==-1 ) return ERROR_CORNER_134_MISSING;
|
94
|
if( output[4]==-1 ) return ERROR_CORNER_025_MISSING;
|
95
|
if( output[5]==-1 ) return ERROR_CORNER_024_MISSING;
|
96
|
if( output[6]==-1 ) return ERROR_CORNER_035_MISSING;
|
97
|
if( output[7]==-1 ) return ERROR_CORNER_034_MISSING;
|
98
|
|
99
|
return 0;
|
100
|
}
|
101
|
|
102
|
////////////////////////////////////////////////////////////////////////////////////////
|
103
|
|
104
|
private int findColor(int[][] corners, int c1, int c2)
|
105
|
{
|
106
|
for(int i=0; i<8; i++)
|
107
|
{
|
108
|
int[] cor = corners[i];
|
109
|
|
110
|
if( cor[0]==c1 && cor[1]==c2 ) return cor[2];
|
111
|
if( cor[1]==c1 && cor[2]==c2 ) return cor[0];
|
112
|
if( cor[2]==c1 && cor[0]==c2 ) return cor[1];
|
113
|
}
|
114
|
|
115
|
return -1;
|
116
|
}
|
117
|
|
118
|
////////////////////////////////////////////////////////////////////////////////////////
|
119
|
|
120
|
private int computeFaceColors(int[][] corners)
|
121
|
{
|
122
|
mFaceColors[1] = corners[1][0];
|
123
|
mFaceColors[2] = corners[1][2];
|
124
|
mFaceColors[4] = corners[1][1];
|
125
|
|
126
|
mFaceColors[0] = findColor(corners,mFaceColors[2],mFaceColors[4]);
|
127
|
mFaceColors[3] = findColor(corners,mFaceColors[4],mFaceColors[1]);
|
128
|
mFaceColors[5] = findColor(corners,mFaceColors[1],mFaceColors[2]);
|
129
|
|
130
|
for(int i=0; i<6; i++)
|
131
|
{
|
132
|
int color = mFaceColors[i];
|
133
|
if( color<0 ) return ERROR_CORNERS_CANNOT;
|
134
|
|
135
|
for(int j=i+1; j<6; j++)
|
136
|
if( mFaceColors[j]==color ) return ERROR_CORNERS_CANNOT;
|
137
|
}
|
138
|
|
139
|
return 0;
|
140
|
}
|
141
|
|
142
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
143
|
|
144
|
public SolverCube2(OperatingSystemInterface os, Resources res, TwistyObject object)
|
145
|
{
|
146
|
super(os,res,object);
|
147
|
mFaceColors = new int[6];
|
148
|
}
|
149
|
|
150
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
151
|
|
152
|
private void getCorners(TwistyObject object, int[][] corners)
|
153
|
{
|
154
|
corners[0][0] = object.getCubitFaceStickerIndex(0,1);
|
155
|
corners[0][1] = object.getCubitFaceStickerIndex(0,3);
|
156
|
corners[0][2] = object.getCubitFaceStickerIndex(0,5);
|
157
|
|
158
|
corners[1][0] = object.getCubitFaceStickerIndex(1,3);
|
159
|
corners[1][1] = object.getCubitFaceStickerIndex(1,5);
|
160
|
corners[1][2] = object.getCubitFaceStickerIndex(1,1);
|
161
|
|
162
|
corners[2][0] = object.getCubitFaceStickerIndex(2,5);
|
163
|
corners[2][1] = object.getCubitFaceStickerIndex(2,1);
|
164
|
corners[2][2] = object.getCubitFaceStickerIndex(2,3);
|
165
|
|
166
|
corners[3][0] = object.getCubitFaceStickerIndex(3,1);
|
167
|
corners[3][1] = object.getCubitFaceStickerIndex(3,3);
|
168
|
corners[3][2] = object.getCubitFaceStickerIndex(3,5);
|
169
|
|
170
|
corners[4][0] = object.getCubitFaceStickerIndex(4,1);
|
171
|
corners[4][1] = object.getCubitFaceStickerIndex(4,3);
|
172
|
corners[4][2] = object.getCubitFaceStickerIndex(4,5);
|
173
|
|
174
|
corners[5][0] = object.getCubitFaceStickerIndex(5,1);
|
175
|
corners[5][1] = object.getCubitFaceStickerIndex(5,3);
|
176
|
corners[5][2] = object.getCubitFaceStickerIndex(5,5);
|
177
|
|
178
|
corners[6][0] = object.getCubitFaceStickerIndex(6,1);
|
179
|
corners[6][1] = object.getCubitFaceStickerIndex(6,3);
|
180
|
corners[6][2] = object.getCubitFaceStickerIndex(6,5);
|
181
|
|
182
|
corners[7][0] = object.getCubitFaceStickerIndex(7,1);
|
183
|
corners[7][1] = object.getCubitFaceStickerIndex(7,3);
|
184
|
corners[7][2] = object.getCubitFaceStickerIndex(7,5);
|
185
|
}
|
186
|
|
187
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
188
|
|
189
|
public int tablebaseIndex(TwistyObject object)
|
190
|
{
|
191
|
int[][] corners= new int[8][3];
|
192
|
getCorners(object,corners);
|
193
|
|
194
|
int result0 = computeFaceColors(corners);
|
195
|
if( result0<0 ) return result0;
|
196
|
|
197
|
int[] perm = new int[8];
|
198
|
int result1 = retCornerPermutation(perm,corners);
|
199
|
if( result1<0 ) return result1;
|
200
|
|
201
|
int[] perm7 = TBCube2.shrinkPerm(perm);
|
202
|
int permNum = TablebaseHelpers.computePermutationNum(perm7);
|
203
|
int[] twist = new int[8];
|
204
|
fillCornerTwists(twist,corners,perm);
|
205
|
|
206
|
int totalTwist = 0;
|
207
|
for(int i=0; i<8; i++) totalTwist += twist[i];
|
208
|
if( ((totalTwist)%3) != 0 ) return ERROR_CORNER_TWISTED;
|
209
|
|
210
|
int twistNum = twist[0] + 3*(twist[2] + 3*(twist[3] + 3*(twist[4] + 3*(twist[5] + 3*twist[6]))));
|
211
|
|
212
|
return twistNum + 729*permNum;
|
213
|
}
|
214
|
|
215
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
216
|
|
217
|
private int getColorIndex4(int face)
|
218
|
{
|
219
|
switch(mFaceColors[face])
|
220
|
{
|
221
|
case 0: return R.string.color_yellow4;
|
222
|
case 1: return R.string.color_white4;
|
223
|
case 2: return R.string.color_blue4;
|
224
|
case 3: return R.string.color_green4;
|
225
|
case 4: return R.string.color_red4;
|
226
|
case 5: return R.string.color_orange4;
|
227
|
}
|
228
|
|
229
|
return -1;
|
230
|
}
|
231
|
|
232
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
233
|
|
234
|
private int getColorIndex3(int face)
|
235
|
{
|
236
|
switch(mFaceColors[face])
|
237
|
{
|
238
|
case 0: return R.string.color_yellow3;
|
239
|
case 1: return R.string.color_white3;
|
240
|
case 2: return R.string.color_blue3;
|
241
|
case 3: return R.string.color_green3;
|
242
|
case 4: return R.string.color_red3;
|
243
|
case 5: return R.string.color_orange3;
|
244
|
}
|
245
|
|
246
|
return -1;
|
247
|
}
|
248
|
|
249
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
250
|
|
251
|
private String cornerError(Resources res, int face0, int face1, int face2)
|
252
|
{
|
253
|
int j0 = getColorIndex3(face0);
|
254
|
int j1 = getColorIndex3(face1);
|
255
|
int j2 = getColorIndex4(face2);
|
256
|
|
257
|
String c0 = res.getString(j0);
|
258
|
String c1 = res.getString(j1);
|
259
|
String c2 = res.getString(j2);
|
260
|
|
261
|
return res.getString(R.string.solver_generic_missing_corner,c0,c1,c2);
|
262
|
}
|
263
|
|
264
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
265
|
|
266
|
public String error(int index, Resources res)
|
267
|
{
|
268
|
switch(index)
|
269
|
{
|
270
|
case ERROR_CORNER_135_MISSING: return cornerError(res,1,3,5);
|
271
|
case ERROR_CORNER_134_MISSING: return cornerError(res,1,3,4);
|
272
|
case ERROR_CORNER_125_MISSING: return cornerError(res,1,2,5);
|
273
|
case ERROR_CORNER_124_MISSING: return cornerError(res,1,2,4);
|
274
|
case ERROR_CORNER_035_MISSING: return cornerError(res,0,3,5);
|
275
|
case ERROR_CORNER_034_MISSING: return cornerError(res,0,3,4);
|
276
|
case ERROR_CORNER_025_MISSING: return cornerError(res,0,2,5);
|
277
|
case ERROR_CORNER_024_MISSING: return cornerError(res,0,2,4);
|
278
|
case ERROR_CORNERS_CANNOT : return res.getString(R.string.solver_generic_corners_cannot);
|
279
|
case ERROR_CORNER_TWISTED : return res.getString(R.string.solver_generic_corner_twist);
|
280
|
}
|
281
|
|
282
|
return null;
|
283
|
}
|
284
|
|
285
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
286
|
|
287
|
public int[][] solution(int index, OperatingSystemInterface os)
|
288
|
{
|
289
|
if( mSolver==null )
|
290
|
{
|
291
|
mSolver = ImplementedTablebasesList.createPacked(os, ListObjects.CUBE_2.name() );
|
292
|
}
|
293
|
|
294
|
return mSolver!=null ? mSolver.solution(index,null,os) : null;
|
295
|
}
|
296
|
}
|
297
|
|