Project

General

Profile

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

magiccube / src / main / java / org / distorted / solvers / SolverCuboid323.java @ 6ddc74e3

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.helpers.OperatingSystemInterface;
16
import org.distorted.objectlib.main.ObjectSignatures;
17
import org.distorted.objectlib.main.TwistyObject;
18
import org.distorted.objectlib.tablebases.ImplementedTablebasesList;
19
import org.distorted.objectlib.tablebases.TBCuboid323;
20
import org.distorted.objectlib.tablebases.TablebaseHelpers;
21
import org.distorted.objectlib.tablebases.TablebasesAbstract;
22

    
23
///////////////////////////////////////////////////////////////////////////////////////////////////
24

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

    
33
  TablebasesAbstract mSolver;
34
  private final int[] mFaceColors;
35
  private int mErrorColor1, mErrorColor2, mErrorColor3;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
  private boolean isFrontEdgeInItsPlace(int[][] edges)
40
    {
41
    return edges[1][1]==mFaceColors[3];
42
    }
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

    
46
  public SolverCuboid323(OperatingSystemInterface os, Resources res, TwistyObject object)
47
    {
48
    super(os,res,object);
49
    mFaceColors = new int[6];
50
    }
51

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

    
54
  private int findCorner(int[][] corners, int c1, int c2)
55
    {
56
    for(int i=0; i<8; i++)
57
      {
58
      int[] c = corners[i];
59

    
60
      if( c[0]==c1 && c[1]==c2 ) return c[2];
61
      if( c[1]==c1 && c[2]==c2 ) return c[0];
62
      if( c[2]==c1 && c[0]==c2 ) return c[1];
63
      }
64

    
65
    return ERROR_CORNERS_CANNOT;
66
    }
67

    
68
////////////////////////////////////////////////////////////////////////////////////////
69

    
70
  private int missingColor()
71
    {
72
    boolean[] present = new boolean[6];
73
    for(int i=0; i<5; i++) present[mFaceColors[i]] = true;
74

    
75
    int indexFalse = -1;
76

    
77
    for(int i=0; i<6; i++)
78
      if( !present[i] )
79
        {
80
        if( indexFalse<0 ) indexFalse=i;
81
        else return ERROR_CORNERS_CANNOT;
82
        }
83

    
84
    return indexFalse;
85
    }
86

    
87
////////////////////////////////////////////////////////////////////////////////////////
88

    
89
  private int edgePresent(int[][] edges, int f0, int f1)
90
    {
91
    int c0 = mFaceColors[f0];
92
    int c1 = mFaceColors[f1];
93

    
94
    for(int i=0; i<8; i++ )
95
      {
96
      int[] edge = edges[i];
97

    
98
      if( edge[0]==c0 && edge[1]==c1 ) return i;
99
      if( edge[0]==c1 && edge[1]==c0 )
100
        {
101
        mErrorColor1 = c0;
102
        mErrorColor2 = c1;
103
        return ERROR_EDGE_TWISTED;
104
        }
105
      }
106

    
107
    mErrorColor1 = c0;
108
    mErrorColor2 = c1;
109
    return ERROR_EDGE_MISSING;
110
    }
111

    
112
////////////////////////////////////////////////////////////////////////////////////////
113

    
114
  private int cornerPresent(int[][] corners, int f0, int f1, int f2)
115
    {
116
    int c0 = mFaceColors[f0];
117
    int c1 = mFaceColors[f1];
118
    int c2 = mFaceColors[f2];
119

    
120
    for(int i=0; i<8; i++)
121
      {
122
      int[] corner = corners[i];
123

    
124
      if(  corner[0]==c0 && corner[1]==c1 && corner[2]==c2 ) return i;
125
      if( (corner[0]==c1 && corner[1]==c2 && corner[2]==c0 ) ||
126
          (corner[0]==c2 && corner[1]==c0 && corner[2]==c1 )  )
127
        {
128
        mErrorColor1 = c0;
129
        mErrorColor2 = c1;
130
        mErrorColor3 = c2;
131
        return ERROR_CORNER_TWISTED;
132
        }
133
      }
134

    
135
    mErrorColor1 = c0;
136
    mErrorColor2 = c1;
137
    mErrorColor3 = c2;
138
    return ERROR_CORNER_MISSING;
139
    }
140

    
141
////////////////////////////////////////////////////////////////////////////////////////
142

    
143
  private int retEdgePermutation(int[] output, int[][] edges)
144
    {
145
    int[][] e = { {5,3}, {4,3}, {5,2}, {4,2}, {1,3}, {1,2}, {0,3}, {0,2} };
146

    
147
    for(int i=0; i<8; i++)
148
      {
149
      int[] ee = e[i];
150
      output[i] = edgePresent(edges,ee[0],ee[1]);
151
      if( output[i]<0 ) return output[i];
152
      }
153

    
154
    return 0;
155
    }
156

    
157
////////////////////////////////////////////////////////////////////////////////////////
158

    
159
  private int retCornerPermutation(int[] output, int[][] corners)
160
    {
161
    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} };
162

    
163
    for(int i=0; i<8; i++)
164
      {
165
      int[] cc = c[i];
166
      output[i] = cornerPresent(corners,cc[0],cc[1],cc[2]);
167
      if( output[i]<0 ) return output[i];
168
      }
169

    
170
    return 0;
171
    }
172

    
173
////////////////////////////////////////////////////////////////////////////////////////
174

    
175
  private int computeFaceColors(int[][] corners, int[][] edges, int[] centers)
176
    {
177
    mFaceColors[2] = centers[0];
178
    mFaceColors[3] = centers[1];
179

    
180
    if( edges[1][1]==mFaceColors[2] || edges[1][1]==mFaceColors[3] )
181
      {
182
      mFaceColors[4] = edges[1][0];
183
      }
184
    else return ERROR_CORNERS_CANNOT;
185

    
186
    mFaceColors[0] = findCorner(corners,mFaceColors[4],mFaceColors[2]);
187
    if( mFaceColors[0]<0 ) return mFaceColors[0];
188

    
189
    mFaceColors[1] = findCorner(corners,mFaceColors[2],mFaceColors[4]);
190
    if( mFaceColors[1]<0 ) return mFaceColors[1];
191

    
192
    mFaceColors[5] = missingColor();
193
    if( mFaceColors[5]<0 ) return mFaceColors[5];
194

    
195
    return 0;
196
    }
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

    
200
  private void getCorners(TwistyObject object, int[][] corners)
201
    {
202
    corners[0][0] = object.getCubitFaceStickerIndex(0,5);
203
    corners[0][1] = object.getCubitFaceStickerIndex(0,1);
204
    corners[0][2] = object.getCubitFaceStickerIndex(0,3);
205

    
206
    corners[1][0] = object.getCubitFaceStickerIndex(1,3);
207
    corners[1][1] = object.getCubitFaceStickerIndex(1,5);
208
    corners[1][2] = object.getCubitFaceStickerIndex(1,1);
209

    
210
    corners[2][0] = object.getCubitFaceStickerIndex(2,5);
211
    corners[2][1] = object.getCubitFaceStickerIndex(2,1);
212
    corners[2][2] = object.getCubitFaceStickerIndex(2,3);
213

    
214
    corners[3][0] = object.getCubitFaceStickerIndex(3,5);
215
    corners[3][1] = object.getCubitFaceStickerIndex(3,1);
216
    corners[3][2] = object.getCubitFaceStickerIndex(3,3);
217

    
218
    corners[4][0] = object.getCubitFaceStickerIndex(4,1);
219
    corners[4][1] = object.getCubitFaceStickerIndex(4,3);
220
    corners[4][2] = object.getCubitFaceStickerIndex(4,5);
221

    
222
    corners[5][0] = object.getCubitFaceStickerIndex(5,5);
223
    corners[5][1] = object.getCubitFaceStickerIndex(5,1);
224
    corners[5][2] = object.getCubitFaceStickerIndex(5,3);
225

    
226
    corners[6][0] = object.getCubitFaceStickerIndex(6,5);
227
    corners[6][1] = object.getCubitFaceStickerIndex(6,1);
228
    corners[6][2] = object.getCubitFaceStickerIndex(6,3);
229

    
230
    corners[7][0] = object.getCubitFaceStickerIndex(7,1);
231
    corners[7][1] = object.getCubitFaceStickerIndex(7,3);
232
    corners[7][2] = object.getCubitFaceStickerIndex(7,5);
233
    }
234

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

    
237
  private void getEdges(TwistyObject object, int[][] edges)
238
    {
239
    edges[0][0] = object.getCubitFaceStickerIndex(8,5);
240
    edges[0][1] = object.getCubitFaceStickerIndex(8,3);
241
    edges[1][0] = object.getCubitFaceStickerIndex(9,3);
242
    edges[1][1] = object.getCubitFaceStickerIndex(9,5);
243
    edges[2][0] = object.getCubitFaceStickerIndex(10,3);
244
    edges[2][1] = object.getCubitFaceStickerIndex(10,5);
245
    edges[3][0] = object.getCubitFaceStickerIndex(11,5);
246
    edges[3][1] = object.getCubitFaceStickerIndex(11,3);
247
    edges[4][0] = object.getCubitFaceStickerIndex(12,3);
248
    edges[4][1] = object.getCubitFaceStickerIndex(12,5);
249
    edges[5][0] = object.getCubitFaceStickerIndex(13,5);
250
    edges[5][1] = object.getCubitFaceStickerIndex(13,3);
251
    edges[6][0] = object.getCubitFaceStickerIndex(14,3);
252
    edges[6][1] = object.getCubitFaceStickerIndex(14,5);
253
    edges[7][0] = object.getCubitFaceStickerIndex(15,5);
254
    edges[7][1] = object.getCubitFaceStickerIndex(15,3);
255
    }
256

    
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258

    
259
  private void getCenters(TwistyObject object, int[] centers)
260
    {
261
    centers[0] = object.getCubitFaceStickerIndex(16,4);
262
    centers[1] = object.getCubitFaceStickerIndex(17,4);
263
    }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

    
267
  public int tablebaseIndex(TwistyObject object)
268
    {
269
    int[][] corners= new int[8][3];
270
    int[][] edges  = new int[8][2];
271
    int[] centers  = new int[2];
272

    
273
    getCorners(object,corners);
274
    getEdges(object,edges);
275
    getCenters(object,centers);
276

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

    
280
    int result0 = computeFaceColors(corners, edges, centers);
281
    if( result0<0 ) return result0;
282

    
283
    int[] corner_perm = new int[8];
284
    int result1 = retCornerPermutation(corner_perm,corners);
285
    if( result1<0 ) return result1;
286

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

    
290
    int[] edge_perm = new int[8];
291
    int result2 = retEdgePermutation(edge_perm,edges);
292
    if( result2<0 ) return result2;
293

    
294
//    TablebaseHelpers.displayTable(corner_perm, "CORNER PERM");
295
//    TablebaseHelpers.displayTable(edge_perm, "EDGE PERM");
296

    
297
    int[] edge_perm2 = TBCuboid323.edgePermTo7(edge_perm);
298

    
299
    int corner_perm_num = TablebaseHelpers.computePermutationNum(corner_perm);
300
    int edge_perm_num = TablebaseHelpers.computePermutationNum(edge_perm2);
301
    boolean inPlace = isFrontEdgeInItsPlace(edges);
302

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

    
306
    TBCuboid323.initialize();
307

    
308
    return corner_perm_num + 40320*( (inPlace?0:1) + 2*edge_perm_num);
309
    }
310

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

    
313
  private int getColorIndex4(int color)
314
    {
315
    switch(color)
316
      {
317
      case 0: return R.string.color_yellow4;
318
      case 1: return R.string.color_white4;
319
      case 2: return R.string.color_blue4;
320
      case 3: return R.string.color_green4;
321
      case 4: return R.string.color_red4;
322
      case 5: return R.string.color_orange4;
323
      }
324

    
325
    return -1;
326
    }
327

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

    
330
  private int getColorIndex3(int color)
331
    {
332
    switch(color)
333
      {
334
      case 0: return R.string.color_yellow3;
335
      case 1: return R.string.color_white3;
336
      case 2: return R.string.color_blue3;
337
      case 3: return R.string.color_green3;
338
      case 4: return R.string.color_red3;
339
      case 5: return R.string.color_orange3;
340
      }
341

    
342
    return -1;
343
    }
344

    
345
///////////////////////////////////////////////////////////////////////////////////////////////////
346

    
347
  private int getColorIndex5(int color)
348
    {
349
    switch(color)
350
      {
351
      case 0: return R.string.color_yellow5;
352
      case 1: return R.string.color_white5;
353
      case 2: return R.string.color_blue5;
354
      case 3: return R.string.color_green5;
355
      case 4: return R.string.color_red5;
356
      case 5: return R.string.color_orange5;
357
      }
358

    
359
    return -1;
360
    }
361

    
362
///////////////////////////////////////////////////////////////////////////////////////////////////
363

    
364
  private int getColorIndex6(int color)
365
    {
366
    switch(color)
367
      {
368
      case 0: return R.string.color_yellow6;
369
      case 1: return R.string.color_white6;
370
      case 2: return R.string.color_blue6;
371
      case 3: return R.string.color_green6;
372
      case 4: return R.string.color_red6;
373
      case 5: return R.string.color_orange6;
374
      }
375

    
376
    return -1;
377
    }
378

    
379
///////////////////////////////////////////////////////////////////////////////////////////////////
380

    
381
  private String edgeTwistedError(Resources res, int color0, int color1)
382
    {
383
    int j0 = getColorIndex3(color0);
384
    int j1 = getColorIndex6(color1);
385

    
386
    String c0 = res.getString(j0);
387
    String c1 = res.getString(j1);
388

    
389
    return res.getString(R.string.solver_generic_twisted_edge,c0,c1);
390
    }
391

    
392
///////////////////////////////////////////////////////////////////////////////////////////////////
393

    
394
  private String cornerTwistedError(Resources res, int color0, int color1, int color2)
395
    {
396
    int j0 = getColorIndex3(color0);
397
    int j1 = getColorIndex3(color1);
398
    int j2 = getColorIndex5(color2);
399

    
400
    String c0 = res.getString(j0);
401
    String c1 = res.getString(j1);
402
    String c2 = res.getString(j2);
403

    
404
    return res.getString(R.string.solver_generic_twisted_corner,c0,c1,c2);
405
    }
406

    
407
///////////////////////////////////////////////////////////////////////////////////////////////////
408

    
409
  private String edgeMissingError(Resources res, int color0, int color1)
410
    {
411
    int j0 = getColorIndex3(color0);
412
    int j1 = getColorIndex6(color1);
413

    
414
    String c0 = res.getString(j0);
415
    String c1 = res.getString(j1);
416

    
417
    return res.getString(R.string.solver_generic_missing_edge,c0,c1);
418
    }
419

    
420
///////////////////////////////////////////////////////////////////////////////////////////////////
421

    
422
  private String cornerMissingError(Resources res, int color0, int color1, int color2)
423
    {
424
    int j0 = getColorIndex3(color0);
425
    int j1 = getColorIndex3(color1);
426
    int j2 = getColorIndex4(color2);
427

    
428
    String c0 = res.getString(j0);
429
    String c1 = res.getString(j1);
430
    String c2 = res.getString(j2);
431

    
432
    return res.getString(R.string.solver_generic_missing_corner,c0,c1,c2);
433
    }
434

    
435
///////////////////////////////////////////////////////////////////////////////////////////////////
436

    
437
  public String error(int index, Resources res)
438
    {
439
    switch(index)
440
      {
441
      case ERROR_CORNER_MISSING : return cornerMissingError(res,mErrorColor1,mErrorColor2,mErrorColor3);
442
      case ERROR_EDGE_MISSING   : return edgeMissingError(res,mErrorColor1,mErrorColor2);
443
      case ERROR_CORNERS_CANNOT : return res.getString(R.string.solver_generic_corners_cannot);
444
      case ERROR_EDGE_TWISTED   : return edgeTwistedError(res,mErrorColor1,mErrorColor2);
445
      case ERROR_CORNER_TWISTED : return cornerTwistedError(res,mErrorColor1,mErrorColor2,mErrorColor3);
446
      }
447

    
448
    return null;
449
    }
450

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

    
453
  public int[][] solution(int index, OperatingSystemInterface os)
454
    {
455
    if( mSolver==null )
456
      {
457
      mSolver = ImplementedTablebasesList.createPacked(os,ObjectSignatures.CU_323);
458
      //mSolver = ImplementedTablebasesList.createUnpacked(ObjectSignatures.CU_323);
459
      //if( mSolver!=null ) mSolver.createTablebase(1);
460
      }
461

    
462
    return mSolver!=null ? mSolver.solution(index,null,os) : null;
463
    }
464
}  
465

    
(5-5/16)