Project

General

Profile

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

magiccube / src / main / java / org / distorted / solvers / SolverTablebaseCUBE2.java @ 3bedda30

1 a4569df4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 da57afae Leszek Koltunski
import org.distorted.objectlib.helpers.OperatingSystemInterface;
16 a4569df4 Leszek Koltunski
import org.distorted.objectlib.main.TwistyObject;
17
import org.distorted.objectlib.tablebases.TablebaseHelpers;
18 f5e5e7a3 Leszek Koltunski
import org.distorted.objectlib.tablebases.TBCube2;
19 a4569df4 Leszek Koltunski
20
///////////////////////////////////////////////////////////////////////////////////////////////////
21
22 1d1f9ccf leszek
public class SolverTablebaseCUBE2 extends SolverTablebase
23 a4569df4 Leszek Koltunski
{
24
  private static final int ERROR_CORNER_135_MISSING = -1;
25
  private static final int ERROR_CORNER_134_MISSING = -2;
26
  private static final int ERROR_CORNER_125_MISSING = -3;
27
  private static final int ERROR_CORNER_124_MISSING = -4;
28
  private static final int ERROR_CORNER_035_MISSING = -5;
29
  private static final int ERROR_CORNER_034_MISSING = -6;
30
  private static final int ERROR_CORNER_025_MISSING = -7;
31
  private static final int ERROR_CORNER_024_MISSING = -8;
32
  private static final int ERROR_CORNERS_CANNOT     = -9;
33
  private static final int ERROR_CORNER_TWISTED     = -10;
34
35
  private final int[] mFaceColors;
36
37
////////////////////////////////////////////////////////////////////////////////////////
38
39
  private void fillCornerTwists(int[] output, int[][] corners, int[] perm)
40
    {
41 6f1dbce1 Leszek Koltunski
    for(int i=0; i<8; i++)
42
      {
43
      int[] c = corners[perm[i]];
44
45
           if( c[0]==mFaceColors[0] || c[0]==mFaceColors[1] ) output[i] = 0;
46
      else if( c[1]==mFaceColors[0] || c[1]==mFaceColors[1] ) output[i] = 1;
47
      else                                                    output[i] = 2;
48
      }
49 a4569df4 Leszek Koltunski
    }
50
51
////////////////////////////////////////////////////////////////////////////////////////
52
53
  private int cornerIndex(int[][] corners, int i1, int i2, int i3)
54
    {
55
    int c1 = mFaceColors[i1];
56
    int c2 = mFaceColors[i2];
57
    int c3 = mFaceColors[i3];
58
59
    for(int i=0; i<8; i++)
60
      {
61
      int[] cor = corners[i];
62
63
      if( (cor[0]==c1 && cor[1]==c2 && cor[2]==c3) ||
64
          (cor[0]==c2 && cor[1]==c3 && cor[2]==c1) ||
65
          (cor[0]==c3 && cor[1]==c1 && cor[2]==c2)  ) return i;
66
      }
67
68
    return -1;
69
    }
70
71
////////////////////////////////////////////////////////////////////////////////////////
72
73
  private int retCornerPermutation(int[] output, int[][] corners)
74
    {
75
    for(int i=0; i<8; i++) output[i] = -1;
76
77
    output[0] = cornerIndex(corners, 1,2,5);
78
    output[1] = cornerIndex(corners, 1,4,2);
79
    output[2] = cornerIndex(corners, 1,5,3);
80
    output[3] = cornerIndex(corners, 1,3,4);
81
    output[4] = cornerIndex(corners, 0,5,2);
82
    output[5] = cornerIndex(corners, 0,2,4);
83
    output[6] = cornerIndex(corners, 0,3,5);
84
    output[7] = cornerIndex(corners, 0,4,3);
85
86 6f1dbce1 Leszek Koltunski
    if( output[0]==-1 ) return ERROR_CORNER_125_MISSING;
87
    if( output[1]==-1 ) return ERROR_CORNER_124_MISSING;
88
    if( output[2]==-1 ) return ERROR_CORNER_135_MISSING;
89
    if( output[3]==-1 ) return ERROR_CORNER_134_MISSING;
90
    if( output[4]==-1 ) return ERROR_CORNER_025_MISSING;
91
    if( output[5]==-1 ) return ERROR_CORNER_024_MISSING;
92
    if( output[6]==-1 ) return ERROR_CORNER_035_MISSING;
93
    if( output[7]==-1 ) return ERROR_CORNER_034_MISSING;
94 a4569df4 Leszek Koltunski
95
    return 0;
96
    }
97
98
////////////////////////////////////////////////////////////////////////////////////////
99
100
  private int findColor(int[][] corners, int c1, int c2)
101
    {
102
    for(int i=0; i<8; i++)
103
      {
104
      int[] cor = corners[i];
105
106
      if( cor[0]==c1 && cor[1]==c2 ) return cor[2];
107
      if( cor[1]==c1 && cor[2]==c2 ) return cor[0];
108
      if( cor[2]==c1 && cor[0]==c2 ) return cor[1];
109
      }
110
111
    return -1;
112
    }
113
114
////////////////////////////////////////////////////////////////////////////////////////
115
116
  private int computeFaceColors(int[][] corners)
117
    {
118
    mFaceColors[1] = corners[1][0];
119
    mFaceColors[2] = corners[1][2];
120
    mFaceColors[4] = corners[1][1];
121
122
    mFaceColors[0] = findColor(corners,mFaceColors[2],mFaceColors[4]);
123
    mFaceColors[3] = findColor(corners,mFaceColors[4],mFaceColors[1]);
124
    mFaceColors[5] = findColor(corners,mFaceColors[1],mFaceColors[2]);
125
126
    for(int i=0; i<6; i++)
127
      {
128
      int color = mFaceColors[i];
129
      if( color<0 ) return ERROR_CORNERS_CANNOT;
130
131
      for(int j=i+1; j<6; j++)
132
        if( mFaceColors[j]==color ) return ERROR_CORNERS_CANNOT;
133
      }
134
135
    return 0;
136
    }
137
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139
140 1d1f9ccf leszek
  public SolverTablebaseCUBE2(OperatingSystemInterface os, Resources res, TwistyObject object)
141 a4569df4 Leszek Koltunski
    {
142 da57afae Leszek Koltunski
    super(os,res,object);
143 a4569df4 Leszek Koltunski
    mFaceColors = new int[6];
144
    }
145
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147
148 cff92952 Leszek Koltunski
  private void getCorners(TwistyObject object, int[][] corners)
149 a4569df4 Leszek Koltunski
    {
150
    corners[0][0] = object.getCubitFaceStickerIndex(0,1);
151
    corners[0][1] = object.getCubitFaceStickerIndex(0,3);
152
    corners[0][2] = object.getCubitFaceStickerIndex(0,5);
153
154
    corners[1][0] = object.getCubitFaceStickerIndex(1,3);
155
    corners[1][1] = object.getCubitFaceStickerIndex(1,5);
156
    corners[1][2] = object.getCubitFaceStickerIndex(1,1);
157
158
    corners[2][0] = object.getCubitFaceStickerIndex(2,5);
159
    corners[2][1] = object.getCubitFaceStickerIndex(2,1);
160
    corners[2][2] = object.getCubitFaceStickerIndex(2,3);
161
162
    corners[3][0] = object.getCubitFaceStickerIndex(3,1);
163
    corners[3][1] = object.getCubitFaceStickerIndex(3,3);
164
    corners[3][2] = object.getCubitFaceStickerIndex(3,5);
165
166
    corners[4][0] = object.getCubitFaceStickerIndex(4,1);
167
    corners[4][1] = object.getCubitFaceStickerIndex(4,3);
168
    corners[4][2] = object.getCubitFaceStickerIndex(4,5);
169
170
    corners[5][0] = object.getCubitFaceStickerIndex(5,1);
171
    corners[5][1] = object.getCubitFaceStickerIndex(5,3);
172
    corners[5][2] = object.getCubitFaceStickerIndex(5,5);
173
174
    corners[6][0] = object.getCubitFaceStickerIndex(6,1);
175
    corners[6][1] = object.getCubitFaceStickerIndex(6,3);
176
    corners[6][2] = object.getCubitFaceStickerIndex(6,5);
177
178
    corners[7][0] = object.getCubitFaceStickerIndex(7,1);
179
    corners[7][1] = object.getCubitFaceStickerIndex(7,3);
180
    corners[7][2] = object.getCubitFaceStickerIndex(7,5);
181 cff92952 Leszek Koltunski
    }
182
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184
185
  public int tablebaseIndex(TwistyObject object)
186
    {
187
    int[][] corners= new int[8][3];
188
    getCorners(object,corners);
189 6f1dbce1 Leszek Koltunski
190 a4569df4 Leszek Koltunski
    int result0 = computeFaceColors(corners);
191
    if( result0<0 ) return result0;
192
193 5312255f Leszek Koltunski
    int[] perm = new int[8];
194
    int result1 = retCornerPermutation(perm,corners);
195 a4569df4 Leszek Koltunski
    if( result1<0 ) return result1;
196
197 f5e5e7a3 Leszek Koltunski
    int[] perm7 = TBCube2.shrinkPerm(perm);
198 5312255f Leszek Koltunski
    int permNum = TablebaseHelpers.computePermutationNum(perm7);
199 a4569df4 Leszek Koltunski
    int[] twist = new int[8];
200 5312255f Leszek Koltunski
    fillCornerTwists(twist,corners,perm);
201 a4569df4 Leszek Koltunski
202
    int totalTwist = 0;
203
    for(int i=0; i<8; i++) totalTwist += twist[i];
204
    if( ((totalTwist)%3) != 0 ) return ERROR_CORNER_TWISTED;
205
206
    int twistNum = twist[0] + 3*(twist[2] + 3*(twist[3] + 3*(twist[4] + 3*(twist[5] + 3*twist[6]))));
207
208 5312255f Leszek Koltunski
    return twistNum + 729*permNum;
209 a4569df4 Leszek Koltunski
    }
210
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212
213
  private int getColorIndex4(int face)
214
    {
215
    switch(mFaceColors[face])
216
      {
217
      case 0: return R.string.color_yellow4;
218
      case 1: return R.string.color_white4;
219 6f1dbce1 Leszek Koltunski
      case 2: return R.string.color_blue4;
220
      case 3: return R.string.color_green4;
221 a4569df4 Leszek Koltunski
      case 4: return R.string.color_red4;
222
      case 5: return R.string.color_orange4;
223
      }
224
225
    return -1;
226
    }
227
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229
230
  private int getColorIndex3(int face)
231
    {
232
    switch(mFaceColors[face])
233
      {
234
      case 0: return R.string.color_yellow3;
235
      case 1: return R.string.color_white3;
236 6f1dbce1 Leszek Koltunski
      case 2: return R.string.color_blue3;
237
      case 3: return R.string.color_green3;
238 a4569df4 Leszek Koltunski
      case 4: return R.string.color_red3;
239
      case 5: return R.string.color_orange3;
240
      }
241
242
    return -1;
243
    }
244
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246
247
  private String cornerError(Resources res, int face0, int face1, int face2)
248
    {
249
    int j0 = getColorIndex3(face0);
250
    int j1 = getColorIndex3(face1);
251
    int j2 = getColorIndex4(face2);
252
253
    String c0 = res.getString(j0);
254
    String c1 = res.getString(j1);
255
    String c2 = res.getString(j2);
256
257
    return res.getString(R.string.solver_generic_missing_corner,c0,c1,c2);
258
    }
259
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261
262
  public String error(int index, Resources res)
263
    {
264
    switch(index)
265
      {
266
      case ERROR_CORNER_135_MISSING: return cornerError(res,1,3,5);
267
      case ERROR_CORNER_134_MISSING: return cornerError(res,1,3,4);
268
      case ERROR_CORNER_125_MISSING: return cornerError(res,1,2,5);
269
      case ERROR_CORNER_124_MISSING: return cornerError(res,1,2,4);
270
      case ERROR_CORNER_035_MISSING: return cornerError(res,0,3,5);
271
      case ERROR_CORNER_034_MISSING: return cornerError(res,0,3,4);
272
      case ERROR_CORNER_025_MISSING: return cornerError(res,0,2,5);
273
      case ERROR_CORNER_024_MISSING: return cornerError(res,0,2,4);
274
      case ERROR_CORNERS_CANNOT    : return res.getString(R.string.solver_generic_corners_cannot);
275
      case ERROR_CORNER_TWISTED    : return res.getString(R.string.solver_generic_corner_twist);
276
      }
277
278
    return null;
279
    }
280
}