Project

General

Profile

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

magiccube / src / main / java / org / distorted / solvers / SolverPyraminxDiamond.java @ 04154c0f

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 SolverPyraminxDiamond extends SolverTablebase
24
{
25
  private static final int ERROR_CORNER_FR_MISSING = -1;
26
  private static final int ERROR_CORNER_BR_MISSING = -2;
27
  private static final int ERROR_CORNER_BL_MISSING = -3;
28
  private static final int ERROR_CORNER_FL_MISSING = -4;
29
  private static final int ERROR_CORNER_TO_MISSING = -5;
30
  private static final int ERROR_CORNER_BO_MISSING = -6;
31

    
32
  private static final int ERROR_CENTER_0_MISSING = -7;
33
  private static final int ERROR_CENTER_1_MISSING = -8;
34
  private static final int ERROR_CENTER_2_MISSING = -9;
35
  private static final int ERROR_CENTER_3_MISSING = -10;
36
  private static final int ERROR_CENTER_4_MISSING = -11;
37
  private static final int ERROR_CENTER_5_MISSING = -12;
38
  private static final int ERROR_CENTER_6_MISSING = -13;
39
  private static final int ERROR_CENTER_7_MISSING = -14;
40

    
41
  private static final int ERROR_TWO_CENTERS      = -15;
42
  private static final int ERROR_CORNERS_CANNOT   = -16;
43

    
44
  private TablebasesAbstract mSolver;
45
  private final int[] mFaceColors;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

    
49
  public SolverPyraminxDiamond(Resources res, TwistyObject object)
50
    {
51
    super(res,object);
52
    mFaceColors = new int[8];
53
    }
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
  private int retCenter(int color, int[] centers)
58
    {
59
    for(int i=0; i<8; i++ )
60
      if( centers[i]==color ) return i;
61

    
62
    return -1;
63
    }
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

    
67
  private int[] getCentersPermutation(int[] centers)
68
    {
69
    int[] perm = new int[8];
70

    
71
    for(int i=0; i<8; i++ )
72
      perm[i] = retCenter(mFaceColors[i],centers);
73

    
74
    return perm;
75
    }
76

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

    
79
  private boolean isTwistEven(int[] twist)
80
    {
81
    int total = twist[0]+twist[1]+twist[2]+twist[3]+twist[4]+twist[5];
82
    return ((total%2)==0);
83
    }
84

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

    
87
  private int indexOf(int[] corner, int color )
88
    {
89
    if( corner[0]==color ) return 0;
90
    if( corner[1]==color ) return 1;
91
    if( corner[2]==color ) return 2;
92
    if( corner[3]==color ) return 3;
93

    
94
    return -1;
95
    }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

    
99
  private int[] getCornersTwist(int[][] corners)
100
    {
101
    int[] twist = new int[6];
102

    
103
    twist[0] = indexOf(corners[0],mFaceColors[0]);
104
    twist[1] = indexOf(corners[1],mFaceColors[5]);
105
    twist[2] = indexOf(corners[2],mFaceColors[2]);
106
    twist[3] = indexOf(corners[3],mFaceColors[4]);
107
    twist[4] = indexOf(corners[4],mFaceColors[2]);
108
    twist[5] = indexOf(corners[5],mFaceColors[6]);
109

    
110

    
111
    return twist;
112
    }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

    
116
  private int checkAllColorsDifferent()
117
    {
118
    for(int i=0; i<8; i++)
119
      {
120
      boolean present = false;
121

    
122
      for(int j=0; j<8; j++)
123
        if( mFaceColors[j]==i )
124
          {
125
          present=true;
126
          break;
127
          }
128

    
129
      if( !present ) return ERROR_CORNERS_CANNOT;
130
      }
131

    
132
    return 0;
133
    }
134

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

    
137
  private int checkAllCentersPresent(int[] centers)
138
    {
139
    for(int i=0; i<8; i++)
140
      {
141
      boolean present = false;
142

    
143
      for(int j=0; j<8; j++)
144
        if( centers[j]==i )
145
          {
146
          present=true;
147
          break;
148
          }
149

    
150
      if( !present )
151
        {
152
        switch(i)
153
          {
154
          case 0: return ERROR_CENTER_0_MISSING;
155
          case 1: return ERROR_CENTER_1_MISSING;
156
          case 2: return ERROR_CENTER_2_MISSING;
157
          case 3: return ERROR_CENTER_3_MISSING;
158
          case 4: return ERROR_CENTER_4_MISSING;
159
          case 5: return ERROR_CENTER_5_MISSING;
160
          case 6: return ERROR_CENTER_6_MISSING;
161
          case 7: return ERROR_CENTER_7_MISSING;
162
          }
163
        }
164
      }
165

    
166
    return 0;
167
    }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

    
171
  private int commonColor(int[][] corners, int index1, int index2, int index3)
172
    {
173
    int[] c1 = corners[index1];
174
    int[] c2 = corners[index2];
175
    int[] c3 = corners[index3];
176

    
177
    for(int i=0; i<4; i++)
178
      {
179
      int c = c1[i];
180

    
181
      if( (c2[0]==c || c2[1]==c || c2[2]==c || c2[3]==c) &&
182
          (c3[0]==c || c3[1]==c || c3[2]==c || c3[3]==c)  ) return c;
183
      }
184

    
185
    return ERROR_CORNERS_CANNOT;
186
    }
187

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189

    
190
  private int figureOutFaceColors(int[] output, int[][] corners)
191
    {
192
    int[][] commonCorners = {{0,2,4},{0,3,5},{1,2,4},{1,3,5},{0,2,3},{1,2,3},{0,4,5},{1,4,5}};
193

    
194
    for(int i=0; i<8; i++)
195
      {
196
      int[] common = commonCorners[i];
197
      output[i] = commonColor(corners,common[0],common[1],common[2]);
198
      if( output[i]<0 ) return output[i];
199
      }
200

    
201
    return 0;
202
    }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

    
206
  private void getCorners(TwistyObject object, int[][] corners)
207
    {
208
    for(int i=0; i<6; i++)
209
      for(int j=0; j<4; j++)
210
         corners[i][j] = object.getCubitFaceStickerIndex(i,j);
211
    }
212

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

    
215
  private void getCenters(TwistyObject object, int[] centers)
216
    {
217
    for(int i=0; i<8; i++)
218
       centers[i] = object.getCubitFaceStickerIndex(6+i,0)-8;
219
    }
220

    
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222

    
223
  public int tablebaseIndex(TwistyObject object)
224
    {
225
    int[][] corners = new int[6][4];
226
    int[] centers = new int[8];
227

    
228
    getCorners(object,corners);
229
    getCenters(object,centers);
230

    
231
    int result1 = figureOutFaceColors(mFaceColors,corners);
232
    if( result1<0 ) return result1;
233

    
234
    int result2 = checkAllCentersPresent(centers);
235
    if( result2<0 ) return result2;
236

    
237
    int result3 = checkAllColorsDifferent();
238
    if( result3<0 ) return result3;
239

    
240
    int[] twist = getCornersTwist(corners);
241
    boolean even1 = isTwistEven(twist);
242

    
243
    int[] centers_perm = getCentersPermutation(centers);
244
    boolean even2 = TablebaseHelpers.permutationIsEven(centers_perm);
245
    if( even1^even2 ) return ERROR_TWO_CENTERS;
246

    
247
    int centers_perm_num = TablebaseHelpers.computePermutationNum(centers_perm);
248
    int total_twist = twist[0]+ 4*(twist[1]+ 4*(twist[2]+ 4*(twist[3]+ 4*(twist[4]+ 4*(twist[5]>1 ? 0:1)))));
249

    
250
/*
251
android.util.Log.e("D", "faces: "+mFaceColors[0]+" "+mFaceColors[1]+" "+mFaceColors[2]+" "
252
+mFaceColors[3]+" "+mFaceColors[4]+" "+mFaceColors[5]+" "+mFaceColors[6]+" "+mFaceColors[7]);
253
android.util.Log.e("D", "corn twist: "+twist[0]+" "+twist[1]+" "+twist[2]+" "+twist[3]+" "+twist[4]+" "+twist[5]);
254
android.util.Log.e("D", "ret="+(total_twist + 2048*centers_perm_num) );
255
*/
256
    return total_twist + 2048*centers_perm_num;
257
    }
258

    
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260

    
261
  private int getColorIndex4(int face)
262
    {
263
    switch(mFaceColors[face])
264
      {
265
      case 0: return R.string.color_violet4;
266
      case 1: return R.string.color_grey4;
267
      case 2: return R.string.color_blue4;
268
      case 3: return R.string.color_red4;
269
      case 4: return R.string.color_orange4;
270
      case 5: return R.string.color_green4;
271
      case 6: return R.string.color_white4;
272
      case 7: return R.string.color_yellow4;
273
      }
274

    
275
    return -1;
276
    }
277

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

    
280
  private int getColorIndex3(int face)
281
    {
282
    switch(mFaceColors[face])
283
      {
284
      case 0: return R.string.color_violet3;
285
      case 1: return R.string.color_grey3;
286
      case 2: return R.string.color_blue3;
287
      case 3: return R.string.color_red3;
288
      case 4: return R.string.color_orange3;
289
      case 5: return R.string.color_green3;
290
      case 6: return R.string.color_white3;
291
      case 7: return R.string.color_yellow3;
292
      }
293

    
294
    return -1;
295
    }
296

    
297
///////////////////////////////////////////////////////////////////////////////////////////////////
298

    
299
  private int getColorIndex2(int face)
300
    {
301
    switch(face)
302
      {
303
      case 0: return R.string.color_violet2;
304
      case 1: return R.string.color_grey2;
305
      case 2: return R.string.color_blue2;
306
      case 3: return R.string.color_red2;
307
      case 4: return R.string.color_orange2;
308
      case 5: return R.string.color_green2;
309
      case 6: return R.string.color_white2;
310
      case 7: return R.string.color_yellow2;
311
      }
312

    
313
    return -1;
314
    }
315

    
316
///////////////////////////////////////////////////////////////////////////////////////////////////
317

    
318
  private String centerError(Resources res, int face)
319
    {
320
    String color = res.getString(getColorIndex2(face));
321
    return res.getString(R.string.solver_generic_missing_center,color);
322
    }
323

    
324
///////////////////////////////////////////////////////////////////////////////////////////////////
325

    
326
  private String cornerError(Resources res, int f1, int f2)
327
    {
328
    String c1 = res.getString(getColorIndex3(f1));
329
    String c2 = res.getString(getColorIndex4(f2));
330
    return res.getString(R.string.solver_generic_missing_corner2,c1,c2);
331
    }
332

    
333
///////////////////////////////////////////////////////////////////////////////////////////////////
334

    
335
  public String error(int index, Resources res)
336
    {
337
    switch(index)
338
      {
339
      case ERROR_CORNER_FR_MISSING: return cornerError(res,1,4);
340
      case ERROR_CORNER_BR_MISSING: return cornerError(res,1,6);
341
      case ERROR_CORNER_BL_MISSING: return cornerError(res,3,6);
342
      case ERROR_CORNER_FL_MISSING: return cornerError(res,3,4);
343
      case ERROR_CORNER_TO_MISSING: return cornerError(res,1,3);
344
      case ERROR_CORNER_BO_MISSING: return cornerError(res,4,6);
345

    
346
      case ERROR_CENTER_0_MISSING : return centerError(res,0);
347
      case ERROR_CENTER_1_MISSING : return centerError(res,1);
348
      case ERROR_CENTER_2_MISSING : return centerError(res,2);
349
      case ERROR_CENTER_3_MISSING : return centerError(res,3);
350
      case ERROR_CENTER_4_MISSING : return centerError(res,4);
351
      case ERROR_CENTER_5_MISSING : return centerError(res,5);
352
      case ERROR_CENTER_6_MISSING : return centerError(res,6);
353
      case ERROR_CENTER_7_MISSING : return centerError(res,7);
354

    
355
      case ERROR_TWO_CENTERS      : return res.getString(R.string.solver_generic_two_centers);
356
      case ERROR_CORNERS_CANNOT   : return res.getString(R.string.solver_generic_corners_cannot);
357
      }
358

    
359
    return null;
360
    }
361

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

    
364
  public int[][] solution(int index, Resources res)
365
    {
366
    if( mSolver==null )
367
      {
368
      mSolver = ImplementedTablebasesList.createUnpacked(ObjectSignatures.PDIA_3);
369

    
370
      if( mSolver!=null )
371
        {
372
        mSolver.createTablebase(2);
373
        }
374
      }
375

    
376
    return mSolver!=null ? mSolver.solution(index,null) : null;
377
    }
378
}  
379

    
(11-11/15)