Project

General

Profile

« Previous | Next » 

Revision a4569df4

Added by Leszek Koltunski almost 2 years ago

Beginnings of the 2x2 solver.

View differences:

src/main/java/org/distorted/solvers/SolverCube2.java
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.main.ObjectSignatures;
16
import org.distorted.objectlib.main.TwistyObject;
17
import org.distorted.objectlib.tablebases.ImplementedTablebasesList;
18
import org.distorted.objectlib.tablebases.TablebaseHelpers;
19
import org.distorted.objectlib.tablebases.TablebasesAbstract;
20

  
21
///////////////////////////////////////////////////////////////////////////////////////////////////
22

  
23
public class SolverCube2 extends SolverTablebase
24
{
25
  private static final int ERROR_CORNER_135_MISSING = -1;
26
  private static final int ERROR_CORNER_134_MISSING = -2;
27
  private static final int ERROR_CORNER_125_MISSING = -3;
28
  private static final int ERROR_CORNER_124_MISSING = -4;
29
  private static final int ERROR_CORNER_035_MISSING = -5;
30
  private static final int ERROR_CORNER_034_MISSING = -6;
31
  private static final int ERROR_CORNER_025_MISSING = -7;
32
  private static final int ERROR_CORNER_024_MISSING = -8;
33
  private static final int ERROR_CORNERS_CANNOT     = -9;
34
  private static final int ERROR_CORNER_TWISTED     = -10;
35

  
36
  TablebasesAbstract mSolver;
37
  private final int[] mFaceColors;
38

  
39
////////////////////////////////////////////////////////////////////////////////////////
40

  
41
  private void fillCornerTwists(int[] output, int[][] corners, int[] perm)
42
    {
43
    // TODO
44
    }
45

  
46
////////////////////////////////////////////////////////////////////////////////////////
47

  
48
  private int cornerIndex(int[][] corners, int i1, int i2, int i3)
49
    {
50
    int c1 = mFaceColors[i1];
51
    int c2 = mFaceColors[i2];
52
    int c3 = mFaceColors[i3];
53

  
54
    for(int i=0; i<8; i++)
55
      {
56
      int[] cor = corners[i];
57

  
58
      if( (cor[0]==c1 && cor[1]==c2 && cor[2]==c3) ||
59
          (cor[0]==c2 && cor[1]==c3 && cor[2]==c1) ||
60
          (cor[0]==c3 && cor[1]==c1 && cor[2]==c2)  ) return i;
61
      }
62

  
63
    return -1;
64
    }
65

  
66
////////////////////////////////////////////////////////////////////////////////////////
67

  
68
  private int retCornerPermutation(int[] output, int[][] corners)
69
    {
70
    for(int i=0; i<8; i++) output[i] = -1;
71

  
72
    output[0] = cornerIndex(corners, 1,2,5);
73
    output[1] = cornerIndex(corners, 1,4,2);
74
    output[2] = cornerIndex(corners, 1,5,3);
75
    output[3] = cornerIndex(corners, 1,3,4);
76
    output[4] = cornerIndex(corners, 0,5,2);
77
    output[5] = cornerIndex(corners, 0,2,4);
78
    output[6] = cornerIndex(corners, 0,3,5);
79
    output[7] = cornerIndex(corners, 0,4,3);
80

  
81
    if( output[0]==-1 ) { android.util.Log.e("D ", "135"); return ERROR_CORNER_135_MISSING; }
82
    if( output[1]==-1 ) { android.util.Log.e("D ", "134"); return ERROR_CORNER_134_MISSING; }
83
    if( output[2]==-1 ) { android.util.Log.e("D ", "125"); return ERROR_CORNER_125_MISSING; }
84
    if( output[3]==-1 ) { android.util.Log.e("D ", "124"); return ERROR_CORNER_124_MISSING; }
85
    if( output[4]==-1 ) { android.util.Log.e("D ", "035"); return ERROR_CORNER_035_MISSING; }
86
    if( output[5]==-1 ) { android.util.Log.e("D ", "034"); return ERROR_CORNER_034_MISSING; }
87
    if( output[6]==-1 ) { android.util.Log.e("D ", "025"); return ERROR_CORNER_025_MISSING; }
88
    if( output[7]==-1 ) { android.util.Log.e("D ", "024"); return ERROR_CORNER_024_MISSING; }
89

  
90
    return 0;
91
    }
92

  
93
////////////////////////////////////////////////////////////////////////////////////////
94

  
95
  private int findColor(int[][] corners, int c1, int c2)
96
    {
97
    for(int i=0; i<8; i++)
98
      {
99
      int[] cor = corners[i];
100

  
101
      if( cor[0]==c1 && cor[1]==c2 ) return cor[2];
102
      if( cor[1]==c1 && cor[2]==c2 ) return cor[0];
103
      if( cor[2]==c1 && cor[0]==c2 ) return cor[1];
104
      }
105

  
106
    return -1;
107
    }
108

  
109
////////////////////////////////////////////////////////////////////////////////////////
110

  
111
  private int computeFaceColors(int[][] corners)
112
    {
113
    mFaceColors[1] = corners[1][0];
114
    mFaceColors[2] = corners[1][2];
115
    mFaceColors[4] = corners[1][1];
116

  
117
    mFaceColors[0] = findColor(corners,mFaceColors[2],mFaceColors[4]);
118
    mFaceColors[3] = findColor(corners,mFaceColors[4],mFaceColors[1]);
119
    mFaceColors[5] = findColor(corners,mFaceColors[1],mFaceColors[2]);
120

  
121
    for(int i=0; i<6; i++) android.util.Log.e("D", "face "+i+" : "+mFaceColors[i]);
122

  
123
    for(int i=0; i<6; i++)
124
      {
125
      int color = mFaceColors[i];
126
      if( color<0 ) return ERROR_CORNERS_CANNOT;
127

  
128
      for(int j=i+1; j<6; j++)
129
        if( mFaceColors[j]==color ) return ERROR_CORNERS_CANNOT;
130
      }
131

  
132
    return 0;
133
    }
134

  
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

  
137
  public SolverCube2(Resources res, TwistyObject object)
138
    {
139
    super(res,object);
140
    mFaceColors = new int[6];
141
    }
142

  
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

  
145
  public int tablebaseIndex(TwistyObject object)
146
    {
147
    int[][] corners= new int[8][3];
148

  
149
    corners[0][0] = object.getCubitFaceStickerIndex(0,1);
150
    corners[0][1] = object.getCubitFaceStickerIndex(0,3);
151
    corners[0][2] = object.getCubitFaceStickerIndex(0,5);
152

  
153
    corners[1][0] = object.getCubitFaceStickerIndex(1,3);
154
    corners[1][1] = object.getCubitFaceStickerIndex(1,5);
155
    corners[1][2] = object.getCubitFaceStickerIndex(1,1);
156

  
157
    corners[2][0] = object.getCubitFaceStickerIndex(2,5);
158
    corners[2][1] = object.getCubitFaceStickerIndex(2,1);
159
    corners[2][2] = object.getCubitFaceStickerIndex(2,3);
160

  
161
    corners[3][0] = object.getCubitFaceStickerIndex(3,1);
162
    corners[3][1] = object.getCubitFaceStickerIndex(3,3);
163
    corners[3][2] = object.getCubitFaceStickerIndex(3,5);
164

  
165
    corners[4][0] = object.getCubitFaceStickerIndex(4,1);
166
    corners[4][1] = object.getCubitFaceStickerIndex(4,3);
167
    corners[4][2] = object.getCubitFaceStickerIndex(4,5);
168

  
169
    corners[5][0] = object.getCubitFaceStickerIndex(5,1);
170
    corners[5][1] = object.getCubitFaceStickerIndex(5,3);
171
    corners[5][2] = object.getCubitFaceStickerIndex(5,5);
172

  
173
    corners[6][0] = object.getCubitFaceStickerIndex(6,1);
174
    corners[6][1] = object.getCubitFaceStickerIndex(6,3);
175
    corners[6][2] = object.getCubitFaceStickerIndex(6,5);
176

  
177
    corners[7][0] = object.getCubitFaceStickerIndex(7,1);
178
    corners[7][1] = object.getCubitFaceStickerIndex(7,3);
179
    corners[7][2] = object.getCubitFaceStickerIndex(7,5);
180
/*
181
    for(int i=0; i<8; i++)
182
      for(int f=0; f<3; f++)
183
        {
184
        android.util.Log.e("D", "cubit "+i+" face "+f+" : "+corners[i][f]);
185
        }
186
*/
187
    int result0 = computeFaceColors(corners);
188
    if( result0<0 ) return result0;
189

  
190
    int[] corner_perm = new int[8];
191
    int result1 = retCornerPermutation(corner_perm,corners);
192
    if( result1<0 ) return result1;
193

  
194
    int[] perm = new int[7];
195

  
196
    perm[0] = corner_perm[0]>1 ? corner_perm[0]-1 : corner_perm[0];
197
    perm[1] = corner_perm[2]>1 ? corner_perm[2]-1 : corner_perm[2];
198
    perm[2] = corner_perm[3]>1 ? corner_perm[3]-1 : corner_perm[3];
199
    perm[3] = corner_perm[4]>1 ? corner_perm[4]-1 : corner_perm[4];
200
    perm[4] = corner_perm[5]>1 ? corner_perm[5]-1 : corner_perm[5];
201
    perm[5] = corner_perm[6]>1 ? corner_perm[6]-1 : corner_perm[6];
202
    perm[6] = corner_perm[7]>1 ? corner_perm[7]-1 : corner_perm[7];
203

  
204
    int perm_num = TablebaseHelpers.computePermutationNum(perm);
205
/*
206
    android.util.Log.e("D", "corner perm num="+perm_num);
207

  
208
    for(int i=0; i<8; i++)
209
      {
210
      android.util.Log.e("D", "8perm "+i+" "+corner_perm[i]);
211
      }
212
    for(int i=0; i<7; i++)
213
      {
214
      android.util.Log.e("D", "7perm "+i+" "+perm[i]);
215
      }
216
*/
217
    int[] twist = new int[8];
218
    fillCornerTwists(twist,corners,corner_perm);
219

  
220
    int totalTwist = 0;
221
    for(int i=0; i<8; i++) totalTwist += twist[i];
222
    if( ((totalTwist)%3) != 0 ) return ERROR_CORNER_TWISTED;
223

  
224
    int twistNum = twist[0] + 3*(twist[2] + 3*(twist[3] + 3*(twist[4] + 3*(twist[5] + 3*twist[6]))));
225

  
226
    return twistNum + 729*perm_num;
227
    }
228

  
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

  
231
  private int getColorIndex4(int face)
232
    {
233
    switch(mFaceColors[face])
234
      {
235
      case 0: return R.string.color_yellow4;
236
      case 1: return R.string.color_white4;
237
      case 2: return R.string.color_green4;
238
      case 3: return R.string.color_blue4;
239
      case 4: return R.string.color_red4;
240
      case 5: return R.string.color_orange4;
241
      }
242

  
243
    return -1;
244
    }
245

  
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

  
248
  private int getColorIndex3(int face)
249
    {
250
    switch(mFaceColors[face])
251
      {
252
      case 0: return R.string.color_yellow3;
253
      case 1: return R.string.color_white3;
254
      case 2: return R.string.color_green3;
255
      case 3: return R.string.color_blue3;
256
      case 4: return R.string.color_red3;
257
      case 5: return R.string.color_orange3;
258
      }
259

  
260
    return -1;
261
    }
262

  
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

  
265
  private String cornerError(Resources res, int face0, int face1, int face2)
266
    {
267
    int j0 = getColorIndex3(face0);
268
    int j1 = getColorIndex3(face1);
269
    int j2 = getColorIndex4(face2);
270

  
271
    String c0 = res.getString(j0);
272
    String c1 = res.getString(j1);
273
    String c2 = res.getString(j2);
274

  
275
    return res.getString(R.string.solver_generic_missing_corner,c0,c1,c2);
276
    }
277

  
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279

  
280
  public String error(int index, Resources res)
281
    {
282
    switch(index)
283
      {
284
      case ERROR_CORNER_135_MISSING: return cornerError(res,1,3,5);
285
      case ERROR_CORNER_134_MISSING: return cornerError(res,1,3,4);
286
      case ERROR_CORNER_125_MISSING: return cornerError(res,1,2,5);
287
      case ERROR_CORNER_124_MISSING: return cornerError(res,1,2,4);
288
      case ERROR_CORNER_035_MISSING: return cornerError(res,0,3,5);
289
      case ERROR_CORNER_034_MISSING: return cornerError(res,0,3,4);
290
      case ERROR_CORNER_025_MISSING: return cornerError(res,0,2,5);
291
      case ERROR_CORNER_024_MISSING: return cornerError(res,0,2,4);
292
      case ERROR_CORNERS_CANNOT    : return res.getString(R.string.solver_generic_corners_cannot);
293
      case ERROR_CORNER_TWISTED    : return res.getString(R.string.solver_generic_corner_twist);
294
      }
295

  
296
    return null;
297
    }
298

  
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300

  
301
  public int[][] solution(int index, Resources res)
302
    {
303
    if( mSolver==null )
304
      {
305
      mSolver = ImplementedTablebasesList.createUnpacked(ObjectSignatures.CUBE_2);
306
      if( mSolver!=null ) mSolver.createTablebase();
307
      }
308

  
309
    return mSolver!=null ? mSolver.solution(index,null) : null;
310
    }
311
}  
312

  
src/main/java/org/distorted/solvers/SolverCuboid232.java
291 291

  
292 292
    int corner_perm_num = TablebaseHelpers.computePermutationNum(corner_perm);
293 293
    int edge_perm_num = TablebaseHelpers.computePermutationNum(edge_perm2);
294
/*
295
    android.util.Log.e("D", "corner perm num="+corner_perm_num);
296
    for(int i=0; i<8; i++)
297
      {
298
      android.util.Log.e("D", "corner perm "+i+" "+corner_perm[i]);
299
      }
300 294

  
301
    android.util.Log.e("D", "edge perm num="+edge_perm_num);
302
    for(int i=0; i<4; i++)
303
      {
304
      android.util.Log.e("D", "edge perm "+i+" "+edge_perm[i]);
305
      }
306
    for(int i=0; i<3; i++)
307
      {
308
      android.util.Log.e("D", "edge perm2 "+i+" "+edge_perm2[i]);
309
      }
310
*/
311 295
    return edge_perm_num + 6*corner_perm_num;
312 296
    }
313 297

  
......
319 303
      {
320 304
      case 0: return R.string.color_yellow4;
321 305
      case 1: return R.string.color_white4;
322
      case 2: return R.string.color_blue4;
323
      case 3: return R.string.color_green4;
306
      case 2: return R.string.color_green4;
307
      case 3: return R.string.color_blue4;
324 308
      case 4: return R.string.color_red4;
325 309
      case 5: return R.string.color_orange4;
326 310
      }
......
336 320
      {
337 321
      case 0: return R.string.color_yellow3;
338 322
      case 1: return R.string.color_white3;
339
      case 2: return R.string.color_blue3;
340
      case 3: return R.string.color_green3;
323
      case 2: return R.string.color_green3;
324
      case 3: return R.string.color_blue3;
341 325
      case 4: return R.string.color_red3;
342 326
      case 5: return R.string.color_orange3;
343 327
      }
......
353 337
      {
354 338
      case 0: return R.string.color_yellow6;
355 339
      case 1: return R.string.color_white6;
356
      case 2: return R.string.color_blue6;
357
      case 3: return R.string.color_green6;
340
      case 2: return R.string.color_green6;
341
      case 3: return R.string.color_blue6;
358 342
      case 4: return R.string.color_red6;
359 343
      case 5: return R.string.color_orange6;
360 344
      }
src/main/java/org/distorted/solvers/SolverMain.java
101 101
      SolverTablebase solver = new SolverSkewbDiamond(mRes,mObject);
102 102
      solver.solve(screen);
103 103
      }
104
    else if( mSignature==ObjectSignatures.CUBE_2 )
105
      {
106
      SolverTablebase solver = new SolverCube2(mRes,mObject);
107
      solver.solve(screen);
108
      }
104 109
    else
105 110
      {
106 111
      screen.displayErrorDialog(mRes.getString(R.string.solver_generic_not_implemented));

Also available in: Unified diff