Project

General

Profile

« Previous | Next » 

Revision 420eb96d

Added by Leszek Koltunski almost 2 years ago

Beginnings of the Pyraminx Duo solver.

View differences:

src/main/java/org/distorted/solvers/SolverCube3.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.TwistyObject;
16
import org.distorted.screens.RubikScreenSolver;
17
import org.distorted.solvers.cube3.SolverSearch;
18

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

  
21
public class SolverCube3
22
{
23
  private final Resources mRes;
24
  private final TwistyObject mObject;
25
  private int mColorID;
26

  
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28

  
29
  private int mapCubitToFace(int cubit, int face)
30
    {
31
    if( cubit<8 )
32
      {
33
      switch(face)
34
        {
35
        case 0: return 1;
36
        case 1: if( cubit==2 ) return 5;
37
                if( cubit==1 ) return 3;
38
                return 1;
39
        case 2: return cubit==7 ? 5 : 3;
40
        case 3: if( cubit==1 ) return 1;
41
                return cubit==4 ? 5 : 3;
42
        case 4: return cubit==7 ? 3 : 5;
43
        case 5: if( cubit==2 ) return 1;
44
                if( cubit==4 ) return 3;
45
                return 5;
46
        }
47
      }
48

  
49
    if( cubit>19 ) return 4;
50

  
51
    switch(face)
52
      {
53
      case 0: return cubit==15 || cubit==18 ? 3 : 5;
54
      case 1: return cubit==13 || cubit==16 ? 3 : 5;
55
      case 2: return cubit==10              ? 5 : 3;
56
      case 3: return cubit== 8              ? 3 : 5;
57
      case 4: return cubit== 9              ? 3 : 5;
58
      case 5: return cubit== 8              ? 5 : 3;
59
      }
60

  
61
    return -1;
62
    }
63

  
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

  
66
  private int checkPosition(String position)
67
    {
68
    int[] numColors = new int[6];
69
    int len = position.length();
70

  
71
    for(int i=0; i<len; i++)
72
      {
73
      char ch = position.charAt(i);
74

  
75
      switch(ch)
76
        {
77
        case 'R': numColors[0]++; break;
78
        case 'L': numColors[1]++; break;
79
        case 'U': numColors[2]++; break;
80
        case 'D': numColors[3]++; break;
81
        case 'F': numColors[4]++; break;
82
        case 'B': numColors[5]++; break;
83
        }
84
      }
85

  
86
    if( numColors[0]<9 ) { mColorID = R.string.color_yellow; return numColors[0]; }
87
    if( numColors[1]<9 ) { mColorID = R.string.color_white ; return numColors[1]; }
88
    if( numColors[2]<9 ) { mColorID = R.string.color_blue  ; return numColors[2]; }
89
    if( numColors[3]<9 ) { mColorID = R.string.color_green ; return numColors[3]; }
90
    if( numColors[4]<9 ) { mColorID = R.string.color_red   ; return numColors[4]; }
91
    if( numColors[5]<9 ) { mColorID = R.string.color_orange; return numColors[5]; }
92

  
93
    return -1;
94
    }
95

  
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
// order: Up --> Right --> Front --> Down --> Left --> Back
98
// (because the first implemented Solver - the two-phase Cube3 one - expects such order)
99
//
100
// Solved 3x3x3 Cube maps to "UUUUUUUUURRRRRRRRRFFFFFFFFFDDDDDDDDDLLLLLLLLLBBBBBBBBB"
101

  
102
  private String preparePosition()
103
    {
104
    StringBuilder objectString = new StringBuilder();
105

  
106
    final int R = 0;
107
    final int L = 1;
108
    final int U = 2;
109
    final int D = 3;
110
    final int F = 4;
111
    final int B = 5;
112

  
113
    // 'I' - interior, theoretically can happen
114
    final char[] FACE_NAMES = { 'R', 'L', 'U', 'D', 'F', 'B', 'I'};
115

  
116
    final int[] U_INDEX = { 2,10, 6,17,22,19, 3,11, 7};
117
    final int[] R_INDEX = { 7,19, 6,15,20,14, 5,18, 4};
118
    final int[] F_INDEX = { 3,11, 7,13,24,15, 1, 9, 5};
119
    final int[] D_INDEX = { 1, 9, 5,16,23,18, 0, 8, 4};
120
    final int[] L_INDEX = { 2,17, 3,12,21,13, 0,16, 1};
121
    final int[] B_INDEX = { 6,10, 2,14,25,12, 4, 8, 0};
122

  
123
    for(int i=0; i<9; i++)
124
      {
125
      int face = mapCubitToFace(U_INDEX[i],U);
126
      int color = mObject.getCubitFaceColorIndex(U_INDEX[i], face);
127
      objectString.append(FACE_NAMES[color]);
128
      }
129
    for(int i=0; i<9; i++)
130
      {
131
      int face = mapCubitToFace(R_INDEX[i],R);
132
      int color = mObject.getCubitFaceColorIndex(R_INDEX[i], face);
133
      objectString.append(FACE_NAMES[color]);
134
      }
135
    for(int i=0; i<9; i++)
136
      {
137
      int face = mapCubitToFace(F_INDEX[i],F);
138
      int color = mObject.getCubitFaceColorIndex(F_INDEX[i], face);
139
      objectString.append(FACE_NAMES[color]);
140
      }
141
    for(int i=0; i<9; i++)
142
      {
143
      int face = mapCubitToFace(D_INDEX[i],D);
144
      int color = mObject.getCubitFaceColorIndex(D_INDEX[i], face);
145
      objectString.append(FACE_NAMES[color]);
146
      }
147
    for(int i=0; i<9; i++)
148
      {
149
      int face = mapCubitToFace(L_INDEX[i],L);
150
      int color = mObject.getCubitFaceColorIndex(L_INDEX[i], face);
151
      objectString.append(FACE_NAMES[color]);
152
      }
153
    for(int i=0; i<9; i++)
154
      {
155
      int face = mapCubitToFace(B_INDEX[i],B);
156
      int color = mObject.getCubitFaceColorIndex(B_INDEX[i], face);
157
      objectString.append(FACE_NAMES[color]);
158
      }
159

  
160
    return objectString.toString();
161
    }
162

  
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164
// PUBLIC API
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

  
167
  public SolverCube3(Resources res, TwistyObject object)
168
    {
169
    mRes   = res;
170
    mObject= object;
171
    }
172

  
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

  
175
  public void solve(RubikScreenSolver solver)
176
    {
177
    String result;
178

  
179
    SolverSearch.prepare(mRes);
180
    String objectPosition = preparePosition();
181
    int check = checkPosition(objectPosition);
182

  
183
    if( check<0 )
184
      {
185
      result = SolverSearch.solution(objectPosition, 24, 20);
186

  
187
      if (result.contains("Error"))
188
        {
189
        switch (result.charAt(result.length() - 1))
190
          {
191
          case '1': result = mRes.getString(R.string.solver_cube3_error1); break;
192
          case '2': result = mRes.getString(R.string.solver_cube3_error2); break;
193
          case '3': result = mRes.getString(R.string.solver_cube3_error3); break;
194
          case '4': result = mRes.getString(R.string.solver_cube3_error4); break;
195
          case '5': result = mRes.getString(R.string.solver_cube3_error5); break;
196
          case '6': result = mRes.getString(R.string.solver_cube3_error6); break;
197
          case '7': result = mRes.getString(R.string.solver_cube3_error7); break;
198
          case '8': result = mRes.getString(R.string.solver_cube3_error8); break;
199
          case '9': result = mRes.getString(R.string.solver_cube3_error9); break;
200
          }
201

  
202
        solver.displayErrorDialog(result);
203
        }
204
      else
205
        {
206
        solver.setSolved(result);
207
        }
208
      }
209
    else
210
      {
211
      String color = mRes.getString(mColorID);
212
      result = mRes.getString(R.string.solver_cube3_error1,check,color);
213
      solver.displayErrorDialog(result);
214
      }
215
    }
216
}  
217

  
src/main/java/org/distorted/solvers/SolverMain.java
17 17
import org.distorted.main.R;
18 18
import org.distorted.screens.ScreenList;
19 19
import org.distorted.screens.RubikScreenSolver;
20
import org.distorted.solvers.cube3.SolverSearch;
20
import org.distorted.solvers.pduo.SolverPyraminxDuo;
21 21

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

  
24 24
public class SolverMain implements Runnable
25 25
{
26 26
  private final Resources mRes;
27
  private final TwistyObject mObject;
28 27
  private final long mSignature;
29
  private int mColorID;
28
  private final TwistyObject mObject;
30 29

  
31 30
///////////////////////////////////////////////////////////////////////////////////////////////////
32 31

  
......
59 58
    return -1;
60 59
    }
61 60

  
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

  
64
  private void solveCube3(RubikScreenSolver solver)
65
    {
66
    String result;
67

  
68
    SolverSearch.prepare(mRes);
69
    String objectPosition = prepareCube3position();
70
    int check = checkPosition(objectPosition);
71

  
72
    if( check<0 )
73
      {
74
      result = SolverSearch.solution(objectPosition, 24, 20);
75

  
76
      if (result.contains("Error"))
77
        {
78
        switch (result.charAt(result.length() - 1))
79
          {
80
          case '1': result = mRes.getString(R.string.solver_cube3_error1); break;
81
          case '2': result = mRes.getString(R.string.solver_cube3_error2); break;
82
          case '3': result = mRes.getString(R.string.solver_cube3_error3); break;
83
          case '4': result = mRes.getString(R.string.solver_cube3_error4); break;
84
          case '5': result = mRes.getString(R.string.solver_cube3_error5); break;
85
          case '6': result = mRes.getString(R.string.solver_cube3_error6); break;
86
          case '7': result = mRes.getString(R.string.solver_cube3_error7); break;
87
          case '8': result = mRes.getString(R.string.solver_cube3_error8); break;
88
          case '9': result = mRes.getString(R.string.solver_cube3_error9); break;
89
          }
90

  
91
        solver.displayErrorDialog(result);
92
        }
93
      else
94
        {
95
        solver.setSolved(result);
96
        }
97
      }
98
    else
99
      {
100
      String color = mRes.getString(mColorID);
101
      result = mRes.getString(R.string.solver_cube3_error1,check,color);
102
      solver.displayErrorDialog(result);
103
      }
104
    }
105

  
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

  
108
  private int mapCubitToFace(int cubit, int face)
109
    {
110
    if( cubit<8 )
111
      {
112
      switch(face)
113
        {
114
        case 0: return 1;
115
        case 1: if( cubit==2 ) return 5;
116
                if( cubit==1 ) return 3;
117
                return 1;
118
        case 2: return cubit==7 ? 5 : 3;
119
        case 3: if( cubit==1 ) return 1;
120
                return cubit==4 ? 5 : 3;
121
        case 4: return cubit==7 ? 3 : 5;
122
        case 5: if( cubit==2 ) return 1;
123
                if( cubit==4 ) return 3;
124
                return 5;
125
        }
126
      }
127

  
128
    if( cubit>19 ) return 4;
129

  
130
    switch(face)
131
      {
132
      case 0: return cubit==15 || cubit==18 ? 3 : 5;
133
      case 1: return cubit==13 || cubit==16 ? 3 : 5;
134
      case 2: return cubit==10              ? 5 : 3;
135
      case 3: return cubit== 8              ? 3 : 5;
136
      case 4: return cubit== 9              ? 3 : 5;
137
      case 5: return cubit== 8              ? 5 : 3;
138
      }
139

  
140
    return -1;
141
    }
142

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

  
145
  private int checkPosition(String position)
146
    {
147
    int[] numColors = new int[6];
148
    int len = position.length();
149

  
150
    for(int i=0; i<len; i++)
151
      {
152
      char ch = position.charAt(i);
153

  
154
      switch(ch)
155
        {
156
        case 'R': numColors[0]++; break;
157
        case 'L': numColors[1]++; break;
158
        case 'U': numColors[2]++; break;
159
        case 'D': numColors[3]++; break;
160
        case 'F': numColors[4]++; break;
161
        case 'B': numColors[5]++; break;
162
        }
163
      }
164

  
165
    if( numColors[0]<9 ) { mColorID = R.string.color_yellow; return numColors[0]; }
166
    if( numColors[1]<9 ) { mColorID = R.string.color_white ; return numColors[1]; }
167
    if( numColors[2]<9 ) { mColorID = R.string.color_blue  ; return numColors[2]; }
168
    if( numColors[3]<9 ) { mColorID = R.string.color_green ; return numColors[3]; }
169
    if( numColors[4]<9 ) { mColorID = R.string.color_red   ; return numColors[4]; }
170
    if( numColors[5]<9 ) { mColorID = R.string.color_orange; return numColors[5]; }
171

  
172
    return -1;
173
    }
174

  
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176
// order: Up --> Right --> Front --> Down --> Left --> Back
177
// (because the first implemented Solver - the two-phase Cube3 one - expects such order)
178
//
179
// Solved 3x3x3 Cube maps to "UUUUUUUUURRRRRRRRRFFFFFFFFFDDDDDDDDDLLLLLLLLLBBBBBBBBB"
180

  
181
  private String prepareCube3position()
182
    {
183
    StringBuilder objectString = new StringBuilder();
184

  
185
    final int R = 0;
186
    final int L = 1;
187
    final int U = 2;
188
    final int D = 3;
189
    final int F = 4;
190
    final int B = 5;
191

  
192
    // 'I' - interior, theoretically can happen
193
    final char[] FACE_NAMES = { 'R', 'L', 'U', 'D', 'F', 'B', 'I'};
194

  
195
    final int[] U_INDEX = { 2,10, 6,17,22,19, 3,11, 7};
196
    final int[] R_INDEX = { 7,19, 6,15,20,14, 5,18, 4};
197
    final int[] F_INDEX = { 3,11, 7,13,24,15, 1, 9, 5};
198
    final int[] D_INDEX = { 1, 9, 5,16,23,18, 0, 8, 4};
199
    final int[] L_INDEX = { 2,17, 3,12,21,13, 0,16, 1};
200
    final int[] B_INDEX = { 6,10, 2,14,25,12, 4, 8, 0};
201

  
202
    for(int i=0; i<9; i++)
203
      {
204
      int face = mapCubitToFace(U_INDEX[i],U);
205
      int color = mObject.getCubitFaceColorIndex(U_INDEX[i], face);
206
      objectString.append(FACE_NAMES[color]);
207
      }
208
    for(int i=0; i<9; i++)
209
      {
210
      int face = mapCubitToFace(R_INDEX[i],R);
211
      int color = mObject.getCubitFaceColorIndex(R_INDEX[i], face);
212
      objectString.append(FACE_NAMES[color]);
213
      }
214
    for(int i=0; i<9; i++)
215
      {
216
      int face = mapCubitToFace(F_INDEX[i],F);
217
      int color = mObject.getCubitFaceColorIndex(F_INDEX[i], face);
218
      objectString.append(FACE_NAMES[color]);
219
      }
220
    for(int i=0; i<9; i++)
221
      {
222
      int face = mapCubitToFace(D_INDEX[i],D);
223
      int color = mObject.getCubitFaceColorIndex(D_INDEX[i], face);
224
      objectString.append(FACE_NAMES[color]);
225
      }
226
    for(int i=0; i<9; i++)
227
      {
228
      int face = mapCubitToFace(L_INDEX[i],L);
229
      int color = mObject.getCubitFaceColorIndex(L_INDEX[i], face);
230
      objectString.append(FACE_NAMES[color]);
231
      }
232
    for(int i=0; i<9; i++)
233
      {
234
      int face = mapCubitToFace(B_INDEX[i],B);
235
      int color = mObject.getCubitFaceColorIndex(B_INDEX[i], face);
236
      objectString.append(FACE_NAMES[color]);
237
      }
238

  
239
    return objectString.toString();
240
    }
241

  
242 61
///////////////////////////////////////////////////////////////////////////////////////////////////
243 62

  
244 63
  public void start()
......
251 70

  
252 71
  public void run()
253 72
    {
254
    RubikScreenSolver solver = (RubikScreenSolver) ScreenList.SVER.getScreenClass();
73
    RubikScreenSolver screen = (RubikScreenSolver) ScreenList.SVER.getScreenClass();
255 74

  
256 75
    if( mSignature==ObjectSignatures.CUBE_3 )
257 76
      {
258
      solveCube3(solver);
77
      SolverCube3 solver = new SolverCube3(mRes,mObject);
78
      solver.solve(screen);
79
      }
80
    if( mSignature==ObjectSignatures.PDUO_2 )
81
      {
82
      SolverTablebase solver = new SolverPyraminxDuo(mRes,mObject);
83
      solver.solve(screen);
259 84
      }
260 85
    else
261 86
      {
262
      solver.displayErrorDialog(mRes.getString(R.string.solver_generic_error1));
87
      screen.displayErrorDialog(mRes.getString(R.string.solver_generic_error1));
263 88
      }
264 89
    }
265 90
}  
src/main/java/org/distorted/solvers/SolverTablebase.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.objectlib.main.TwistyObject;
15
import org.distorted.screens.RubikScreenSolver;
16

  
17
///////////////////////////////////////////////////////////////////////////////////////////////////
18

  
19
public abstract class SolverTablebase
20
{
21
  private final Resources mRes;
22
  private final TwistyObject mObject;
23

  
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25

  
26
  public abstract String constructError();
27
  public abstract int[] prepareQuats(TwistyObject object);
28
  public abstract String solution(int[] quats, Resources res);
29

  
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31
// PUBLIC API
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33

  
34
  public SolverTablebase(Resources res, TwistyObject object)
35
    {
36
    mRes   = res;
37
    mObject= object;
38
    }
39

  
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

  
42
  public void solve(RubikScreenSolver solver)
43
    {
44
    String result;
45

  
46
    int[] quats = prepareQuats(mObject);
47

  
48
    if( quats!=null )
49
      {
50
      result = solution(quats,mRes);
51
      solver.setSolved(result);
52
      }
53
    else
54
      {
55
      String error = constructError();
56
      solver.displayErrorDialog(error);
57
      }
58
    }
59
}  
60

  
src/main/java/org/distorted/solvers/pduo/SolverPyraminxDuo.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.pduo;
11

  
12
import android.content.res.Resources;
13

  
14
import org.distorted.objectlib.main.TwistyObject;
15
import org.distorted.solvers.SolverTablebase;
16

  
17
///////////////////////////////////////////////////////////////////////////////////////////////////
18

  
19
public class SolverPyraminxDuo extends SolverTablebase
20
{
21
  private int mErrorID;
22

  
23
///////////////////////////////////////////////////////////////////////////////////////////////////
24

  
25
  public SolverPyraminxDuo(Resources res, TwistyObject object)
26
    {
27
    super(res,object);
28
    }
29

  
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31
// TODO
32
  public String constructError()
33
    {
34
    return null;
35
    }
36

  
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38
// TODO
39
  public int[] prepareQuats(TwistyObject object)
40
    {
41
    return null;
42
    }
43

  
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45
// TODO
46
  public String solution(int[] quats, Resources res)
47
    {
48
    return null;
49
    }
50
}  
51

  

Also available in: Unified diff