Project

General

Profile

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

magiccube / src / main / java / org / distorted / solvers / SolverCuboid323.java @ bfac9e97

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
  private boolean isFrontEdgeInItsPlace(int[][] edges)
93
    {
94
    return edges[1][1]==mFaceColors[3];
95
    }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

    
99
  public SolverCuboid323(OperatingSystemInterface os, Resources res, TwistyObject object)
100
    {
101
    super(os,res,object);
102
    mFaceColors = new int[6];
103
    }
104

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

    
107
  private int findCorner(int[][] corners, int c1, int c2)
108
    {
109
    for(int i=0; i<8; i++)
110
      {
111
      int[] c = corners[i];
112

    
113
      if( c[0]==c1 && c[1]==c2 ) return c[2];
114
      if( c[1]==c1 && c[2]==c2 ) return c[0];
115
      if( c[2]==c1 && c[0]==c2 ) return c[1];
116
      }
117

    
118
    return ERROR_CORNERS_CANNOT;
119
    }
120

    
121
////////////////////////////////////////////////////////////////////////////////////////
122

    
123
  private int missingColor()
124
    {
125
    boolean[] present = new boolean[6];
126
    for(int i=0; i<5; i++) present[mFaceColors[i]] = true;
127

    
128
    int indexFalse = -1;
129

    
130
    for(int i=0; i<6; i++)
131
      if( !present[i] )
132
        {
133
        if( indexFalse<0 ) indexFalse=i;
134
        else return ERROR_CORNERS_CANNOT;
135
        }
136

    
137
    return indexFalse;
138
    }
139

    
140
////////////////////////////////////////////////////////////////////////////////////////
141

    
142
  private int edgePresent(int[][] edges, int f0, int f1)
143
    {
144
    int c0 = mFaceColors[f0];
145
    int c1 = mFaceColors[f1];
146

    
147
    for(int i=0; i<8; i++ )
148
      {
149
      int[] edge = edges[i];
150

    
151
      if( edge[0]==c0 && edge[1]==c1 ) return i;
152
      if( edge[0]==c1 && edge[1]==c0 )
153
        {
154
        mErrorColor1 = c0;
155
        mErrorColor2 = c1;
156
        return ERROR_EDGE_TWISTED;
157
        }
158
      }
159

    
160
    mErrorColor1 = c0;
161
    mErrorColor2 = c1;
162
    return ERROR_EDGE_MISSING;
163
    }
164

    
165
////////////////////////////////////////////////////////////////////////////////////////
166

    
167
  private int cornerPresent(int[][] corners, int f0, int f1, int f2)
168
    {
169
    int c0 = mFaceColors[f0];
170
    int c1 = mFaceColors[f1];
171
    int c2 = mFaceColors[f2];
172

    
173
    for(int i=0; i<8; i++)
174
      {
175
      int[] corner = corners[i];
176

    
177
      if(  corner[0]==c0 && corner[1]==c1 && corner[2]==c2 ) return i;
178
      if( (corner[0]==c1 && corner[1]==c2 && corner[2]==c0 ) ||
179
          (corner[0]==c2 && corner[1]==c0 && corner[2]==c1 )  )
180
        {
181
        mErrorColor1 = c0;
182
        mErrorColor2 = c1;
183
        mErrorColor3 = c2;
184
        return ERROR_CORNER_TWISTED;
185
        }
186
      }
187

    
188
    mErrorColor1 = c0;
189
    mErrorColor2 = c1;
190
    mErrorColor3 = c2;
191
    return ERROR_CORNER_MISSING;
192
    }
193

    
194
////////////////////////////////////////////////////////////////////////////////////////
195

    
196
  private int retEdgePermutation(int[] output, int[][] edges)
197
    {
198
    int[][] e = { {5,3}, {4,3}, {5,2}, {4,2}, {1,3}, {1,2}, {0,3}, {0,2} };
199

    
200
    for(int i=0; i<8; i++)
201
      {
202
      int[] ee = e[i];
203
      output[i] = edgePresent(edges,ee[0],ee[1]);
204
      if( output[i]<0 ) return output[i];
205
      }
206

    
207
    return 0;
208
    }
209

    
210
////////////////////////////////////////////////////////////////////////////////////////
211

    
212
  private int retCornerPermutation(int[] output, int[][] corners)
213
    {
214
    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} };
215

    
216
    for(int i=0; i<8; i++)
217
      {
218
      int[] cc = c[i];
219
      output[i] = cornerPresent(corners,cc[0],cc[1],cc[2]);
220
      if( output[i]<0 ) return output[i];
221
      }
222

    
223
    return 0;
224
    }
225

    
226
////////////////////////////////////////////////////////////////////////////////////////
227

    
228
  private int computeFaceColors(int[][] corners, int[][] edges, int[] centers)
229
    {
230
    mFaceColors[2] = centers[0];
231
    mFaceColors[3] = centers[1];
232

    
233
    if( edges[1][1]==mFaceColors[2] || edges[1][1]==mFaceColors[3] )
234
      {
235
      mFaceColors[4] = edges[1][0];
236
      }
237
    else return ERROR_CORNERS_CANNOT;
238

    
239
    mFaceColors[0] = findCorner(corners,mFaceColors[4],mFaceColors[2]);
240
    if( mFaceColors[0]<0 ) return mFaceColors[0];
241

    
242
    mFaceColors[1] = findCorner(corners,mFaceColors[2],mFaceColors[4]);
243
    if( mFaceColors[1]<0 ) return mFaceColors[1];
244

    
245
    mFaceColors[5] = missingColor();
246
    if( mFaceColors[5]<0 ) return mFaceColors[5];
247

    
248
    return 0;
249
    }
250

    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252

    
253
  private void getCorners(TwistyObject object, int[][] corners)
254
    {
255
    corners[0][0] = object.getCubitFaceStickerIndex(0,5);
256
    corners[0][1] = object.getCubitFaceStickerIndex(0,1);
257
    corners[0][2] = object.getCubitFaceStickerIndex(0,3);
258

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

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

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

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

    
275
    corners[5][0] = object.getCubitFaceStickerIndex(5,5);
276
    corners[5][1] = object.getCubitFaceStickerIndex(5,1);
277
    corners[5][2] = object.getCubitFaceStickerIndex(5,3);
278

    
279
    corners[6][0] = object.getCubitFaceStickerIndex(6,5);
280
    corners[6][1] = object.getCubitFaceStickerIndex(6,1);
281
    corners[6][2] = object.getCubitFaceStickerIndex(6,3);
282

    
283
    corners[7][0] = object.getCubitFaceStickerIndex(7,1);
284
    corners[7][1] = object.getCubitFaceStickerIndex(7,3);
285
    corners[7][2] = object.getCubitFaceStickerIndex(7,5);
286
    }
287

    
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289

    
290
  private void getEdges(TwistyObject object, int[][] edges)
291
    {
292
    edges[0][0] = object.getCubitFaceStickerIndex(8,5);
293
    edges[0][1] = object.getCubitFaceStickerIndex(8,3);
294
    edges[1][0] = object.getCubitFaceStickerIndex(9,3);
295
    edges[1][1] = object.getCubitFaceStickerIndex(9,5);
296
    edges[2][0] = object.getCubitFaceStickerIndex(10,3);
297
    edges[2][1] = object.getCubitFaceStickerIndex(10,5);
298
    edges[3][0] = object.getCubitFaceStickerIndex(11,5);
299
    edges[3][1] = object.getCubitFaceStickerIndex(11,3);
300
    edges[4][0] = object.getCubitFaceStickerIndex(12,3);
301
    edges[4][1] = object.getCubitFaceStickerIndex(12,5);
302
    edges[5][0] = object.getCubitFaceStickerIndex(13,5);
303
    edges[5][1] = object.getCubitFaceStickerIndex(13,3);
304
    edges[6][0] = object.getCubitFaceStickerIndex(14,3);
305
    edges[6][1] = object.getCubitFaceStickerIndex(14,5);
306
    edges[7][0] = object.getCubitFaceStickerIndex(15,5);
307
    edges[7][1] = object.getCubitFaceStickerIndex(15,3);
308
    }
309

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

    
312
  private void getCenters(TwistyObject object, int[] centers)
313
    {
314
    centers[0] = object.getCubitFaceStickerIndex(16,4);
315
    centers[1] = object.getCubitFaceStickerIndex(17,4);
316
    }
317

    
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319

    
320
  public int tablebaseIndex(TwistyObject object)
321
    {
322
    int[][] corners= new int[8][3];
323
    int[][] edges  = new int[8][2];
324
    int[] centers  = new int[2];
325

    
326
    getCorners(object,corners);
327
    getEdges(object,edges);
328
    getCenters(object,centers);
329

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

    
333
    int result0 = computeFaceColors(corners, edges, centers);
334
    if( result0<0 ) return result0;
335

    
336
    int[] corner_perm = new int[8];
337
    int result1 = retCornerPermutation(corner_perm,corners);
338
    if( result1<0 ) return result1;
339

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

    
343
    int[] edge_perm8 = new int[8];
344
    int result2 = retEdgePermutation(edge_perm8,edges);
345
    if( result2<0 ) return result2;
346

    
347
    TablebaseHelpers.displayTable(corner_perm, "CORNER PERM");
348
    TablebaseHelpers.displayTable(edge_perm8, "EDGE PERM8");
349

    
350
    normalizePermutations(corner_perm,edge_perm8);
351

    
352
    int[] edge_perm7 = TBCuboid323.edgePermTo7(edge_perm8);
353

    
354
TablebaseHelpers.displayTable(edge_perm7, "EDGE PERM7");
355

    
356
    int corner_perm_num = TablebaseHelpers.computePermutationNum(corner_perm);
357
    int edge_perm_num = TablebaseHelpers.computePermutationNum(edge_perm7);
358
    boolean inPlace = isFrontEdgeInItsPlace(edges);
359

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

    
363
    TBCuboid323.initialize();
364

    
365
    return corner_perm_num + 40320*( (inPlace?0:1) + 2*edge_perm_num);
366
    }
367

    
368
///////////////////////////////////////////////////////////////////////////////////////////////////
369

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

    
382
    return -1;
383
    }
384

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

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

    
399
    return -1;
400
    }
401

    
402
///////////////////////////////////////////////////////////////////////////////////////////////////
403

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

    
416
    return -1;
417
    }
418

    
419
///////////////////////////////////////////////////////////////////////////////////////////////////
420

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

    
433
    return -1;
434
    }
435

    
436
///////////////////////////////////////////////////////////////////////////////////////////////////
437

    
438
  private String edgeTwistedError(Resources res, int color0, int color1)
439
    {
440
    int j0 = getColorIndex3(color0);
441
    int j1 = getColorIndex6(color1);
442

    
443
    String c0 = res.getString(j0);
444
    String c1 = res.getString(j1);
445

    
446
    return res.getString(R.string.solver_generic_twisted_edge,c0,c1);
447
    }
448

    
449
///////////////////////////////////////////////////////////////////////////////////////////////////
450

    
451
  private String cornerTwistedError(Resources res, int color0, int color1, int color2)
452
    {
453
    int j0 = getColorIndex3(color0);
454
    int j1 = getColorIndex3(color1);
455
    int j2 = getColorIndex5(color2);
456

    
457
    String c0 = res.getString(j0);
458
    String c1 = res.getString(j1);
459
    String c2 = res.getString(j2);
460

    
461
    return res.getString(R.string.solver_generic_twisted_corner,c0,c1,c2);
462
    }
463

    
464
///////////////////////////////////////////////////////////////////////////////////////////////////
465

    
466
  private String edgeMissingError(Resources res, int color0, int color1)
467
    {
468
    int j0 = getColorIndex3(color0);
469
    int j1 = getColorIndex6(color1);
470

    
471
    String c0 = res.getString(j0);
472
    String c1 = res.getString(j1);
473

    
474
    return res.getString(R.string.solver_generic_missing_edge,c0,c1);
475
    }
476

    
477
///////////////////////////////////////////////////////////////////////////////////////////////////
478

    
479
  private String cornerMissingError(Resources res, int color0, int color1, int color2)
480
    {
481
    int j0 = getColorIndex3(color0);
482
    int j1 = getColorIndex3(color1);
483
    int j2 = getColorIndex4(color2);
484

    
485
    String c0 = res.getString(j0);
486
    String c1 = res.getString(j1);
487
    String c2 = res.getString(j2);
488

    
489
    return res.getString(R.string.solver_generic_missing_corner,c0,c1,c2);
490
    }
491

    
492
///////////////////////////////////////////////////////////////////////////////////////////////////
493

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

    
505
    return null;
506
    }
507

    
508
///////////////////////////////////////////////////////////////////////////////////////////////////
509

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

    
519
    return mSolver!=null ? mSolver.solution(index,null,os) : null;
520
    }
521
}  
522

    
(5-5/16)