Project

General

Profile

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

magiccube / src / main / java / org / distorted / solvers / SolverTablebasePDIA3.java @ fc08e3ca

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.TablebaseHelpers;
18

    
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

    
21
public class SolverTablebasePDIA3 extends SolverTablebase
22
{
23
  private static final int ERROR_CENTER_MISSING = -1;
24
  private static final int ERROR_TWO_CENTERS    = -2;
25
  private static final int ERROR_CORNERS_CANNOT = -3;
26

    
27
  private final int[] mFaceColors;
28
  private int mErrColor;
29

    
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31

    
32
  public SolverTablebasePDIA3(OperatingSystemInterface os, Resources res, TwistyObject object)
33
    {
34
    super(os,res,object);
35
    mFaceColors = new int[8];
36
    }
37

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

    
40
  private int retCenter(int color, int[] centers)
41
    {
42
    for(int i=0; i<8; i++ )
43
      if( centers[i]==color ) return i;
44

    
45
    return -1;
46
    }
47

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

    
50
  private int[] getCentersPermutation(int[] centers)
51
    {
52
    int[] perm = new int[8];
53

    
54
    for(int i=0; i<8; i++ )
55
      perm[i] = retCenter(mFaceColors[i],centers);
56

    
57
    return perm;
58
    }
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
  private boolean isTwistEven(int[] twist)
63
    {
64
    int total = twist[0]+twist[1]+twist[2]+twist[3]+twist[4]+twist[5];
65
    return ((total%2)==0);
66
    }
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

    
70
  private int indexOf(int[] corner, int color )
71
    {
72
    if( corner[0]==color ) return 0;
73
    if( corner[1]==color ) return 1;
74
    if( corner[2]==color ) return 2;
75
    if( corner[3]==color ) return 3;
76

    
77
    return -1;
78
    }
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

    
82
  private int[] getCornersTwist(int[][] corners)
83
    {
84
    int[] twist = new int[6];
85

    
86
    twist[0] = indexOf(corners[0],mFaceColors[0]);
87
    twist[1] = indexOf(corners[1],mFaceColors[5]);
88
    twist[2] = indexOf(corners[2],mFaceColors[2]);
89
    twist[3] = indexOf(corners[3],mFaceColors[4]);
90
    twist[4] = indexOf(corners[4],mFaceColors[2]);
91
    twist[5] = indexOf(corners[5],mFaceColors[6]);
92

    
93

    
94
    return twist;
95
    }
96

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

    
99
  private int checkAllColorsDifferent()
100
    {
101
    for(int i=0; i<8; i++)
102
      {
103
      boolean present = false;
104

    
105
      for(int j=0; j<8; j++)
106
        if( mFaceColors[j]==i )
107
          {
108
          present=true;
109
          break;
110
          }
111

    
112
      if( !present ) return ERROR_CORNERS_CANNOT;
113
      }
114

    
115
    return 0;
116
    }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

    
120
  private int checkAllCentersPresent(int[] centers)
121
    {
122
    for(int i=0; i<8; i++)
123
      {
124
      boolean present = false;
125

    
126
      for(int j=0; j<8; j++)
127
        if( centers[j]==i )
128
          {
129
          present=true;
130
          break;
131
          }
132

    
133
      if( !present ) { mErrColor=i; return ERROR_CENTER_MISSING; }
134
      }
135

    
136
    return 0;
137
    }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

    
141
  private int commonColor(int[][] corners, int index1, int index2, int index3)
142
    {
143
    int[] c1 = corners[index1];
144
    int[] c2 = corners[index2];
145
    int[] c3 = corners[index3];
146

    
147
    for(int i=0; i<4; i++)
148
      {
149
      int c = c1[i];
150

    
151
      if( (c2[0]==c || c2[1]==c || c2[2]==c || c2[3]==c) &&
152
          (c3[0]==c || c3[1]==c || c3[2]==c || c3[3]==c)  ) return c;
153
      }
154

    
155
    return ERROR_CORNERS_CANNOT;
156
    }
157

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159

    
160
  private int figureOutFaceColors(int[] output, int[][] corners)
161
    {
162
    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}};
163

    
164
    for(int i=0; i<8; i++)
165
      {
166
      int[] common = commonCorners[i];
167
      output[i] = commonColor(corners,common[0],common[1],common[2]);
168
      if( output[i]<0 ) return output[i];
169
      }
170

    
171
    return 0;
172
    }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

    
176
  private void getCorners(TwistyObject object, int[][] corners)
177
    {
178
    for(int i=0; i<6; i++)
179
      for(int j=0; j<4; j++)
180
         corners[i][j] = object.getCubitFaceStickerIndex(i,j);
181
    }
182

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184

    
185
  private void getCenters(TwistyObject object, int[] centers)
186
    {
187
    for(int i=0; i<8; i++)
188
       centers[i] = object.getCubitFaceStickerIndex(6+i,0)-8;
189
    }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

    
193
  public int tablebaseIndex(TwistyObject object)
194
    {
195
    int[][] corners = new int[6][4];
196
    int[] centers = new int[8];
197

    
198
    getCorners(object,corners);
199
    getCenters(object,centers);
200

    
201
    int result1 = figureOutFaceColors(mFaceColors,corners);
202
    if( result1<0 ) return result1;
203

    
204
    int result2 = checkAllCentersPresent(centers);
205
    if( result2<0 ) return result2;
206

    
207
    int result3 = checkAllColorsDifferent();
208
    if( result3<0 ) return result3;
209

    
210
    int[] twist = getCornersTwist(corners);
211
    boolean even1 = isTwistEven(twist);
212

    
213
    int[] centers_perm = getCentersPermutation(centers);
214
    boolean even2 = TablebaseHelpers.permutationIsEven(centers_perm);
215
    if( even1^even2 ) return ERROR_TWO_CENTERS;
216

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

    
220
/*
221
android.util.Log.e("D", "faces: "+mFaceColors[0]+" "+mFaceColors[1]+" "+mFaceColors[2]+" "
222
+mFaceColors[3]+" "+mFaceColors[4]+" "+mFaceColors[5]+" "+mFaceColors[6]+" "+mFaceColors[7]);
223
android.util.Log.e("D", "corn twist: "+twist[0]+" "+twist[1]+" "+twist[2]+" "+twist[3]+" "+twist[4]+" "+twist[5]);
224
android.util.Log.e("D", "ret="+(total_twist + 2048*centers_perm_num) );
225
*/
226
    return total_twist + 2048*centers_perm_num;
227
    }
228

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

    
231
  private String centerError(Resources res, int face)
232
    {
233
    int index = getOctColor(face,2);
234
    String color = res.getString(index);
235
    return res.getString(R.string.solver_generic_missing_center,color);
236
    }
237

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239

    
240
  public String error(int index, Resources res)
241
    {
242
    switch(index)
243
      {
244
      case ERROR_CENTER_MISSING : return centerError(res,mErrColor);
245
      case ERROR_TWO_CENTERS    : return res.getString(R.string.solver_generic_two_centers);
246
      case ERROR_CORNERS_CANNOT : return res.getString(R.string.solver_generic_corners_cannot);
247
      }
248

    
249
    return null;
250
    }
251
}  
252

    
(13-13/19)