Project

General

Profile

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

magiccube / src / main / java / org / distorted / solvers / SolverCuboid323.java @ 144fa403

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.library.type.Static3D;
15
import org.distorted.library.type.Static4D;
16
import org.distorted.main.R;
17
import org.distorted.objectlib.helpers.OperatingSystemInterface;
18
import org.distorted.objectlib.helpers.QuatGroupGenerator;
19
import org.distorted.objectlib.main.ObjectSignatures;
20
import org.distorted.objectlib.main.TwistyObject;
21
import org.distorted.objectlib.tablebases.ImplementedTablebasesList;
22
import org.distorted.objectlib.tablebases.TBCuboid323;
23
import org.distorted.objectlib.tablebases.TablebaseHelpers;
24
import org.distorted.objectlib.tablebases.TablebasesAbstract;
25

    
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27

    
28
public class SolverCuboid323 extends SolverTablebase
29
{
30
  private static final int ERROR_CORNER_MISSING = -1;
31
  private static final int ERROR_EDGE_MISSING   = -2;
32
  private static final int ERROR_CORNERS_CANNOT = -3;
33
  private static final int ERROR_EDGE_TWISTED   = -4;
34
  private static final int ERROR_CORNER_TWISTED = -5;
35

    
36
  TablebasesAbstract mSolver;
37
  private final int[] mFaceColors;
38
  private int mErrorColor1, mErrorColor2, mErrorColor3;
39
  private Static4D[] mQuats;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
  private void initializeQuats()
44
    {
45
    int[] tmp2 = {2,2,2};
46
    int[] tmp4 = {4,4};
47
    int[][] angles = new int[][] { tmp2,tmp4,tmp2 };
48
    Static3D[] axis = new Static3D[] { new Static3D(1,0,0), new Static3D(0,1,0), new Static3D(0,0,1) };
49
    mQuats = QuatGroupGenerator.computeGroup(axis,angles);
50
    }
51

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

    
54
  private int mulQuat(int q1, int q2)
55
    {
56
    if( mQuats==null ) initializeQuats();
57
    return TablebasesAbstract.mulQuat(q1,q2,mQuats);
58
    }
59

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

    
62
  private void rotatePermutations(int[] corner, int[] edge8, int quat)
63
    {
64
    int[] quats = TBCuboid323.quatsFromPermutations(corner,edge8);
65
    int num = quats.length;
66

    
67
    for( int i=0; i<num; i++ ) quats[i] = mulQuat(quat,quats[i]);
68

    
69
    TBCuboid323.cornerFromQuats(corner,quats);
70
    TBCuboid323.edgeFromQuats(edge8,quats);
71
    }
72

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74
// when we input the position, edge1 can be anywhere. We need to rotate the permutations so that
75
// edge1 is always in the front (i.e. so that edge[1] is 1 or 3)
76

    
77
  private void normalizePermutations(int[] corner, int[] edge8)
78
    {
79
    switch(edge8[1])
80
      {
81
      case 0: case 2: rotatePermutations(corner,edge8,3);
82
                      break;
83
      case 4: case 5: rotatePermutations(corner,edge8,4);
84
                      break;
85
      case 6: case 7: rotatePermutations(corner,edge8,2);
86
                      break;
87
      }
88
    }
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91

    
92
  public SolverCuboid323(OperatingSystemInterface os, Resources res, TwistyObject object)
93
    {
94
    super(os,res,object);
95
    mFaceColors = new int[6];
96
    }
97

    
98
////////////////////////////////////////////////////////////////////////////////////////
99

    
100
  private int findCorner(int[][] corners, int c1, int c2)
101
    {
102
    for(int i=0; i<8; i++)
103
      {
104
      int[] c = corners[i];
105

    
106
      if( c[0]==c1 && c[1]==c2 ) return c[2];
107
      if( c[1]==c1 && c[2]==c2 ) return c[0];
108
      if( c[2]==c1 && c[0]==c2 ) return c[1];
109
      }
110

    
111
    return ERROR_CORNERS_CANNOT;
112
    }
113

    
114
////////////////////////////////////////////////////////////////////////////////////////
115

    
116
  private int missingColor()
117
    {
118
    boolean[] present = new boolean[6];
119
    for(int i=0; i<5; i++) present[mFaceColors[i]] = true;
120

    
121
    int indexFalse = -1;
122

    
123
    for(int i=0; i<6; i++)
124
      if( !present[i] )
125
        {
126
        if( indexFalse<0 ) indexFalse=i;
127
        else return ERROR_CORNERS_CANNOT;
128
        }
129

    
130
    return indexFalse;
131
    }
132

    
133
////////////////////////////////////////////////////////////////////////////////////////
134

    
135
  private int edgePresent(int[][] edges, int f0, int f1)
136
    {
137
    int c0 = mFaceColors[f0];
138
    int c1 = mFaceColors[f1];
139

    
140
    for(int i=0; i<8; i++ )
141
      {
142
      int[] edge = edges[i];
143

    
144
      if( edge[0]==c0 && edge[1]==c1 ) return i;
145
      if( edge[0]==c1 && edge[1]==c0 )
146
        {
147
        mErrorColor1 = c0;
148
        mErrorColor2 = c1;
149
        return ERROR_EDGE_TWISTED;
150
        }
151
      }
152

    
153
    mErrorColor1 = c0;
154
    mErrorColor2 = c1;
155
    return ERROR_EDGE_MISSING;
156
    }
157

    
158
////////////////////////////////////////////////////////////////////////////////////////
159

    
160
  private int cornerPresent(int[][] corners, int f0, int f1, int f2)
161
    {
162
    int c0 = mFaceColors[f0];
163
    int c1 = mFaceColors[f1];
164
    int c2 = mFaceColors[f2];
165

    
166
    for(int i=0; i<8; i++)
167
      {
168
      int[] corner = corners[i];
169

    
170
      if(  corner[0]==c0 && corner[1]==c1 && corner[2]==c2 ) return i;
171
      if( (corner[0]==c1 && corner[1]==c2 && corner[2]==c0 ) ||
172
          (corner[0]==c2 && corner[1]==c0 && corner[2]==c1 )  )
173
        {
174
        mErrorColor1 = c0;
175
        mErrorColor2 = c1;
176
        mErrorColor3 = c2;
177
        return ERROR_CORNER_TWISTED;
178
        }
179
      }
180

    
181
    mErrorColor1 = c0;
182
    mErrorColor2 = c1;
183
    mErrorColor3 = c2;
184
    return ERROR_CORNER_MISSING;
185
    }
186

    
187
////////////////////////////////////////////////////////////////////////////////////////
188

    
189
  private int retEdgePermutation(int[] output, int[][] edges)
190
    {
191
    int[][] e = { {5,3}, {4,3}, {5,2}, {4,2}, {1,3}, {1,2}, {0,3}, {0,2} };
192

    
193
    for(int i=0; i<8; i++)
194
      {
195
      int[] ee = e[i];
196
      output[i] = edgePresent(edges,ee[0],ee[1]);
197
      if( output[i]<0 ) return output[i];
198
      }
199

    
200
    return 0;
201
    }
202

    
203
////////////////////////////////////////////////////////////////////////////////////////
204

    
205
  private int retCornerPermutation(int[] output, int[][] corners)
206
    {
207
    int[][] c = { {5,1,3}, {1,4,3}, {1,5,2}, {4,1,2}, {0,5,3}, {4,0,3}, {5,0,2}, {0,4,2} };
208

    
209
    for(int i=0; i<8; i++)
210
      {
211
      int[] cc = c[i];
212
      output[i] = cornerPresent(corners,cc[0],cc[1],cc[2]);
213
      if( output[i]<0 ) return output[i];
214
      }
215

    
216
    return 0;
217
    }
218

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

    
221
  private int computeFaceColors(int[][] corners, int[][] edges, int[] centers)
222
    {
223
    mFaceColors[2] = centers[0];
224
    mFaceColors[3] = centers[1];
225

    
226
    if( edges[1][1]==mFaceColors[2] || edges[1][1]==mFaceColors[3] )
227
      {
228
      mFaceColors[4] = edges[1][0];
229
      }
230
    else return ERROR_CORNERS_CANNOT;
231

    
232
    mFaceColors[0] = findCorner(corners,mFaceColors[4],mFaceColors[2]);
233
    if( mFaceColors[0]<0 ) return mFaceColors[0];
234

    
235
    mFaceColors[1] = findCorner(corners,mFaceColors[2],mFaceColors[4]);
236
    if( mFaceColors[1]<0 ) return mFaceColors[1];
237

    
238
    mFaceColors[5] = missingColor();
239
    if( mFaceColors[5]<0 ) return mFaceColors[5];
240

    
241
    return 0;
242
    }
243

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

    
246
  private void getCorners(TwistyObject object, int[][] corners)
247
    {
248
    corners[0][0] = object.getCubitFaceStickerIndex(0,5);
249
    corners[0][1] = object.getCubitFaceStickerIndex(0,1);
250
    corners[0][2] = object.getCubitFaceStickerIndex(0,3);
251

    
252
    corners[1][0] = object.getCubitFaceStickerIndex(1,3);
253
    corners[1][1] = object.getCubitFaceStickerIndex(1,5);
254
    corners[1][2] = object.getCubitFaceStickerIndex(1,1);
255

    
256
    corners[2][0] = object.getCubitFaceStickerIndex(2,5);
257
    corners[2][1] = object.getCubitFaceStickerIndex(2,1);
258
    corners[2][2] = object.getCubitFaceStickerIndex(2,3);
259

    
260
    corners[3][0] = object.getCubitFaceStickerIndex(3,5);
261
    corners[3][1] = object.getCubitFaceStickerIndex(3,1);
262
    corners[3][2] = object.getCubitFaceStickerIndex(3,3);
263

    
264
    corners[4][0] = object.getCubitFaceStickerIndex(4,1);
265
    corners[4][1] = object.getCubitFaceStickerIndex(4,3);
266
    corners[4][2] = object.getCubitFaceStickerIndex(4,5);
267

    
268
    corners[5][0] = object.getCubitFaceStickerIndex(5,5);
269
    corners[5][1] = object.getCubitFaceStickerIndex(5,1);
270
    corners[5][2] = object.getCubitFaceStickerIndex(5,3);
271

    
272
    corners[6][0] = object.getCubitFaceStickerIndex(6,5);
273
    corners[6][1] = object.getCubitFaceStickerIndex(6,1);
274
    corners[6][2] = object.getCubitFaceStickerIndex(6,3);
275

    
276
    corners[7][0] = object.getCubitFaceStickerIndex(7,1);
277
    corners[7][1] = object.getCubitFaceStickerIndex(7,3);
278
    corners[7][2] = object.getCubitFaceStickerIndex(7,5);
279
    }
280

    
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282

    
283
  private void getEdges(TwistyObject object, int[][] edges)
284
    {
285
    edges[0][0] = object.getCubitFaceStickerIndex(8,5);
286
    edges[0][1] = object.getCubitFaceStickerIndex(8,3);
287
    edges[1][0] = object.getCubitFaceStickerIndex(9,3);
288
    edges[1][1] = object.getCubitFaceStickerIndex(9,5);
289
    edges[2][0] = object.getCubitFaceStickerIndex(10,3);
290
    edges[2][1] = object.getCubitFaceStickerIndex(10,5);
291
    edges[3][0] = object.getCubitFaceStickerIndex(11,5);
292
    edges[3][1] = object.getCubitFaceStickerIndex(11,3);
293
    edges[4][0] = object.getCubitFaceStickerIndex(12,3);
294
    edges[4][1] = object.getCubitFaceStickerIndex(12,5);
295
    edges[5][0] = object.getCubitFaceStickerIndex(13,5);
296
    edges[5][1] = object.getCubitFaceStickerIndex(13,3);
297
    edges[6][0] = object.getCubitFaceStickerIndex(14,3);
298
    edges[6][1] = object.getCubitFaceStickerIndex(14,5);
299
    edges[7][0] = object.getCubitFaceStickerIndex(15,5);
300
    edges[7][1] = object.getCubitFaceStickerIndex(15,3);
301
    }
302

    
303
///////////////////////////////////////////////////////////////////////////////////////////////////
304

    
305
  private void getCenters(TwistyObject object, int[] centers)
306
    {
307
    centers[0] = object.getCubitFaceStickerIndex(16,4);
308
    centers[1] = object.getCubitFaceStickerIndex(17,4);
309
    }
310

    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312

    
313
  public int tablebaseIndex(TwistyObject object)
314
    {
315
    int[][] corners= new int[8][3];
316
    int[][] edges  = new int[8][2];
317
    int[] centers  = new int[2];
318

    
319
    getCorners(object,corners);
320
    getEdges(object,edges);
321
    getCenters(object,centers);
322

    
323
//for(int i=0; i<8; i++) android.util.Log.e("D", "corner: "+i+" : "+corners[i][0]+" "+corners[i][1]+" "+corners[i][2]);
324
//for(int i=0; i<8; i++) android.util.Log.e("D", "edge: "+i+" : "+edges[i][0]+" "+edges[i][1]);
325

    
326
    int result0 = computeFaceColors(corners, edges, centers);
327
    if( result0<0 ) return result0;
328

    
329
    int[] corner_perm = new int[8];
330
    int result1 = retCornerPermutation(corner_perm,corners);
331
    if( result1<0 ) return result1;
332

    
333
//android.util.Log.e("D", "upper: "+mUpper);
334
for(int i=0; i<6; i++) android.util.Log.e("D", "face color: "+mFaceColors[i]);
335

    
336
    int[] edge_perm8 = new int[8];
337
    int result2 = retEdgePermutation(edge_perm8,edges);
338
    if( result2<0 ) return result2;
339

    
340
    TablebaseHelpers.displayTable(corner_perm, "CORNER PERM (before norm)");
341
    TablebaseHelpers.displayTable(edge_perm8, "EDGE PERM8 (before norm)");
342

    
343
    normalizePermutations(corner_perm,edge_perm8);
344

    
345
    TablebaseHelpers.displayTable(corner_perm, "CORNER PERM (after norm)");
346
    TablebaseHelpers.displayTable(edge_perm8, "EDGE PERM8 (after norm)");
347

    
348
    int[] edge_perm7 = TBCuboid323.edgePermTo7(edge_perm8);
349

    
350
TablebaseHelpers.displayTable(edge_perm7, "EDGE PERM7");
351

    
352
    int corner_perm_num = TablebaseHelpers.computePermutationNum(corner_perm);
353
    int edge_perm_num = TablebaseHelpers.computePermutationNum(edge_perm7);
354
    boolean inPlace = TBCuboid323.isFrontEdgeInItsPlace(edge_perm8);
355

    
356
android.util.Log.e("D", "corner_perm_num: "+corner_perm_num+" edge_perm_num: "+edge_perm_num+" inPlace: "+inPlace);
357
android.util.Log.e("D", "index="+(corner_perm_num + 40320*( (inPlace?0:1) + 2*edge_perm_num)));
358

    
359
    TBCuboid323.initialize();
360

    
361
    return corner_perm_num + 40320*( (inPlace?0:1) + 2*edge_perm_num);
362
    }
363

    
364
///////////////////////////////////////////////////////////////////////////////////////////////////
365

    
366
  private int getColorIndex4(int color)
367
    {
368
    switch(color)
369
      {
370
      case 0: return R.string.color_yellow4;
371
      case 1: return R.string.color_white4;
372
      case 2: return R.string.color_blue4;
373
      case 3: return R.string.color_green4;
374
      case 4: return R.string.color_red4;
375
      case 5: return R.string.color_orange4;
376
      }
377

    
378
    return -1;
379
    }
380

    
381
///////////////////////////////////////////////////////////////////////////////////////////////////
382

    
383
  private int getColorIndex3(int color)
384
    {
385
    switch(color)
386
      {
387
      case 0: return R.string.color_yellow3;
388
      case 1: return R.string.color_white3;
389
      case 2: return R.string.color_blue3;
390
      case 3: return R.string.color_green3;
391
      case 4: return R.string.color_red3;
392
      case 5: return R.string.color_orange3;
393
      }
394

    
395
    return -1;
396
    }
397

    
398
///////////////////////////////////////////////////////////////////////////////////////////////////
399

    
400
  private int getColorIndex5(int color)
401
    {
402
    switch(color)
403
      {
404
      case 0: return R.string.color_yellow5;
405
      case 1: return R.string.color_white5;
406
      case 2: return R.string.color_blue5;
407
      case 3: return R.string.color_green5;
408
      case 4: return R.string.color_red5;
409
      case 5: return R.string.color_orange5;
410
      }
411

    
412
    return -1;
413
    }
414

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

    
417
  private int getColorIndex6(int color)
418
    {
419
    switch(color)
420
      {
421
      case 0: return R.string.color_yellow6;
422
      case 1: return R.string.color_white6;
423
      case 2: return R.string.color_blue6;
424
      case 3: return R.string.color_green6;
425
      case 4: return R.string.color_red6;
426
      case 5: return R.string.color_orange6;
427
      }
428

    
429
    return -1;
430
    }
431

    
432
///////////////////////////////////////////////////////////////////////////////////////////////////
433

    
434
  private String edgeTwistedError(Resources res, int color0, int color1)
435
    {
436
    int j0 = getColorIndex3(color0);
437
    int j1 = getColorIndex6(color1);
438

    
439
    String c0 = res.getString(j0);
440
    String c1 = res.getString(j1);
441

    
442
    return res.getString(R.string.solver_generic_twisted_edge,c0,c1);
443
    }
444

    
445
///////////////////////////////////////////////////////////////////////////////////////////////////
446

    
447
  private String cornerTwistedError(Resources res, int color0, int color1, int color2)
448
    {
449
    int j0 = getColorIndex3(color0);
450
    int j1 = getColorIndex3(color1);
451
    int j2 = getColorIndex5(color2);
452

    
453
    String c0 = res.getString(j0);
454
    String c1 = res.getString(j1);
455
    String c2 = res.getString(j2);
456

    
457
    return res.getString(R.string.solver_generic_twisted_corner,c0,c1,c2);
458
    }
459

    
460
///////////////////////////////////////////////////////////////////////////////////////////////////
461

    
462
  private String edgeMissingError(Resources res, int color0, int color1)
463
    {
464
    int j0 = getColorIndex3(color0);
465
    int j1 = getColorIndex6(color1);
466

    
467
    String c0 = res.getString(j0);
468
    String c1 = res.getString(j1);
469

    
470
    return res.getString(R.string.solver_generic_missing_edge,c0,c1);
471
    }
472

    
473
///////////////////////////////////////////////////////////////////////////////////////////////////
474

    
475
  private String cornerMissingError(Resources res, int color0, int color1, int color2)
476
    {
477
    int j0 = getColorIndex3(color0);
478
    int j1 = getColorIndex3(color1);
479
    int j2 = getColorIndex4(color2);
480

    
481
    String c0 = res.getString(j0);
482
    String c1 = res.getString(j1);
483
    String c2 = res.getString(j2);
484

    
485
    return res.getString(R.string.solver_generic_missing_corner,c0,c1,c2);
486
    }
487

    
488
///////////////////////////////////////////////////////////////////////////////////////////////////
489

    
490
  public String error(int index, Resources res)
491
    {
492
    switch(index)
493
      {
494
      case ERROR_CORNER_MISSING : return cornerMissingError(res,mErrorColor1,mErrorColor2,mErrorColor3);
495
      case ERROR_EDGE_MISSING   : return edgeMissingError(res,mErrorColor1,mErrorColor2);
496
      case ERROR_CORNERS_CANNOT : return res.getString(R.string.solver_generic_corners_cannot);
497
      case ERROR_EDGE_TWISTED   : return edgeTwistedError(res,mErrorColor1,mErrorColor2);
498
      case ERROR_CORNER_TWISTED : return cornerTwistedError(res,mErrorColor1,mErrorColor2,mErrorColor3);
499
      }
500

    
501
    return null;
502
    }
503

    
504
///////////////////////////////////////////////////////////////////////////////////////////////////
505

    
506
  public int[][] solution(int index, OperatingSystemInterface os)
507
    {
508
    if( mSolver==null )
509
      {
510
      mSolver = ImplementedTablebasesList.createPacked(os,ObjectSignatures.CU_323);
511
      //mSolver = ImplementedTablebasesList.createUnpacked(ObjectSignatures.CU_323);
512
      //if( mSolver!=null ) mSolver.createTablebase(1);
513
      }
514

    
515
    return mSolver!=null ? mSolver.solution(index,null,os) : null;
516
    }
517
}  
518

    
(5-5/16)