Project

General

Profile

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

magiccube / src / main / java / org / distorted / solvers / SolverPyraminxDiamond.java @ master

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.metadata.ListObjects;
17
import org.distorted.objectlib.main.TwistyObject;
18
import org.distorted.objectlib.tablebases.ImplementedTablebasesList;
19
import org.distorted.objectlib.tablebases.TablebaseHelpers;
20
import org.distorted.objectlib.tablebases.TablebasesAbstract;
21

    
22
///////////////////////////////////////////////////////////////////////////////////////////////////
23

    
24
public class SolverPyraminxDiamond extends SolverTablebase
25
{
26
  private static final int ERROR_CORNER_FR_MISSING = -1;
27
  private static final int ERROR_CORNER_BR_MISSING = -2;
28
  private static final int ERROR_CORNER_BL_MISSING = -3;
29
  private static final int ERROR_CORNER_FL_MISSING = -4;
30
  private static final int ERROR_CORNER_TO_MISSING = -5;
31
  private static final int ERROR_CORNER_BO_MISSING = -6;
32

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

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

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

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

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

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

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

    
63
    return -1;
64
    }
65

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

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

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

    
75
    return perm;
76
    }
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79

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

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

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

    
95
    return -1;
96
    }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

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

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

    
111

    
112
    return twist;
113
    }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116

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

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

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

    
133
    return 0;
134
    }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137

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

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

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

    
167
    return 0;
168
    }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

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

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

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

    
186
    return ERROR_CORNERS_CANNOT;
187
    }
188

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

    
191
  private int figureOutFaceColors(int[] output, int[][] corners)
192
    {
193
    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}};
194

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

    
202
    return 0;
203
    }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

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

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

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

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

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

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

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

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

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

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

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

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

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

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261

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

    
276
    return -1;
277
    }
278

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

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

    
295
    return -1;
296
    }
297

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

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

    
314
    return -1;
315
    }
316

    
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318

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

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

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

    
334
///////////////////////////////////////////////////////////////////////////////////////////////////
335

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

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

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

    
360
    return null;
361
    }
362

    
363
///////////////////////////////////////////////////////////////////////////////////////////////////
364

    
365
  public int[][] solution(int index, OperatingSystemInterface os)
366
    {
367
    if( mSolver==null )
368
      {
369
      mSolver = ImplementedTablebasesList.createPacked(os, ListObjects.PDIA_3.name() );
370
      }
371

    
372
    return mSolver!=null ? mSolver.solution(index,null,os) : null;
373
    }
374
}  
375

    
(12-12/16)