Project

General

Profile

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

magiccube / src / main / java / org / distorted / solvers / SolverTablebaseCU323.java @ c65092dd

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.TwistyObject;
17
import org.distorted.objectlib.tablebases.TBCuboid323;
18
import org.distorted.objectlib.tablebases.TablebaseHelpers;
19

    
20
///////////////////////////////////////////////////////////////////////////////////////////////////
21
// a few cu_323 max (depth 18) indices: 1180633, 1180642, 1182044, 1190482, 128151851, 128190028
22

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

    
31
  private final int[] mFaceColors;
32
  private int mErrorColor1, mErrorColor2, mErrorColor3;
33

    
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35

    
36
  public SolverTablebaseCU323(OperatingSystemInterface os, Resources res, TwistyObject object)
37
    {
38
    super(os,res,object);
39
    mFaceColors = new int[6];
40
    }
41

    
42
////////////////////////////////////////////////////////////////////////////////////////
43

    
44
  private int findCorner(int[][] corners, int c1, int c2)
45
    {
46
    for(int i=0; i<8; i++)
47
      {
48
      int[] c = corners[i];
49

    
50
      if( c[0]==c1 && c[1]==c2 ) return c[2];
51
      if( c[1]==c1 && c[2]==c2 ) return c[0];
52
      if( c[2]==c1 && c[0]==c2 ) return c[1];
53
      }
54

    
55
    return ERROR_CORNERS_CANNOT;
56
    }
57

    
58
////////////////////////////////////////////////////////////////////////////////////////
59

    
60
  private int missingColor()
61
    {
62
    boolean[] present = new boolean[6];
63
    for(int i=0; i<5; i++) present[mFaceColors[i]] = true;
64

    
65
    int indexFalse = -1;
66

    
67
    for(int i=0; i<6; i++)
68
      if( !present[i] )
69
        {
70
        if( indexFalse<0 ) indexFalse=i;
71
        else return ERROR_CORNERS_CANNOT;
72
        }
73

    
74
    return indexFalse;
75
    }
76

    
77
////////////////////////////////////////////////////////////////////////////////////////
78

    
79
  private int edgePresent(int[][] edges, int f0, int f1)
80
    {
81
    int c0 = mFaceColors[f0];
82
    int c1 = mFaceColors[f1];
83

    
84
    for(int i=0; i<8; i++ )
85
      {
86
      int[] edge = edges[i];
87

    
88
      if( edge[0]==c0 && edge[1]==c1 ) return i;
89
      if( edge[0]==c1 && edge[1]==c0 )
90
        {
91
        mErrorColor1 = c0;
92
        mErrorColor2 = c1;
93
        return ERROR_EDGE_TWISTED;
94
        }
95
      }
96

    
97
    mErrorColor1 = c0;
98
    mErrorColor2 = c1;
99
    return ERROR_EDGE_MISSING;
100
    }
101

    
102
////////////////////////////////////////////////////////////////////////////////////////
103

    
104
  private int cornerPresent(int[][] corners, int f0, int f1, int f2)
105
    {
106
    int c0 = mFaceColors[f0];
107
    int c1 = mFaceColors[f1];
108
    int c2 = mFaceColors[f2];
109

    
110
    for(int i=0; i<8; i++)
111
      {
112
      int[] corner = corners[i];
113

    
114
      if(  corner[0]==c0 && corner[1]==c1 && corner[2]==c2 ) return i;
115
      if( (corner[0]==c1 && corner[1]==c2 && corner[2]==c0 ) ||
116
          (corner[0]==c2 && corner[1]==c0 && corner[2]==c1 )  )
117
        {
118
        mErrorColor1 = c0;
119
        mErrorColor2 = c1;
120
        mErrorColor3 = c2;
121
        return ERROR_CORNER_TWISTED;
122
        }
123
      }
124

    
125
    mErrorColor1 = c0;
126
    mErrorColor2 = c1;
127
    mErrorColor3 = c2;
128
    return ERROR_CORNER_MISSING;
129
    }
130

    
131
////////////////////////////////////////////////////////////////////////////////////////
132

    
133
  private int retEdgePermutation(int[] output, int[][] edges)
134
    {
135
    int[][] e = { {5,3}, {4,3}, {5,2}, {4,2}, {1,3}, {1,2}, {0,3}, {0,2} };
136

    
137
    for(int i=0; i<8; i++)
138
      {
139
      int[] ee = e[i];
140
      output[i] = edgePresent(edges,ee[0],ee[1]);
141
      if( output[i]<0 ) return output[i];
142
      }
143

    
144
    return 0;
145
    }
146

    
147
////////////////////////////////////////////////////////////////////////////////////////
148

    
149
  private int retCornerPermutation(int[] output, int[][] corners)
150
    {
151
    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} };
152

    
153
    for(int i=0; i<8; i++)
154
      {
155
      int[] cc = c[i];
156
      output[i] = cornerPresent(corners,cc[0],cc[1],cc[2]);
157
      if( output[i]<0 ) return output[i];
158
      }
159

    
160
    return 0;
161
    }
162

    
163
////////////////////////////////////////////////////////////////////////////////////////
164

    
165
  private int computeFaceColors(int[][] corners, int[][] edges, int[] centers)
166
    {
167
    mFaceColors[4] = edges[1][0];
168
    mFaceColors[3] = edges[1][1];
169

    
170
         if( centers[0]!=mFaceColors[3] ) mFaceColors[2] = centers[0];
171
    else if( centers[1]!=mFaceColors[3] ) mFaceColors[2] = centers[1];
172
    else return ERROR_CORNERS_CANNOT;
173

    
174
    mFaceColors[0] = findCorner(corners,mFaceColors[4],mFaceColors[2]);
175
    if( mFaceColors[0]<0 ) return mFaceColors[0];
176

    
177
    mFaceColors[1] = findCorner(corners,mFaceColors[2],mFaceColors[4]);
178
    if( mFaceColors[1]<0 ) return mFaceColors[1];
179

    
180
    mFaceColors[5] = missingColor();
181
    if( mFaceColors[5]<0 ) return mFaceColors[5];
182

    
183
    return 0;
184
    }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

    
188
  private void getCorners(TwistyObject object, int[][] corners)
189
    {
190
    corners[0][0] = object.getCubitFaceStickerIndex(0,5);
191
    corners[0][1] = object.getCubitFaceStickerIndex(0,1);
192
    corners[0][2] = object.getCubitFaceStickerIndex(0,3);
193

    
194
    corners[1][0] = object.getCubitFaceStickerIndex(1,3);
195
    corners[1][1] = object.getCubitFaceStickerIndex(1,5);
196
    corners[1][2] = object.getCubitFaceStickerIndex(1,1);
197

    
198
    corners[2][0] = object.getCubitFaceStickerIndex(2,5);
199
    corners[2][1] = object.getCubitFaceStickerIndex(2,1);
200
    corners[2][2] = object.getCubitFaceStickerIndex(2,3);
201

    
202
    corners[3][0] = object.getCubitFaceStickerIndex(3,5);
203
    corners[3][1] = object.getCubitFaceStickerIndex(3,1);
204
    corners[3][2] = object.getCubitFaceStickerIndex(3,3);
205

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

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

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

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

    
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224

    
225
  private void getEdges(TwistyObject object, int[][] edges)
226
    {
227
    edges[0][0] = object.getCubitFaceStickerIndex(8,5);
228
    edges[0][1] = object.getCubitFaceStickerIndex(8,3);
229
    edges[1][0] = object.getCubitFaceStickerIndex(9,3);
230
    edges[1][1] = object.getCubitFaceStickerIndex(9,5);
231
    edges[2][0] = object.getCubitFaceStickerIndex(10,3);
232
    edges[2][1] = object.getCubitFaceStickerIndex(10,5);
233
    edges[3][0] = object.getCubitFaceStickerIndex(11,5);
234
    edges[3][1] = object.getCubitFaceStickerIndex(11,3);
235
    edges[4][0] = object.getCubitFaceStickerIndex(12,3);
236
    edges[4][1] = object.getCubitFaceStickerIndex(12,5);
237
    edges[5][0] = object.getCubitFaceStickerIndex(13,5);
238
    edges[5][1] = object.getCubitFaceStickerIndex(13,3);
239
    edges[6][0] = object.getCubitFaceStickerIndex(14,3);
240
    edges[6][1] = object.getCubitFaceStickerIndex(14,5);
241
    edges[7][0] = object.getCubitFaceStickerIndex(15,5);
242
    edges[7][1] = object.getCubitFaceStickerIndex(15,3);
243
    }
244

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

    
247
  private void getCenters(TwistyObject object, int[] centers)
248
    {
249
    centers[0] = object.getCubitFaceStickerIndex(16,4);
250
    centers[1] = object.getCubitFaceStickerIndex(17,4);
251
    }
252

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254

    
255
  public int tablebaseIndex(TwistyObject object)
256
    {
257
    int[][] corners= new int[8][3];
258
    int[][] edges  = new int[8][2];
259
    int[] centers  = new int[2];
260

    
261
    getCorners(object,corners);
262
    getEdges(object,edges);
263
    getCenters(object,centers);
264

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

    
268
    int result0 = computeFaceColors(corners, edges, centers);
269
    if( result0<0 ) return result0;
270

    
271
    int[] corner_perm = new int[8];
272
    int result1 = retCornerPermutation(corner_perm,corners);
273
    if( result1<0 ) return result1;
274

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

    
278
    int[] edge_perm8 = new int[8];
279
    int result2 = retEdgePermutation(edge_perm8,edges);
280
    if( result2<0 ) return result2;
281

    
282
    int[] edge_perm7 = TBCuboid323.edgePermTo7(edge_perm8);
283
/*
284
    TablebaseHelpers.displayTable(corner_perm, "CORNER PERM");
285
    TablebaseHelpers.displayTable(edge_perm8, "EDGE PERM8");
286
    TablebaseHelpers.displayTable(edge_perm7, "EDGE PERM7");
287
*/
288
    int corner_perm_num = TablebaseHelpers.computePermutationNum(corner_perm);
289
    int edge_perm_num = TablebaseHelpers.computePermutationNum(edge_perm7);
290
    int centersInPlace = (centers[0]==mFaceColors[2]) ? 0 : 1;
291

    
292
//android.util.Log.e("D", "corner_perm_num: "+corner_perm_num+" edge_perm_num: "+edge_perm_num+" inPlace: "+centersInPlace);
293
//android.util.Log.e("D", "index="+(corner_perm_num + 40320*( centersInPlace + 2*edge_perm_num)));
294

    
295
    return corner_perm_num + 40320*( centersInPlace + 2*edge_perm_num);
296
    }
297

    
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299

    
300
  private String edgeTwistedError(Resources res, int color0, int color1)
301
    {
302
    int j0 = getHexColor(color0,3);
303
    int j1 = getHexColor(color1,6);
304

    
305
    String c0 = res.getString(j0);
306
    String c1 = res.getString(j1);
307

    
308
    return res.getString(R.string.solver_generic_twisted_edge,c0,c1);
309
    }
310

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

    
313
  private String cornerTwistedError(Resources res, int color0, int color1, int color2)
314
    {
315
    int j0 = getHexColor(color0,3);
316
    int j1 = getHexColor(color1,3);
317
    int j2 = getHexColor(color2,5);
318

    
319
    String c0 = res.getString(j0);
320
    String c1 = res.getString(j1);
321
    String c2 = res.getString(j2);
322

    
323
    return res.getString(R.string.solver_generic_twisted_corner,c0,c1,c2);
324
    }
325

    
326
///////////////////////////////////////////////////////////////////////////////////////////////////
327

    
328
  private String edgeMissingError(Resources res, int color0, int color1)
329
    {
330
    int j0 = getHexColor(color0,3);
331
    int j1 = getHexColor(color1,6);
332

    
333
    String c0 = res.getString(j0);
334
    String c1 = res.getString(j1);
335

    
336
    return res.getString(R.string.solver_generic_missing_edge,c0,c1);
337
    }
338

    
339
///////////////////////////////////////////////////////////////////////////////////////////////////
340

    
341
  private String cornerMissingError(Resources res, int color0, int color1, int color2)
342
    {
343
    int j0 = getHexColor(color0,3);
344
    int j1 = getHexColor(color1,3);
345
    int j2 = getHexColor(color2,4);
346

    
347
    String c0 = res.getString(j0);
348
    String c1 = res.getString(j1);
349
    String c2 = res.getString(j2);
350

    
351
    return res.getString(R.string.solver_generic_missing_corner,c0,c1,c2);
352
    }
353

    
354
///////////////////////////////////////////////////////////////////////////////////////////////////
355

    
356
  public String error(int index, Resources res)
357
    {
358
    switch(index)
359
      {
360
      case ERROR_CORNER_MISSING : return cornerMissingError(res,mErrorColor1,mErrorColor2,mErrorColor3);
361
      case ERROR_EDGE_MISSING   : return edgeMissingError(res,mErrorColor1,mErrorColor2);
362
      case ERROR_CORNERS_CANNOT : return res.getString(R.string.solver_generic_corners_cannot);
363
      case ERROR_EDGE_TWISTED   : return edgeTwistedError(res,mErrorColor1,mErrorColor2);
364
      case ERROR_CORNER_TWISTED : return cornerTwistedError(res,mErrorColor1,mErrorColor2,mErrorColor3);
365
      }
366

    
367
    return null;
368
    }
369
}  
370

    
(4-4/17)