Project

General

Profile

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

magiccube / src / main / java / org / distorted / solvers / SolverSkewbDiamond.java @ 030f1bf4

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
      if( t==2 ) total++;
138
      total *= 2;
139
      }
140

    
141
    return total;
142
    }
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

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

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

    
167
    return -1;
168
    }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171
// We only move the UR, UR, DR & DB faces --> those centers are fixed and determine the colors of
172
// the faces.
173

    
174
  private int figureOutFaceColors(int[] output, int[] centers, int[][] corners)
175
    {
176
    output[1] = centers[1];
177
    output[3] = centers[3];
178
    output[4] = centers[4];
179
    output[6] = centers[6];
180

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

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

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

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

    
217
    return 0;
218
    }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

    
222
  private int checkAllColorsDifferent()
223
    {
224
    for(int i=0; i<8; i++)
225
      {
226
      boolean present = false;
227

    
228
      for(int j=0; j<8; j++)
229
        if( mFaceColors[j]==i )
230
          {
231
          present=true;
232
          break;
233
          }
234

    
235
      if( !present ) return ERROR_CORNERS_CANNOT;
236
      }
237

    
238
    return 0;
239
    }
240

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

    
243
  private int checkAllCentersPresent(int[] centers)
244
    {
245
    for(int i=0; i<8; i++)
246
      {
247
      boolean present = false;
248

    
249
      for(int j=0; j<8; j++)
250
        if( centers[j]==i )
251
          {
252
          present=true;
253
          break;
254
          }
255

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

    
272
    return 0;
273
    }
274

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

    
277
  private void getCorners(TwistyObject object, int[][] corners)
278
    {
279
    corners[0][0] = object.getCubitFaceStickerIndex( 0,5); // FR
280
    corners[0][1] = object.getCubitFaceStickerIndex( 0,4);
281
    corners[0][2] = object.getCubitFaceStickerIndex( 0,0);
282
    corners[0][3] = object.getCubitFaceStickerIndex( 0,1);
283

    
284
    corners[1][0] = object.getCubitFaceStickerIndex( 1,2); // BR
285
    corners[1][1] = object.getCubitFaceStickerIndex( 1,6);
286
    corners[1][2] = object.getCubitFaceStickerIndex( 1,5);
287
    corners[1][3] = object.getCubitFaceStickerIndex( 1,1);
288

    
289
    corners[2][0] = object.getCubitFaceStickerIndex( 2,7); // BL
290
    corners[2][1] = object.getCubitFaceStickerIndex( 2,6);
291
    corners[2][2] = object.getCubitFaceStickerIndex( 2,2);
292
    corners[2][3] = object.getCubitFaceStickerIndex( 2,3);
293

    
294
    corners[3][0] = object.getCubitFaceStickerIndex( 3,0); // FL
295
    corners[3][1] = object.getCubitFaceStickerIndex( 3,4);
296
    corners[3][2] = object.getCubitFaceStickerIndex( 3,7);
297
    corners[3][3] = object.getCubitFaceStickerIndex( 3,3);
298

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

    
304
    corners[5][0] = object.getCubitFaceStickerIndex( 5,5); // D
305
    corners[5][1] = object.getCubitFaceStickerIndex( 5,6);
306
    corners[5][2] = object.getCubitFaceStickerIndex( 5,7);
307
    corners[5][3] = object.getCubitFaceStickerIndex( 5,4);
308
    }
309

    
310
///////////////////////////////////////////////////////////////////////////////////////////////////
311

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

    
324
///////////////////////////////////////////////////////////////////////////////////////////////////
325

    
326
  public int tablebaseIndex(TwistyObject object)
327
    {
328
    int[][] corners = new int[6][4];
329
    int[] centers = new int[8];
330

    
331
    getCorners(object,corners);
332
    getCenters(object,centers);
333

    
334
for(int j=0; j<8; j++)
335
  {
336
  android.util.Log.e("D", "center "+j+" : "+centers[j]);
337
  }
338

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

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

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

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

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

    
356
    int[] corners_twist = getCornersTwist(corners_perm, corners);
357

    
358
android.util.Log.e("D", "faces: "+mFaceColors[0]+" "+mFaceColors[1]+" "+mFaceColors[2]+" "
359
+mFaceColors[3]+" "+mFaceColors[4]+" "+mFaceColors[5]+" "+mFaceColors[6]+" "+mFaceColors[7]);
360

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

    
364
android.util.Log.e("D", "corn twist: "+corners_twist[0]+" "+corners_twist[1]+" "+corners_twist[2]+" "
365
+corners_twist[3]+" "+corners_twist[4]+" "+corners_twist[5]);
366

    
367
    int totalTwist = getTotalTwist(corners_twist);
368
    if( totalTwist<0 ) return ERROR_CORNER_TWIST;
369

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

    
373
    return centers_perm_num + 12*(totalTwist + 128*corners_perm_num);
374
    }
375

    
376
///////////////////////////////////////////////////////////////////////////////////////////////////
377

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

    
392
    return -1;
393
    }
394

    
395
///////////////////////////////////////////////////////////////////////////////////////////////////
396

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

    
411
    return -1;
412
    }
413

    
414
///////////////////////////////////////////////////////////////////////////////////////////////////
415

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

    
430
    return -1;
431
    }
432

    
433
///////////////////////////////////////////////////////////////////////////////////////////////////
434

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

    
441
///////////////////////////////////////////////////////////////////////////////////////////////////
442

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

    
450
///////////////////////////////////////////////////////////////////////////////////////////////////
451

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

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

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

    
478
    return null;
479
    }
480

    
481
///////////////////////////////////////////////////////////////////////////////////////////////////
482

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

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

    
(8-8/9)