Project

General

Profile

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

magiccube / src / main / java / org / distorted / solvers / SolverSkewbDiamond.java @ ca5260c2

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.main.ObjectType;
16
import org.distorted.objectlib.main.TwistyObject;
17
import org.distorted.objectlib.tablebases.ImplementedTablebasesList;
18
import org.distorted.objectlib.tablebases.TablebaseHelpers;
19
import org.distorted.objectlib.tablebases.TablebasesAbstract;
20

    
21
///////////////////////////////////////////////////////////////////////////////////////////////////
22

    
23
public class SolverSkewbDiamond extends SolverTablebase
24
{
25
  private static final int ERROR_CORNER_FR_MISSING = -1;
26
  private static final int ERROR_CORNER_BR_MISSING = -2;
27
  private static final int ERROR_CORNER_BL_MISSING = -3;
28
  private static final int ERROR_CORNER_FL_MISSING = -4;
29
  private static final int ERROR_CORNER_TO_MISSING = -5;
30
  private static final int ERROR_CORNER_BO_MISSING = -6;
31

    
32
  private static final int ERROR_CENTER_0_MISSING = -7;
33
  private static final int ERROR_CENTER_1_MISSING = -8;
34
  private static final int ERROR_CENTER_2_MISSING = -9;
35
  private static final int ERROR_CENTER_3_MISSING = -10;
36
  private static final int ERROR_CENTER_4_MISSING = -11;
37
  private static final int ERROR_CENTER_5_MISSING = -12;
38
  private static final int ERROR_CENTER_6_MISSING = -13;
39
  private static final int ERROR_CENTER_7_MISSING = -14;
40

    
41
  private static final int ERROR_TWO_CORNERS      = -15;
42
  private static final int ERROR_TWO_CENTERS      = -16;
43
  private static final int ERROR_CORNER_TWIST     = -17;
44
  private static final int ERROR_CORNERS_CANNOT   = -18;
45

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

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

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
  public SolverSkewbDiamond(Resources res, TwistyObject object)
54
    {
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;
87
    }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

    
91
  private int[] getCornersTwist(int[] corners_perm, int[][] corners)
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]);
101

    
102
    return twist;
103
    }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

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

    
112
    return -1;
113
    }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116

    
117
  private int[] getFreeCentersPermutation(int[] centers)
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);
123

    
124
    return perm;
125
    }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

    
129
  private int getTotalTwist(int[] twist)
130
    {
131
    int total = 0;
132

    
133
    for(int i=0; i<6; i++)
134
      {
135
      int t= twist[i];
136
      if( t==1 || t==3 ) return -1;
137

    
138
      if( i<5 )
139
        {
140
        if( t==2 ) total++;
141
        total *= 2;
142
        }
143
      }
144

    
145
    return total;
146
    }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

    
150
  private int retCornerTwist(int[] corner, int c1, int c2)
151
    {
152
    if( corner[0]==c1 && corner[2]==c2 ) return 1;
153
    if( corner[1]==c1 && corner[3]==c2 ) return 2;
154
    if( corner[2]==c1 && corner[0]==c2 ) return 3;
155
    if( corner[3]==c1 && corner[1]==c2 ) return 0;
156

    
157
    return -1;
158
    }
159

    
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161

    
162
  private int figureOutColor(int[][] corners, int c1, int c2)
163
    {
164
    for(int i=0; i<6; i++)
165
      {
166
      int[] cor = corners[i];
167
      int twist = retCornerTwist(cor,c1,c2);
168
      if( twist>=0 ) return cor[twist];
169
      }
170

    
171
    return -1;
172
    }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175
// We only move the UR, UR, DR & DB faces --> those centers are fixed and determine the colors of
176
// the faces.
177

    
178
  private int figureOutFaceColors(int[] output, int[] centers, int[][] corners)
179
    {
180
    output[1] = centers[1];
181
    output[3] = centers[3];
182
    output[4] = centers[4];
183
    output[6] = centers[6];
184

    
185
    int color01 = figureOutColor(corners,output[4],output[1]);
186
    if( color01<0 ) return ERROR_CORNER_FR_MISSING;
187
    int color02 = figureOutColor(corners,output[3],output[4]);
188
    if( color02<0 ) return ERROR_CORNER_FL_MISSING;
189
    int color03 = figureOutColor(corners,output[1],output[3]);
190
    if( color03<0 ) return ERROR_CORNER_TO_MISSING;
191
    if( color01!=color02 || color01!=color03 ) return ERROR_CORNERS_CANNOT;
192
    output[0] = color01;
193

    
194
    int color21 = figureOutColor(corners,output[1],output[6]);
195
    if( color21<0 ) return ERROR_CORNER_BR_MISSING;
196
    int color22 = figureOutColor(corners,output[6],output[3]);
197
    if( color22<0 ) return ERROR_CORNER_BL_MISSING;
198
    int color23 = figureOutColor(corners,output[3],output[1]);
199
    if( color23<0 ) return ERROR_CORNER_TO_MISSING;
200
    if( color21!=color22 || color21!=color23 ) return ERROR_CORNERS_CANNOT;
201
    output[2] = color21;
202

    
203
    int color51 = figureOutColor(corners,output[1],output[4]);
204
    if( color51<0 ) return ERROR_CORNER_FR_MISSING;
205
    int color52 = figureOutColor(corners,output[6],output[1]);
206
    if( color52<0 ) return ERROR_CORNER_BR_MISSING;
207
    int color53 = figureOutColor(corners,output[4],output[6]);
208
    if( color53<0 ) return ERROR_CORNER_BO_MISSING;
209
    if( color51!=color52 || color51!=color53 ) return ERROR_CORNERS_CANNOT;
210
    output[5] = color51;
211

    
212
    int color71 = figureOutColor(corners,output[3],output[6]);
213
    if( color71<0 ) return ERROR_CORNER_BL_MISSING;
214
    int color72 = figureOutColor(corners,output[4],output[3]);
215
    if( color72<0 ) return ERROR_CORNER_FL_MISSING;
216
    int color73 = figureOutColor(corners,output[6],output[4]);
217
    if( color73<0 ) return ERROR_CORNER_BO_MISSING;
218
    if( color71!=color72 || color71!=color73 ) return ERROR_CORNERS_CANNOT;
219
    output[7] = color71;
220

    
221
    return 0;
222
    }
223

    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225

    
226
  private int checkAllColorsDifferent()
227
    {
228
    for(int i=0; i<8; i++)
229
      {
230
      boolean present = false;
231

    
232
      for(int j=0; j<8; j++)
233
        if( mFaceColors[j]==i )
234
          {
235
          present=true;
236
          break;
237
          }
238

    
239
      if( !present ) return ERROR_CORNERS_CANNOT;
240
      }
241

    
242
    return 0;
243
    }
244

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

    
247
  private int checkAllCentersPresent(int[] centers)
248
    {
249
    for(int i=0; i<8; i++)
250
      {
251
      boolean present = false;
252

    
253
      for(int j=0; j<8; j++)
254
        if( centers[j]==i )
255
          {
256
          present=true;
257
          break;
258
          }
259

    
260
      if( !present )
261
        {
262
        switch(i)
263
          {
264
          case 0: return ERROR_CENTER_0_MISSING;
265
          case 1: return ERROR_CENTER_1_MISSING;
266
          case 2: return ERROR_CENTER_2_MISSING;
267
          case 3: return ERROR_CENTER_3_MISSING;
268
          case 4: return ERROR_CENTER_4_MISSING;
269
          case 5: return ERROR_CENTER_5_MISSING;
270
          case 6: return ERROR_CENTER_6_MISSING;
271
          case 7: return ERROR_CENTER_7_MISSING;
272
          }
273
        }
274
      }
275

    
276
    return 0;
277
    }
278

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

    
281
  private void getCorners(TwistyObject object, int[][] corners)
282
    {
283
    corners[0][0] = object.getCubitFaceStickerIndex( 0,5); // FR
284
    corners[0][1] = object.getCubitFaceStickerIndex( 0,4);
285
    corners[0][2] = object.getCubitFaceStickerIndex( 0,0);
286
    corners[0][3] = object.getCubitFaceStickerIndex( 0,1);
287

    
288
    corners[1][0] = object.getCubitFaceStickerIndex( 1,2); // BR
289
    corners[1][1] = object.getCubitFaceStickerIndex( 1,6);
290
    corners[1][2] = object.getCubitFaceStickerIndex( 1,5);
291
    corners[1][3] = object.getCubitFaceStickerIndex( 1,1);
292

    
293
    corners[2][0] = object.getCubitFaceStickerIndex( 2,7); // BL
294
    corners[2][1] = object.getCubitFaceStickerIndex( 2,6);
295
    corners[2][2] = object.getCubitFaceStickerIndex( 2,2);
296
    corners[2][3] = object.getCubitFaceStickerIndex( 2,3);
297

    
298
    corners[3][0] = object.getCubitFaceStickerIndex( 3,0); // FL
299
    corners[3][1] = object.getCubitFaceStickerIndex( 3,4);
300
    corners[3][2] = object.getCubitFaceStickerIndex( 3,7);
301
    corners[3][3] = object.getCubitFaceStickerIndex( 3,3);
302

    
303
    corners[4][0] = object.getCubitFaceStickerIndex( 4,0); // U
304
    corners[4][1] = object.getCubitFaceStickerIndex( 4,3);
305
    corners[4][2] = object.getCubitFaceStickerIndex( 4,2);
306
    corners[4][3] = object.getCubitFaceStickerIndex( 4,1);
307

    
308
    corners[5][0] = object.getCubitFaceStickerIndex( 5,5); // D
309
    corners[5][1] = object.getCubitFaceStickerIndex( 5,6);
310
    corners[5][2] = object.getCubitFaceStickerIndex( 5,7);
311
    corners[5][3] = object.getCubitFaceStickerIndex( 5,4);
312
    }
313

    
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315

    
316
  private void getCenters(TwistyObject object, int[] centers)
317
    {
318
    centers[0] = object.getCubitFaceStickerIndex( 9,0); // UF
319
    centers[1] = object.getCubitFaceStickerIndex( 6,0); // UR
320
    centers[2] = object.getCubitFaceStickerIndex( 7,0); // UB
321
    centers[3] = object.getCubitFaceStickerIndex( 8,0); // UL
322
    centers[4] = object.getCubitFaceStickerIndex(13,0); // DF
323
    centers[5] = object.getCubitFaceStickerIndex(10,1); // DR
324
    centers[6] = object.getCubitFaceStickerIndex(11,0); // DB
325
    centers[7] = object.getCubitFaceStickerIndex(12,0); // DL
326
    }
327

    
328
///////////////////////////////////////////////////////////////////////////////////////////////////
329

    
330
  public int tablebaseIndex(TwistyObject object)
331
    {
332
    int[][] corners = new int[6][4];
333
    int[] centers = new int[8];
334

    
335
    getCorners(object,corners);
336
    getCenters(object,centers);
337

    
338
//for(int j=0; j<8; j++) android.util.Log.e("D", "center "+j+" : "+centers[j]);
339

    
340
    int result1 = checkAllCentersPresent(centers);
341
    if( result1<0 ) return result1;
342

    
343
    int result2 = figureOutFaceColors(mFaceColors,centers,corners);
344
    if( result2<0 ) return result2;
345

    
346
    int result3 = checkAllColorsDifferent();
347
    if( result3<0 ) return result3;
348

    
349
    int[] corners_perm = getCornersPermutation(corners);
350
    boolean even1 = TablebaseHelpers.permutationIsEven(corners_perm);
351
    if( !even1 ) return ERROR_TWO_CORNERS;
352

    
353
    int[] free_centers_perm = getFreeCentersPermutation(centers);
354
    boolean even2 = TablebaseHelpers.permutationIsEven(free_centers_perm);
355
    if( !even2 ) return ERROR_TWO_CENTERS;
356

    
357
    int[] corners_twist = getCornersTwist(corners_perm, corners);
358
/*
359
android.util.Log.e("D", "faces: "+mFaceColors[0]+" "+mFaceColors[1]+" "+mFaceColors[2]+" "
360
+mFaceColors[3]+" "+mFaceColors[4]+" "+mFaceColors[5]+" "+mFaceColors[6]+" "+mFaceColors[7]);
361

    
362
android.util.Log.e("D", "corn perm: "+corners_perm[0]+" "+corners_perm[1]+" "+corners_perm[2]+" "
363
+corners_perm[3]+" "+corners_perm[4]+" "+corners_perm[5]);
364

    
365
android.util.Log.e("D", "corn twist: "+corners_twist[0]+" "+corners_twist[1]+" "+corners_twist[2]+" "
366
+corners_twist[3]+" "+corners_twist[4]+" "+corners_twist[5]);
367
*/
368
    int totalTwist = getTotalTwist(corners_twist);
369
    if( totalTwist<0 ) return ERROR_CORNER_TWIST;
370

    
371
    int corners_perm_num = TablebaseHelpers.computeEvenPermutationNum(corners_perm);
372
    int centers_perm_num = TablebaseHelpers.computeEvenPermutationNum(free_centers_perm);
373

    
374
    return centers_perm_num + 12*(totalTwist + 32*corners_perm_num);
375
    }
376

    
377
///////////////////////////////////////////////////////////////////////////////////////////////////
378

    
379
  private int getColorIndex4(int face)
380
    {
381
    switch(mFaceColors[face])
382
      {
383
      case 0: return R.string.color_violet4;
384
      case 1: return R.string.color_grey4;
385
      case 2: return R.string.color_blue4;
386
      case 3: return R.string.color_red4;
387
      case 4: return R.string.color_orange4;
388
      case 5: return R.string.color_green4;
389
      case 6: return R.string.color_white4;
390
      case 7: return R.string.color_yellow4;
391
      }
392

    
393
    return -1;
394
    }
395

    
396
///////////////////////////////////////////////////////////////////////////////////////////////////
397

    
398
  private int getColorIndex3(int face)
399
    {
400
    switch(mFaceColors[face])
401
      {
402
      case 0: return R.string.color_violet3;
403
      case 1: return R.string.color_grey3;
404
      case 2: return R.string.color_blue3;
405
      case 3: return R.string.color_red3;
406
      case 4: return R.string.color_orange3;
407
      case 5: return R.string.color_green3;
408
      case 6: return R.string.color_white3;
409
      case 7: return R.string.color_yellow3;
410
      }
411

    
412
    return -1;
413
    }
414

    
415
///////////////////////////////////////////////////////////////////////////////////////////////////
416

    
417
  private int getColorIndex2(int face)
418
    {
419
    switch(face)
420
      {
421
      case 0: return R.string.color_violet2;
422
      case 1: return R.string.color_grey2;
423
      case 2: return R.string.color_blue2;
424
      case 3: return R.string.color_red2;
425
      case 4: return R.string.color_orange2;
426
      case 5: return R.string.color_green2;
427
      case 6: return R.string.color_white2;
428
      case 7: return R.string.color_yellow2;
429
      }
430

    
431
    return -1;
432
    }
433

    
434
///////////////////////////////////////////////////////////////////////////////////////////////////
435

    
436
  private String centerError(Resources res, int face)
437
    {
438
    String color = res.getString(getColorIndex2(face));
439
    return res.getString(R.string.solver_generic_missing_center,color);
440
    }
441

    
442
///////////////////////////////////////////////////////////////////////////////////////////////////
443

    
444
  private String cornerError(Resources res, int f1, int f2)
445
    {
446
    String c1 = res.getString(getColorIndex3(f1));
447
    String c2 = res.getString(getColorIndex4(f2));
448
    return res.getString(R.string.solver_generic_missing_corner2,c1,c2);
449
    }
450

    
451
///////////////////////////////////////////////////////////////////////////////////////////////////
452

    
453
  public String error(int index, Resources res)
454
    {
455
    switch(index)
456
      {
457
      case ERROR_CORNER_FR_MISSING: return cornerError(res,1,4);
458
      case ERROR_CORNER_BR_MISSING: return cornerError(res,1,6);
459
      case ERROR_CORNER_BL_MISSING: return cornerError(res,3,6);
460
      case ERROR_CORNER_FL_MISSING: return cornerError(res,3,4);
461
      case ERROR_CORNER_TO_MISSING: return cornerError(res,1,3);
462
      case ERROR_CORNER_BO_MISSING: return cornerError(res,4,6);
463

    
464
      case ERROR_CENTER_0_MISSING : return centerError(res,0);
465
      case ERROR_CENTER_1_MISSING : return centerError(res,1);
466
      case ERROR_CENTER_2_MISSING : return centerError(res,2);
467
      case ERROR_CENTER_3_MISSING : return centerError(res,3);
468
      case ERROR_CENTER_4_MISSING : return centerError(res,4);
469
      case ERROR_CENTER_5_MISSING : return centerError(res,5);
470
      case ERROR_CENTER_6_MISSING : return centerError(res,6);
471
      case ERROR_CENTER_7_MISSING : return centerError(res,7);
472

    
473
      case ERROR_TWO_CORNERS      : return res.getString(R.string.solver_generic_two_corners);
474
      case ERROR_TWO_CENTERS      : return res.getString(R.string.solver_generic_two_centers);
475
      case ERROR_CORNER_TWIST     : return res.getString(R.string.solver_generic_corner_twist);
476
      case ERROR_CORNERS_CANNOT   : return res.getString(R.string.solver_generic_corners_cannot);
477
      }
478

    
479
    return null;
480
    }
481

    
482
///////////////////////////////////////////////////////////////////////////////////////////////////
483

    
484
  public int[][] solution(int index, Resources res)
485
    {
486
    if( mSolver==null )
487
      {
488
      mSolver = ImplementedTablebasesList.createUnpacked(ObjectType.DIAM_2);
489
      if( mSolver!=null ) mSolver.createTablebase();
490
      }
491

    
492
    return mSolver!=null ? mSolver.solution(index) : null;
493
    }
494
}  
495

    
(8-8/9)