Project

General

Profile

« Previous | Next » 

Revision 478a4c45

Added by Leszek Koltunski about 1 year ago

Progress with Skewb Diamond solver.

View differences:

src/main/java/org/distorted/solvers/SolverSkewbDiamond.java
11 11

  
12 12
import android.content.res.Resources;
13 13

  
14
import org.distorted.main.R;
14 15
import org.distorted.objectlib.main.ObjectType;
15 16
import org.distorted.objectlib.main.TwistyObject;
16 17
import org.distorted.objectlib.tablebases.ImplementedTablebasesList;
......
43 44
  private static final int ERROR_CORNERS_CANNOT   = -18;
44 45

  
45 46
  private TablebasesAbstract mSolver;
47
  private final int[] mFaceColors;
48

  
49
  private static final int[] FREE_CENTERS = {0,2,5,7};
46 50

  
47 51
///////////////////////////////////////////////////////////////////////////////////////////////////
48 52

  
49 53
  public SolverSkewbDiamond(Resources res, TwistyObject object)
50 54
    {
51 55
    super(res,object);
56
    mFaceColors = new int[8];
57
    }
58

  
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

  
61
  private int retCorner(int[][] corners, int c1, int c2)
62
    {
63
    for(int i=0; i<6; i++)
64
      {
65
      int[] cor = corners[i];
66
      int twist = retCornerTwist(cor,c1,c2);
67
      if( twist>=0 ) return i;
68
      }
69

  
70
    return -1;
71
    }
72

  
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74

  
75
  private int[] getCornersPermutation(int[][] corners)
76
    {
77
    int[] perm = new int[6];
78

  
79
    perm[0] = retCorner(corners,mFaceColors[1],mFaceColors[4]);
80
    perm[1] = retCorner(corners,mFaceColors[1],mFaceColors[6]);
81
    perm[2] = retCorner(corners,mFaceColors[3],mFaceColors[6]);
82
    perm[3] = retCorner(corners,mFaceColors[3],mFaceColors[4]);
83
    perm[4] = retCorner(corners,mFaceColors[1],mFaceColors[3]);
84
    perm[5] = retCorner(corners,mFaceColors[4],mFaceColors[6]);
85

  
86
    return perm;
52 87
    }
53 88

  
54 89
///////////////////////////////////////////////////////////////////////////////////////////////////
55 90

  
56
  private int[] getCornersPermutation(int[] colors, int[][] corners)
91
  private int[] getCornersTwist(int[] corners_perm, int[][] corners)
57 92
    {
93
    int[] twist = new int[6];
94

  
95
    twist[0] = retCornerTwist(corners[corners_perm[0]], mFaceColors[1],mFaceColors[4]);
96
    twist[1] = retCornerTwist(corners[corners_perm[1]], mFaceColors[1],mFaceColors[6]);
97
    twist[2] = retCornerTwist(corners[corners_perm[2]], mFaceColors[3],mFaceColors[6]);
98
    twist[3] = retCornerTwist(corners[corners_perm[3]], mFaceColors[3],mFaceColors[4]);
99
    twist[4] = retCornerTwist(corners[corners_perm[4]], mFaceColors[1],mFaceColors[3]);
100
    twist[5] = retCornerTwist(corners[corners_perm[5]], mFaceColors[4],mFaceColors[6]);
58 101

  
102
    return twist;
59 103
    }
60 104

  
61 105
///////////////////////////////////////////////////////////////////////////////////////////////////
62 106

  
63
  private int[] getCornersTwist(int[] colors, int[][] corners)
107
  private int retCenter(int color, int[] centers)
64 108
    {
109
    for(int i=0; i<4; i++ )
110
      if( centers[FREE_CENTERS[i]]==color ) return i;
65 111

  
112
    return -1;
66 113
    }
67 114

  
68 115
///////////////////////////////////////////////////////////////////////////////////////////////////
69 116

  
70
  private int[] getFreeCentersPermutation(int[] colors, int[] centers)
117
  private int[] getFreeCentersPermutation(int[] centers)
71 118
    {
119
    int[] perm = new int[4];
120

  
121
    for(int i=0; i<4; i++ )
122
      perm[i] = retCenter(mFaceColors[FREE_CENTERS[i]],centers);
72 123

  
124
    return perm;
73 125
    }
74 126

  
75 127
///////////////////////////////////////////////////////////////////////////////////////////////////
......
89 141
    return total;
90 142
    }
91 143

  
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145

  
146
  private int retCornerTwist(int[] corner, int c1, int c2)
147
    {
148
    if( corner[0]==c1 && corner[2]==c2 ) return 1;
149
    if( corner[1]==c1 && corner[3]==c2 ) return 2;
150
    if( corner[2]==c1 && corner[0]==c2 ) return 3;
151
    if( corner[3]==c1 && corner[1]==c2 ) return 0;
152

  
153
    return -1;
154
    }
155

  
92 156
///////////////////////////////////////////////////////////////////////////////////////////////////
93 157

  
94 158
  private int figureOutColor(int[][] corners, int c1, int c2)
......
96 160
    for(int i=0; i<6; i++)
97 161
      {
98 162
      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];
163
      int twist = retCornerTwist(cor,c1,c2);
164
      if( twist>=0 ) return cor[twist];
104 165
      }
105 166

  
106 167
    return -1;
......
158 219

  
159 220
///////////////////////////////////////////////////////////////////////////////////////////////////
160 221

  
161
  private int checkAllColorsDifferent(int[] colors)
222
  private int checkAllColorsDifferent()
162 223
    {
163 224
    for(int i=0; i<8; i++)
164 225
      {
165 226
      boolean present = false;
166 227

  
167 228
      for(int j=0; j<8; j++)
168
        if( colors[j]==i )
229
        if( mFaceColors[j]==i )
169 230
          {
170 231
          present=true;
171 232
          break;
......
266 327
    {
267 328
    int[][] corners = new int[6][4];
268 329
    int[] centers = new int[8];
269
    int[] face_colors = new int[8];
270 330

  
271 331
    getCorners(object,corners);
272 332
    getCenters(object,centers);
......
274 334
    int result1 = checkAllCentersPresent(centers);
275 335
    if( result1<0 ) return result1;
276 336

  
277
    int result2 = figureOutFaceColors(face_colors,centers,corners);
337
    int result2 = figureOutFaceColors(mFaceColors,centers,corners);
278 338
    if( result2<0 ) return result2;
279 339

  
280
    int result3 = checkAllColorsDifferent(face_colors);
340
    int result3 = checkAllColorsDifferent();
281 341
    if( result3<0 ) return result3;
282 342

  
283
    int[] corners_perm = getCornersPermutation(face_colors, corners);
343
    int[] corners_perm = getCornersPermutation(corners);
284 344
    boolean even1 = TablebaseHelpers.permutationIsEven(corners_perm);
285 345
    if( !even1 ) return ERROR_TWO_CORNERS;
286 346

  
287
    int[] free_centers_perm = getFreeCentersPermutation(face_colors, centers);
347
    int[] free_centers_perm = getFreeCentersPermutation(centers);
288 348
    boolean even2 = TablebaseHelpers.permutationIsEven(free_centers_perm);
289 349
    if( !even2 ) return ERROR_TWO_CENTERS;
290 350

  
291
    int[] corners_twist = getCornersTwist(face_colors, corners);
351
    int[] corners_twist = getCornersTwist(corners_perm, corners);
292 352
    int totalTwist = getTotalTwist(corners_twist);
293 353
    if( totalTwist<0 ) return ERROR_CORNER_TWIST;
294 354

  
......
298 358
    return centers_perm_num + 12*(totalTwist + 128*corners_perm_num);
299 359
    }
300 360

  
361
///////////////////////////////////////////////////////////////////////////////////////////////////
362

  
363
  private int getColorIndex4(int face)
364
    {
365
    switch(mFaceColors[face])
366
      {
367
      case 0: return R.string.color_violet4;
368
      case 1: return R.string.color_grey4;
369
      case 2: return R.string.color_blue4;
370
      case 3: return R.string.color_red4;
371
      case 4: return R.string.color_orange4;
372
      case 5: return R.string.color_green4;
373
      case 6: return R.string.color_white4;
374
      case 7: return R.string.color_yellow4;
375
      }
376

  
377
    return -1;
378
    }
379

  
380
///////////////////////////////////////////////////////////////////////////////////////////////////
381

  
382
  private int getColorIndex3(int face)
383
    {
384
    switch(mFaceColors[face])
385
      {
386
      case 0: return R.string.color_violet3;
387
      case 1: return R.string.color_grey3;
388
      case 2: return R.string.color_blue3;
389
      case 3: return R.string.color_red3;
390
      case 4: return R.string.color_orange3;
391
      case 5: return R.string.color_green3;
392
      case 6: return R.string.color_white3;
393
      case 7: return R.string.color_yellow3;
394
      }
395

  
396
    return -1;
397
    }
398

  
399
///////////////////////////////////////////////////////////////////////////////////////////////////
400

  
401
  private int getColorIndex2(int face)
402
    {
403
    switch(mFaceColors[face])
404
      {
405
      case 0: return R.string.color_violet2;
406
      case 1: return R.string.color_grey2;
407
      case 2: return R.string.color_blue2;
408
      case 3: return R.string.color_red2;
409
      case 4: return R.string.color_orange2;
410
      case 5: return R.string.color_green2;
411
      case 6: return R.string.color_white2;
412
      case 7: return R.string.color_yellow2;
413
      }
414

  
415
    return -1;
416
    }
417

  
418
///////////////////////////////////////////////////////////////////////////////////////////////////
419

  
420
  private String centerError(Resources res, int face)
421
    {
422
    String color = res.getString(getColorIndex2(face));
423
    return res.getString(R.string.solver_generic_missing_center,color);
424
    }
425

  
426
///////////////////////////////////////////////////////////////////////////////////////////////////
427

  
428
  private String cornerError(Resources res, int f1, int f2)
429
    {
430
    String c1 = res.getString(getColorIndex3(f1));
431
    String c2 = res.getString(getColorIndex4(f2));
432
    return res.getString(R.string.solver_generic_missing_corner2,c1,c2);
433
    }
434

  
301 435
///////////////////////////////////////////////////////////////////////////////////////////////////
302 436

  
303 437
  public String error(int index, Resources res)
304 438
    {
305 439
    switch(index)
306 440
      {
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   :
441
      case ERROR_CORNER_FR_MISSING: return cornerError(res,1,4);
442
      case ERROR_CORNER_BR_MISSING: return cornerError(res,1,6);
443
      case ERROR_CORNER_BL_MISSING: return cornerError(res,3,6);
444
      case ERROR_CORNER_FL_MISSING: return cornerError(res,3,4);
445
      case ERROR_CORNER_TO_MISSING: return cornerError(res,1,3);
446
      case ERROR_CORNER_BO_MISSING: return cornerError(res,4,6);
447

  
448
      case ERROR_CENTER_0_MISSING : return centerError(res,0);
449
      case ERROR_CENTER_1_MISSING : return centerError(res,1);
450
      case ERROR_CENTER_2_MISSING : return centerError(res,2);
451
      case ERROR_CENTER_3_MISSING : return centerError(res,3);
452
      case ERROR_CENTER_4_MISSING : return centerError(res,4);
453
      case ERROR_CENTER_5_MISSING : return centerError(res,5);
454
      case ERROR_CENTER_6_MISSING : return centerError(res,6);
455
      case ERROR_CENTER_7_MISSING : return centerError(res,7);
456

  
457
      case ERROR_TWO_CORNERS      : return res.getString(R.string.solver_generic_two_corners);
458
      case ERROR_TWO_CENTERS      : return res.getString(R.string.solver_generic_two_centers);
459
      case ERROR_CORNER_TWIST     : return res.getString(R.string.solver_generic_corner_twist);
460
      case ERROR_CORNERS_CANNOT   : return res.getString(R.string.solver_generic_corners_cannot);
327 461
      }
328 462

  
329 463
    return null;

Also available in: Unified diff