Project

General

Profile

Download (13.5 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / solvers / SolverSkewb.java @ db4775f7

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 static org.distorted.objectlib.tablebases.TBSkewb.FREE;
13

    
14
import android.content.res.Resources;
15

    
16
import org.distorted.main.R;
17
import org.distorted.objectlib.main.ObjectSignatures;
18
import org.distorted.objectlib.main.TwistyObject;
19
import org.distorted.objectlib.tablebases.ImplementedTablebasesList;
20
import org.distorted.objectlib.tablebases.TBSkewb;
21
import org.distorted.objectlib.tablebases.TablebaseHelpers;
22
import org.distorted.objectlib.tablebases.TablebasesAbstract;
23

    
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25

    
26
public class SolverSkewb extends SolverTablebase
27
{
28
  private static final int ERROR_CORNER_135_MISSING = -1;
29
  private static final int ERROR_CORNER_134_MISSING = -2;
30
  private static final int ERROR_CORNER_125_MISSING = -3;
31
  private static final int ERROR_CORNER_124_MISSING = -4;
32
  private static final int ERROR_CORNER_035_MISSING = -5;
33
  private static final int ERROR_CORNER_034_MISSING = -6;
34
  private static final int ERROR_CORNER_025_MISSING = -7;
35
  private static final int ERROR_CORNER_024_MISSING = -8;
36

    
37
  private static final int ERROR_CENTER_0_MISSING   = -9;
38
  private static final int ERROR_CENTER_1_MISSING   = -10;
39
  private static final int ERROR_CENTER_2_MISSING   = -11;
40
  private static final int ERROR_CENTER_3_MISSING   = -12;
41
  private static final int ERROR_CENTER_4_MISSING   = -13;
42
  private static final int ERROR_CENTER_5_MISSING   = -14;
43

    
44
  private static final int ERROR_CORNERS_CANNOT     = -15;
45
  private static final int ERROR_CORNER_TWISTED     = -16;
46
  private static final int ERROR_TWO_CENTERS        = -17;
47
  private static final int ERROR_TWO_CORNERS        = -18;
48

    
49
  private TablebasesAbstract mSolver;
50
  private final int[] mFaceColors;
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

    
54
  public SolverSkewb(Resources res, TwistyObject object)
55
    {
56
    super(res,object);
57
    mFaceColors = new int[6];
58
    }
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
  private void getCorners(TwistyObject object, int[][] corners)
63
    {
64
    for(int i=0; i<8; i++)
65
      for(int j=0; j<3; j++)
66
        corners[i][j] = object.getCubitFaceStickerIndex(i,j);
67
    }
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

    
71
  private int getCenters(TwistyObject object, int[] output)
72
    {
73
    final int[] map = {4,5,2,3,0,1};
74
    int[] inverted_perm = new int[6];
75
    boolean[] present = new boolean[6];
76

    
77
    for(int i=0; i<6; i++)
78
      {
79
      int color = object.getCubitFaceStickerIndex(i+8,0) - 6;
80
      present[color] = true;
81
      inverted_perm[i] = map[mFaceColors[color]];
82
      }
83

    
84
    if( !present[0] ) return ERROR_CENTER_0_MISSING;
85
    if( !present[1] ) return ERROR_CENTER_1_MISSING;
86
    if( !present[2] ) return ERROR_CENTER_2_MISSING;
87
    if( !present[3] ) return ERROR_CENTER_3_MISSING;
88
    if( !present[4] ) return ERROR_CENTER_4_MISSING;
89
    if( !present[5] ) return ERROR_CENTER_5_MISSING;
90

    
91
    TablebaseHelpers.invertPermutation(inverted_perm,output);
92
    return 0;
93
    }
94

    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96

    
97
  private int commonCornerColor(int[] c1, int[] c2)
98
    {
99
    int theSame = 0;
100
    int index   = 0;
101

    
102
    for(int i=0; i<3; i++)
103
      for(int j=0; j<3; j++)
104
        if( c1[i]==c2[j] )
105
          {
106
          index = i;
107
          theSame++;
108
          }
109

    
110
    return theSame==1 ? c1[index] : ERROR_CORNERS_CANNOT;
111
    }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114

    
115
  private int computeFaceColors(int[][] corners, int[] output)
116
    {
117
    final int[][] corner_indices = { {0,3},{5,6},{0,5},{6,3},{0,6},{3,5} };
118

    
119
    for(int i=0; i<6; i++)
120
      {
121
      int c1 = corner_indices[i][0];
122
      int c2 = corner_indices[i][1];
123
      output[i] = commonCornerColor(corners[c1],corners[c2]);
124
      if( output[i]<0 ) return ERROR_CORNERS_CANNOT;
125
      }
126

    
127
    return 0;
128
    }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131

    
132
  private boolean cornerIs(int[] corner, int f0, int f1, int f2)
133
    {
134
    int c0 = mFaceColors[f0];
135
    int c1 = mFaceColors[f1];
136
    int c2 = mFaceColors[f2];
137

    
138
    return ( (corner[0]==c0 && corner[1]==c1 && corner[2]==c2 ) ||
139
             (corner[1]==c0 && corner[2]==c1 && corner[0]==c2 ) ||
140
             (corner[2]==c0 && corner[0]==c1 && corner[1]==c2 )  );
141
    }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
  private int checkAllCornersPresent(int[][] corners)
146
    {
147
    boolean[] present = new boolean[8];
148

    
149
    for(int i=0; i<8; i++)
150
      {
151
      if( cornerIs(corners[i],4,2,0) ) present[0]=true;
152
      if( cornerIs(corners[i],2,5,0) ) present[1]=true;
153
      if( cornerIs(corners[i],3,4,0) ) present[2]=true;
154
      if( cornerIs(corners[i],5,3,0) ) present[3]=true;
155
      if( cornerIs(corners[i],1,2,4) ) present[4]=true;
156
      if( cornerIs(corners[i],5,2,1) ) present[5]=true;
157
      if( cornerIs(corners[i],4,3,1) ) present[6]=true;
158
      if( cornerIs(corners[i],1,3,5) ) present[7]=true;
159
      }
160

    
161
    if( !present[0] ) return ERROR_CORNER_024_MISSING;
162
    if( !present[1] ) return ERROR_CORNER_025_MISSING;
163
    if( !present[2] ) return ERROR_CORNER_034_MISSING;
164
    if( !present[3] ) return ERROR_CORNER_035_MISSING;
165
    if( !present[4] ) return ERROR_CORNER_124_MISSING;
166
    if( !present[5] ) return ERROR_CORNER_125_MISSING;
167
    if( !present[6] ) return ERROR_CORNER_134_MISSING;
168
    if( !present[7] ) return ERROR_CORNER_135_MISSING;
169

    
170
    return 0;
171
    }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

    
175
  private int retFreeCornerPermutation(int[] perm, int[][] corners)
176
    {
177
    perm[0] = -1;
178
    perm[1] = -1;
179
    perm[2] = -1;
180
    perm[3] = -1;
181

    
182
    for(int i=0; i<4; i++)
183
      {
184
      int[] cor = corners[FREE[i]];
185

    
186
      if( cornerIs(cor,2,5,0) ) perm[0] = i;
187
      if( cornerIs(cor,3,4,0) ) perm[1] = i;
188
      if( cornerIs(cor,1,2,4) ) perm[2] = i;
189
      if( cornerIs(cor,1,3,5) ) perm[3] = i;
190
      }
191

    
192
    if( perm[0]==-1 ) return ERROR_CORNER_025_MISSING;
193
    if( perm[1]==-1 ) return ERROR_CORNER_034_MISSING;
194
    if( perm[2]==-1 ) return ERROR_CORNER_124_MISSING;
195
    if( perm[3]==-1 ) return ERROR_CORNER_135_MISSING;
196

    
197
    return 0;
198
    }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

    
202
  private int retCornerPerm(int index, int[] perm)
203
    {
204
    switch(index)
205
      {
206
      case 0: return 0;
207
      case 1: return FREE[perm[0]];
208
      case 2: return FREE[perm[1]];
209
      case 3: return 3;
210
      case 4: return FREE[perm[2]];
211
      case 5: return 5;
212
      case 6: return 6;
213
      case 7: return FREE[perm[3]];
214
      }
215

    
216
    return -1;
217
    }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

    
221
  private void computeCornerQuats(int[] quats, int[][] corners, int[] perm)
222
    {
223
    final int[] zeroeth_face_map = { 4,2,3,5,1,5,4,1 };
224
    int[] twist = new int[8];
225

    
226
    for(int i=0; i<8; i++)
227
      {
228
      int color = mFaceColors[zeroeth_face_map[i]];
229
      int[] c = corners[retCornerPerm(i,perm)];
230

    
231
           if( c[0]==color ) twist[i] = 0;
232
      else if( c[1]==color ) twist[i] = 1;
233
      else                   twist[i] = 2;
234
      }
235

    
236
    TBSkewb.fillInQuats(quats,perm,twist);
237
    }
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

    
241
  public int tablebaseIndex(TwistyObject object)
242
    {
243
    int[][] corners= new int[8][3];
244
    int[] centers  = new int[6];
245
    int[] twist    = new int[8];
246
    int[] freePerm = new int[4];
247
    int[] quats    = new int[8];
248

    
249
    getCorners(object,corners);
250

    
251
    int result1 = computeFaceColors(corners,mFaceColors);
252
    if( result1<0 ) return result1;
253

    
254
    int result2 = checkAllCornersPresent(corners);
255
    if( result2<0 ) return result2;
256

    
257
    int result3 = getCenters(object,centers);
258
    if( result3<0 ) return result3;
259

    
260
    if( !TablebaseHelpers.permutationIsEven(centers) ) return ERROR_TWO_CENTERS;
261
    int center_perm_num = TablebaseHelpers.computeEvenPermutationNum(centers);
262

    
263
    int result4 = retFreeCornerPermutation(freePerm,corners);
264
    if( result4<0 ) return result4;
265

    
266
    computeCornerQuats(quats,corners,freePerm);
267
    TBSkewb.computeCornerTwists(twist,quats);
268

    
269
    int total = 0;
270
    for(int i=0; i<4; i++) total += twist[FREE[i]];
271
    if( (total%3)!=0 ) return ERROR_CORNER_TWISTED;
272
    int totalTwist = twist[0]+ 3*(twist[1]+ 3*(twist[2]+ 3*(twist[3]+ 3*(twist[4]+ 3*(twist[5]+ 3*twist[6])))));
273

    
274
    int locationOfFree0 = TBSkewb.computeLocation(0,quats[1]);
275

    
276
    return center_perm_num+ 360*(totalTwist + 2187*locationOfFree0);
277
    }
278

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

    
281
  private int getColorIndex2(int face)
282
    {
283
    switch(mFaceColors[face])
284
      {
285
      case 0: return R.string.color_yellow2;
286
      case 1: return R.string.color_white2;
287
      case 2: return R.string.color_blue2;
288
      case 3: return R.string.color_green2;
289
      case 4: return R.string.color_red2;
290
      case 5: return R.string.color_orange2;
291
      }
292

    
293
    return -1;
294
    }
295

    
296
///////////////////////////////////////////////////////////////////////////////////////////////////
297

    
298
  private int getColorIndex3(int face)
299
    {
300
    switch(mFaceColors[face])
301
      {
302
      case 0: return R.string.color_yellow3;
303
      case 1: return R.string.color_white3;
304
      case 2: return R.string.color_blue3;
305
      case 3: return R.string.color_green3;
306
      case 4: return R.string.color_red3;
307
      case 5: return R.string.color_orange3;
308
      }
309

    
310
    return -1;
311
    }
312

    
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314

    
315
  private int getColorIndex4(int face)
316
    {
317
    switch(mFaceColors[face])
318
      {
319
      case 0: return R.string.color_yellow4;
320
      case 1: return R.string.color_white4;
321
      case 2: return R.string.color_blue4;
322
      case 3: return R.string.color_green4;
323
      case 4: return R.string.color_red4;
324
      case 5: return R.string.color_orange4;
325
      }
326

    
327
    return -1;
328
    }
329

    
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331

    
332
  private String cornerError(Resources res, int face0, int face1, int face2)
333
    {
334
    int j0 = getColorIndex3(face0);
335
    int j1 = getColorIndex3(face1);
336
    int j2 = getColorIndex4(face2);
337

    
338
    String c0 = res.getString(j0);
339
    String c1 = res.getString(j1);
340
    String c2 = res.getString(j2);
341

    
342
    return res.getString(R.string.solver_generic_missing_corner,c0,c1,c2);
343
    }
344

    
345
///////////////////////////////////////////////////////////////////////////////////////////////////
346

    
347
  private String centerError(Resources res, int face)
348
    {
349
    int color = getColorIndex2(face);
350
    String clr= res.getString(color);
351
    return res.getString(R.string.solver_generic_missing_center,clr);
352
    }
353

    
354
///////////////////////////////////////////////////////////////////////////////////////////////////
355

    
356
  public String error(int index, Resources res)
357
    {
358
    switch(index)
359
      {
360
      case ERROR_CORNER_135_MISSING: return cornerError(res,1,3,5);
361
      case ERROR_CORNER_134_MISSING: return cornerError(res,1,3,4);
362
      case ERROR_CORNER_125_MISSING: return cornerError(res,1,2,5);
363
      case ERROR_CORNER_124_MISSING: return cornerError(res,1,2,4);
364
      case ERROR_CORNER_035_MISSING: return cornerError(res,0,3,5);
365
      case ERROR_CORNER_034_MISSING: return cornerError(res,0,3,4);
366
      case ERROR_CORNER_025_MISSING: return cornerError(res,0,2,5);
367
      case ERROR_CORNER_024_MISSING: return cornerError(res,0,2,4);
368

    
369
      case ERROR_CENTER_0_MISSING  : return centerError(res,0);
370
      case ERROR_CENTER_1_MISSING  : return centerError(res,1);
371
      case ERROR_CENTER_2_MISSING  : return centerError(res,2);
372
      case ERROR_CENTER_3_MISSING  : return centerError(res,3);
373
      case ERROR_CENTER_4_MISSING  : return centerError(res,4);
374
      case ERROR_CENTER_5_MISSING  : return centerError(res,5);
375

    
376
      case ERROR_CORNERS_CANNOT    : return res.getString(R.string.solver_generic_corners_cannot);
377
      case ERROR_CORNER_TWISTED    : return res.getString(R.string.solver_generic_corner_twist);
378
      case ERROR_TWO_CENTERS       : return res.getString(R.string.solver_generic_two_centers);
379
      case ERROR_TWO_CORNERS       : return res.getString(R.string.solver_generic_two_corners);
380
      }
381

    
382
    return null;
383
    }
384

    
385
///////////////////////////////////////////////////////////////////////////////////////////////////
386

    
387
  public int[][] solution(int index, Resources res)
388
    {
389
    if( mSolver==null )
390
      {
391
      mSolver = ImplementedTablebasesList.createPacked(res,ObjectSignatures.SKEW_2);
392
      }
393

    
394
    return mSolver!=null ? mSolver.solution(index,null) : null;
395
    }
396
}  
397

    
(11-11/13)