1 |
f0336037
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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.main.R;
|
25 |
373fa45f
|
Leszek Koltunski
|
import org.distorted.objects.RubikObjectList;
|
26 |
f0336037
|
Leszek Koltunski
|
import org.distorted.states.RubikState;
|
27 |
|
|
import org.distorted.states.RubikStateSolver;
|
28 |
|
|
|
29 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
30 |
|
|
|
31 |
a304ee64
|
Leszek Koltunski
|
public class SolverMain implements Runnable
|
32 |
f0336037
|
Leszek Koltunski
|
{
|
33 |
|
|
private String mObjectPosition;
|
34 |
373fa45f
|
Leszek Koltunski
|
private Resources mRes;
|
35 |
|
|
private RubikObjectList mObject;
|
36 |
|
|
private int mSize;
|
37 |
f0336037
|
Leszek Koltunski
|
|
38 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
39 |
|
|
|
40 |
a304ee64
|
Leszek Koltunski
|
public SolverMain(Resources res, RubikObjectList object, int size, String position )
|
41 |
f0336037
|
Leszek Koltunski
|
{
|
42 |
373fa45f
|
Leszek Koltunski
|
mRes = res;
|
43 |
|
|
mObject = object;
|
44 |
|
|
mSize = size;
|
45 |
f0336037
|
Leszek Koltunski
|
mObjectPosition = position;
|
46 |
|
|
}
|
47 |
|
|
|
48 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
49 |
46a961fd
|
Leszek Koltunski
|
// certain objects have certain cubits locked - for example, the Cube3's centers of
|
50 |
|
|
// sides always have the same color.
|
51 |
|
|
// If a certain cubit is locked, return the color (index into it's FACE_COLORS array) it
|
52 |
|
|
// must have. Otherwise return -1.
|
53 |
|
|
|
54 |
|
|
public static int cubitIsLocked(RubikObjectList object, int size, int cubit)
|
55 |
|
|
{
|
56 |
|
|
if( object == RubikObjectList.CUBE && size == 3)
|
57 |
|
|
{
|
58 |
|
|
if( cubit==21 ) return 0; // center of the right face
|
59 |
|
|
if( cubit== 4 ) return 1; // center of the left face
|
60 |
|
|
if( cubit==15 ) return 2; // center of the up face
|
61 |
|
|
if( cubit==10 ) return 3; // center of the bottom face
|
62 |
|
|
if( cubit==13 ) return 4; // center of the front face
|
63 |
|
|
if( cubit==12 ) return 5; // center of the back face
|
64 |
|
|
}
|
65 |
|
|
|
66 |
|
|
return -1;
|
67 |
|
|
}
|
68 |
|
|
|
69 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
70 |
f0336037
|
Leszek Koltunski
|
|
71 |
373fa45f
|
Leszek Koltunski
|
private void solveCube3(RubikStateSolver solver)
|
72 |
f0336037
|
Leszek Koltunski
|
{
|
73 |
373fa45f
|
Leszek Koltunski
|
String result;
|
74 |
|
|
|
75 |
|
|
if( !org.distorted.solvers.cube3.Search.prepare(mRes) )
|
76 |
|
|
result= "Error 9";
|
77 |
|
|
else
|
78 |
|
|
result = org.distorted.solvers.cube3.Search.solution(mObjectPosition, 24, 20);
|
79 |
|
|
|
80 |
|
|
if (result.contains("Error"))
|
81 |
|
|
{
|
82 |
|
|
switch (result.charAt(result.length() - 1))
|
83 |
|
|
{
|
84 |
|
|
case '1': result = mRes.getString(R.string.solver_cube3_error1); break;
|
85 |
|
|
case '2': result = mRes.getString(R.string.solver_cube3_error2); break;
|
86 |
|
|
case '3': result = mRes.getString(R.string.solver_cube3_error3); break;
|
87 |
|
|
case '4': result = mRes.getString(R.string.solver_cube3_error4); break;
|
88 |
|
|
case '5': result = mRes.getString(R.string.solver_cube3_error5); break;
|
89 |
|
|
case '6': result = mRes.getString(R.string.solver_cube3_error6); break;
|
90 |
|
|
case '7': result = mRes.getString(R.string.solver_cube3_error7); break;
|
91 |
|
|
case '8': result = mRes.getString(R.string.solver_cube3_error8); break;
|
92 |
|
|
case '9': result = mRes.getString(R.string.solver_cube3_error9); break;
|
93 |
|
|
}
|
94 |
|
|
|
95 |
|
|
solver.displayErrorDialog(result);
|
96 |
|
|
}
|
97 |
|
|
else
|
98 |
|
|
{
|
99 |
|
|
solver.setSolved(result);
|
100 |
|
|
}
|
101 |
f0336037
|
Leszek Koltunski
|
}
|
102 |
|
|
|
103 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
104 |
|
|
|
105 |
373fa45f
|
Leszek Koltunski
|
private void interruptCube3()
|
106 |
f0336037
|
Leszek Koltunski
|
{
|
107 |
|
|
org.distorted.solvers.cube3.Search.interrupt();
|
108 |
|
|
}
|
109 |
|
|
|
110 |
373fa45f
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
111 |
|
|
|
112 |
|
|
public void start()
|
113 |
|
|
{
|
114 |
|
|
Thread thr = new Thread(this);
|
115 |
|
|
thr.start();
|
116 |
|
|
}
|
117 |
|
|
|
118 |
f0336037
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
119 |
|
|
|
120 |
|
|
public void run()
|
121 |
|
|
{
|
122 |
|
|
RubikStateSolver solver = (RubikStateSolver) RubikState.SVER.getStateClass();
|
123 |
|
|
|
124 |
373fa45f
|
Leszek Koltunski
|
if( mObject == RubikObjectList.CUBE && mSize == 3)
|
125 |
f0336037
|
Leszek Koltunski
|
{
|
126 |
373fa45f
|
Leszek Koltunski
|
solveCube3(solver);
|
127 |
f0336037
|
Leszek Koltunski
|
}
|
128 |
|
|
else
|
129 |
|
|
{
|
130 |
373fa45f
|
Leszek Koltunski
|
solver.displayErrorDialog(mRes.getString(R.string.solver_generic_error1));
|
131 |
f0336037
|
Leszek Koltunski
|
}
|
132 |
|
|
}
|
133 |
|
|
}
|