Project

General

Profile

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

magiccube / src / main / java / org / distorted / solvers / SolverMain.java @ 97193a09

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.solvers;
21

    
22
import android.content.res.Resources;
23

    
24
import org.distorted.objectlib.main.ObjectType;
25
import org.distorted.objectlib.main.TwistyObject;
26

    
27
import org.distorted.main.R;
28
import org.distorted.screens.ScreenList;
29
import org.distorted.screens.RubikScreenSolver;
30

    
31
///////////////////////////////////////////////////////////////////////////////////////////////////
32

    
33
public class SolverMain implements Runnable
34
{
35
  private final Resources mRes;
36
  private final TwistyObject mObject;
37

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

    
40
  public SolverMain(Resources res, TwistyObject object)
41
    {
42
    mRes   = res;
43
    mObject= object;
44
    }
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47
// certain objects have certain cubits locked - for example, the Cube3's centers of
48
// sides always have the same color.
49
// If a certain cubit is locked, return the color (index into it's FACE_COLORS array) it
50
// must have. Otherwise return -1.
51

    
52
  public static int cubitIsLocked(ObjectType object, int cubit)
53
    {
54
    if( object == ObjectType.CUBE_3 )
55
      {
56
      if( cubit==20 ) return 0; // center of the right  face
57
      if( cubit==21 ) return 1; // center of the left   face
58
      if( cubit==22 ) return 2; // center of the up     face
59
      if( cubit==23 ) return 3; // center of the bottom face
60
      if( cubit==24 ) return 4; // center of the front  face
61
      if( cubit==25 ) return 5; // center of the back   face
62
      }
63

    
64
    return -1;
65
    }
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

    
69
  private void solveCube3(RubikScreenSolver solver)
70
    {
71
    String result;
72

    
73
    if( !org.distorted.solvers.cube3.Search.prepare(mRes) )
74
      result= "Error 9";
75
    else
76
      {
77
      String objectPosition = prepareCube3position();
78
      result = org.distorted.solvers.cube3.Search.solution(objectPosition, 24, 20);
79
      }
80

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

    
96
      solver.displayErrorDialog(result);
97
      }
98
    else
99
      {
100
      solver.setSolved(result);
101
      }
102
    }
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105
// order: Up --> Right --> Front --> Down --> Left --> Back
106
// (because the first implemented Solver - the two-phase Cube3 one - expects such order)
107
//
108
// Solved 3x3x3 Cube maps to "UUUUUUUUURRRRRRRRRFFFFFFFFFDDDDDDDDDLLLLLLLLLBBBBBBBBB"
109

    
110
  private String prepareCube3position()
111
    {
112
    StringBuilder objectString = new StringBuilder();
113

    
114
    final int R = 0;
115
    final int L = 1;
116
    final int U = 2;
117
    final int D = 3;
118
    final int F = 4;
119
    final int B = 5;
120

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

    
124
    final int[] U_INDEX = { 2,10, 6,17,22,19, 3,11, 7};
125
    final int[] R_INDEX = { 7,19, 6,15,20,14, 5,18, 4};
126
    final int[] F_INDEX = { 3,11, 7,13,24,15, 1, 9, 5};
127
    final int[] D_INDEX = { 1, 9, 5,16,23,18, 0, 8, 4};
128
    final int[] L_INDEX = { 2,17, 3,12,21,13, 0,16, 1};
129
    final int[] B_INDEX = { 6,10, 2,14,25,12, 4, 8, 0};
130

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

    
162
android.util.Log.e("D", objectString.toString());
163

    
164
    return objectString.toString();
165
    }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168

    
169
  private void interruptCube3()
170
    {
171
    org.distorted.solvers.cube3.Search.interrupt();
172
    }
173

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

    
176
  public void start()
177
    {
178
    Thread thr = new Thread(this);
179
    thr.start();
180
    }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

    
184
  public void run()
185
    {
186
    RubikScreenSolver solver = (RubikScreenSolver) ScreenList.SVER.getScreenClass();
187

    
188
    if( mObject!=null && mObject.getObjectType()==ObjectType.CUBE_3 )
189
      {
190
      solveCube3(solver);
191
      }
192
    else
193
      {
194
      solver.displayErrorDialog(mRes.getString(R.string.solver_generic_error1));
195
      }
196
    }
197
}  
198

    
(2-2/2)