Project

General

Profile

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

magiccube / src / main / java / org / distorted / solvers / SolverCuboid323.java @ 8fca1c69

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 PERM8");
296

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

    
299
TablebaseHelpers.displayTable(edge_perm2, "EDGE PERM7");
300

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

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

    
308
    TBCuboid323.initialize();
309

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

    
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314

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

    
327
    return -1;
328
    }
329

    
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331

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

    
344
    return -1;
345
    }
346

    
347
///////////////////////////////////////////////////////////////////////////////////////////////////
348

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

    
361
    return -1;
362
    }
363

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

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

    
378
    return -1;
379
    }
380

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

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

    
388
    String c0 = res.getString(j0);
389
    String c1 = res.getString(j1);
390

    
391
    return res.getString(R.string.solver_generic_twisted_edge,c0,c1);
392
    }
393

    
394
///////////////////////////////////////////////////////////////////////////////////////////////////
395

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

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

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

    
409
///////////////////////////////////////////////////////////////////////////////////////////////////
410

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

    
416
    String c0 = res.getString(j0);
417
    String c1 = res.getString(j1);
418

    
419
    return res.getString(R.string.solver_generic_missing_edge,c0,c1);
420
    }
421

    
422
///////////////////////////////////////////////////////////////////////////////////////////////////
423

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

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

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

    
437
///////////////////////////////////////////////////////////////////////////////////////////////////
438

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

    
450
    return null;
451
    }
452

    
453
///////////////////////////////////////////////////////////////////////////////////////////////////
454

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

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

    
(5-5/16)