Revision 373fa45f
Added by Leszek Koltunski over 4 years ago
src/main/java/org/distorted/solvers/ImplementedSolversList.java | ||
---|---|---|
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 org.distorted.objects.RubikObjectList; |
|
23 |
|
|
24 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
25 |
|
|
26 |
public enum ImplementedSolversList |
|
27 |
{ |
|
28 |
CUBE3 ( RubikObjectList.CUBE, 3), |
|
29 |
; |
|
30 |
|
|
31 |
public static final int NUM_OBJECTS = values().length; |
|
32 |
|
|
33 |
private final RubikObjectList mObject; |
|
34 |
private final int mObjectSize; |
|
35 |
|
|
36 |
private static final ImplementedSolversList[] objects; |
|
37 |
|
|
38 |
static |
|
39 |
{ |
|
40 |
objects = new ImplementedSolversList[NUM_OBJECTS]; |
|
41 |
int i=0; |
|
42 |
|
|
43 |
for(ImplementedSolversList object: ImplementedSolversList.values()) |
|
44 |
{ |
|
45 |
objects[i++] = object; |
|
46 |
} |
|
47 |
} |
|
48 |
|
|
49 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
50 |
|
|
51 |
public static RubikObjectList getObject(int ordinal) |
|
52 |
{ |
|
53 |
return objects[ordinal].mObject; |
|
54 |
} |
|
55 |
|
|
56 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
57 |
|
|
58 |
public static int getObjectSize(int ordinal) |
|
59 |
{ |
|
60 |
return objects[ordinal].mObjectSize; |
|
61 |
} |
|
62 |
|
|
63 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
64 |
|
|
65 |
ImplementedSolversList( RubikObjectList object, int size) |
|
66 |
{ |
|
67 |
mObject = object; |
|
68 |
mObjectSize = size; |
|
69 |
} |
|
70 |
} |
src/main/java/org/distorted/solvers/Solver.java | ||
---|---|---|
22 | 22 |
import android.content.res.Resources; |
23 | 23 |
|
24 | 24 |
import org.distorted.main.R; |
25 |
import org.distorted.main.RubikActivity;
|
|
25 |
import org.distorted.objects.RubikObjectList;
|
|
26 | 26 |
import org.distorted.states.RubikState; |
27 | 27 |
import org.distorted.states.RubikStateSolver; |
28 | 28 |
|
... | ... | |
31 | 31 |
public class Solver implements Runnable |
32 | 32 |
{ |
33 | 33 |
private String mObjectPosition; |
34 |
private RubikActivity mAct; |
|
34 |
private Resources mRes; |
|
35 |
private RubikObjectList mObject; |
|
36 |
private int mSize; |
|
35 | 37 |
|
36 | 38 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
37 | 39 |
|
38 |
public Solver(RubikActivity act, String position )
|
|
40 |
public Solver(Resources res, RubikObjectList object, int size, String position )
|
|
39 | 41 |
{ |
42 |
mRes = res; |
|
43 |
mObject = object; |
|
44 |
mSize = size; |
|
40 | 45 |
mObjectPosition = position; |
41 |
mAct = act; |
|
42 | 46 |
} |
43 | 47 |
|
44 | 48 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
45 | 49 |
|
46 |
public void start()
|
|
50 |
private void solveCube3(RubikStateSolver solver)
|
|
47 | 51 |
{ |
48 |
Thread thr = new Thread(this); |
|
49 |
thr.start(); |
|
52 |
String result; |
|
53 |
|
|
54 |
if( !org.distorted.solvers.cube3.Search.prepare(mRes) ) |
|
55 |
result= "Error 9"; |
|
56 |
else |
|
57 |
result = org.distorted.solvers.cube3.Search.solution(mObjectPosition, 24, 20); |
|
58 |
|
|
59 |
if (result.contains("Error")) |
|
60 |
{ |
|
61 |
switch (result.charAt(result.length() - 1)) |
|
62 |
{ |
|
63 |
case '1': result = mRes.getString(R.string.solver_cube3_error1); break; |
|
64 |
case '2': result = mRes.getString(R.string.solver_cube3_error2); break; |
|
65 |
case '3': result = mRes.getString(R.string.solver_cube3_error3); break; |
|
66 |
case '4': result = mRes.getString(R.string.solver_cube3_error4); break; |
|
67 |
case '5': result = mRes.getString(R.string.solver_cube3_error5); break; |
|
68 |
case '6': result = mRes.getString(R.string.solver_cube3_error6); break; |
|
69 |
case '7': result = mRes.getString(R.string.solver_cube3_error7); break; |
|
70 |
case '8': result = mRes.getString(R.string.solver_cube3_error8); break; |
|
71 |
case '9': result = mRes.getString(R.string.solver_cube3_error9); break; |
|
72 |
} |
|
73 |
|
|
74 |
solver.displayErrorDialog(result); |
|
75 |
} |
|
76 |
else |
|
77 |
{ |
|
78 |
solver.setSolved(result); |
|
79 |
} |
|
50 | 80 |
} |
51 | 81 |
|
52 | 82 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
53 | 83 |
|
54 |
public void interrupt()
|
|
84 |
private void interruptCube3()
|
|
55 | 85 |
{ |
56 | 86 |
org.distorted.solvers.cube3.Search.interrupt(); |
57 | 87 |
} |
58 | 88 |
|
89 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
90 |
|
|
91 |
public void start() |
|
92 |
{ |
|
93 |
Thread thr = new Thread(this); |
|
94 |
thr.start(); |
|
95 |
} |
|
96 |
|
|
59 | 97 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
60 | 98 |
|
61 | 99 |
public void run() |
62 | 100 |
{ |
63 |
Resources res = mAct.getResources(); |
|
64 | 101 |
RubikStateSolver solver = (RubikStateSolver) RubikState.SVER.getStateClass(); |
65 |
String result; |
|
66 | 102 |
|
67 |
if( !org.distorted.solvers.cube3.Search.prepare(res) ) |
|
68 |
result= "Error 9"; |
|
69 |
else |
|
70 |
result = org.distorted.solvers.cube3.Search.solution(mObjectPosition, 24, 20); |
|
71 |
|
|
72 |
if (result.contains("Error")) |
|
103 |
if( mObject == RubikObjectList.CUBE && mSize == 3) |
|
73 | 104 |
{ |
74 |
switch (result.charAt(result.length() - 1)) |
|
75 |
{ |
|
76 |
case '1': result = res.getString(R.string.error1); break; |
|
77 |
case '2': result = res.getString(R.string.error2); break; |
|
78 |
case '3': result = res.getString(R.string.error3); break; |
|
79 |
case '4': result = res.getString(R.string.error4); break; |
|
80 |
case '5': result = res.getString(R.string.error5); break; |
|
81 |
case '6': result = res.getString(R.string.error6); break; |
|
82 |
case '7': result = res.getString(R.string.error7); break; |
|
83 |
case '8': result = res.getString(R.string.error8); break; |
|
84 |
case '9': result = res.getString(R.string.error9); break; |
|
85 |
} |
|
86 |
|
|
87 |
solver.displayErrorDialog(mAct,result); |
|
105 |
solveCube3(solver); |
|
88 | 106 |
} |
89 | 107 |
else |
90 | 108 |
{ |
91 |
solver.setSolved( mAct, org.distorted.solvers.cube3.Search.numMoves(), result);
|
|
109 |
solver.displayErrorDialog(mRes.getString(R.string.solver_generic_error1));
|
|
92 | 110 |
} |
93 | 111 |
} |
94 | 112 |
} |
src/main/java/org/distorted/states/RubikStateSolution.java | ||
---|---|---|
250 | 250 |
|
251 | 251 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
252 | 252 |
|
253 |
void setupMoves(final RubikActivity act, int numMoves, String moves)
|
|
253 |
void setupMoves(final RubikActivity act, String moves) |
|
254 | 254 |
{ |
255 |
android.util.Log.e("solution", "got "+numMoves+" moves: "+moves); |
|
256 |
|
|
257 | 255 |
mCanRotate= true; |
258 | 256 |
mCurrMove = 0; |
259 |
mNumMoves = numMoves;
|
|
257 |
mNumMoves = moves.length()/4;
|
|
260 | 258 |
mMoves = new int[mNumMoves][3]; |
261 | 259 |
|
262 |
RubikPattern.parseMoves(mMoves,numMoves,moves);
|
|
260 |
RubikPattern.parseMoves(mMoves,mNumMoves,moves);
|
|
263 | 261 |
|
264 | 262 |
mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves)); |
265 | 263 |
} |
src/main/java/org/distorted/states/RubikStateSolver.java | ||
---|---|---|
39 | 39 |
import org.distorted.main.RubikPostRender; |
40 | 40 |
import org.distorted.objects.RubikObject; |
41 | 41 |
import org.distorted.objects.RubikObjectList; |
42 |
import org.distorted.solvers.ImplementedSolversList; |
|
42 | 43 |
import org.distorted.solvers.Solver; |
43 | 44 |
|
45 |
import java.lang.ref.WeakReference; |
|
46 |
|
|
44 | 47 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
45 | 48 |
|
46 | 49 |
public class RubikStateSolver extends RubikStateAbstract |
47 | 50 |
{ |
48 | 51 |
private static final int BITMAP_SIZE = 35; |
49 | 52 |
|
50 |
private static final RubikObjectList OBJECT = RubikObjectList.CUBE; |
|
51 |
private static final int SIZE = 3; |
|
52 |
|
|
53 | 53 |
private static Bitmap[] mBitmap; |
54 | 54 |
private ImageButton[] mColorButton; |
55 | 55 |
private Button mBackButton, mSolveButton; |
... | ... | |
58 | 58 |
private int[] mFaceColors; |
59 | 59 |
private int mNumFaces; |
60 | 60 |
|
61 |
private RubikObjectList mCurrentObject; |
|
62 |
private int mCurrentObjectSize; |
|
63 |
|
|
64 |
private WeakReference<RubikActivity> mWeakAct; |
|
65 |
|
|
61 | 66 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
62 | 67 |
|
63 | 68 |
void leaveState(RubikActivity act) |
... | ... | |
69 | 74 |
|
70 | 75 |
void enterState(final RubikActivity act) |
71 | 76 |
{ |
77 |
mWeakAct = new WeakReference<>(act); |
|
78 |
|
|
72 | 79 |
mSolving = false; |
73 | 80 |
|
74 |
act.changeObject(OBJECT,SIZE,null); |
|
81 |
mCurrentObject = ImplementedSolversList.getObject(0); |
|
82 |
mCurrentObjectSize = ImplementedSolversList.getObjectSize(0); |
|
83 |
|
|
84 |
act.changeObject(mCurrentObject, mCurrentObjectSize, null); |
|
75 | 85 |
RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass(); |
76 |
play.setObjectAndSize(OBJECT,SIZE);
|
|
86 |
play.setObjectAndSize(mCurrentObject, mCurrentObjectSize);
|
|
77 | 87 |
|
78 |
mFaceColors = RubikObjectList.retFaceColors(OBJECT);
|
|
88 |
mFaceColors = RubikObjectList.retFaceColors(mCurrentObject);
|
|
79 | 89 |
mNumFaces = mFaceColors!=null ? mFaceColors.length : 0; |
80 | 90 |
|
81 | 91 |
DisplayMetrics metrics = act.getResources().getDisplayMetrics(); |
... | ... | |
189 | 199 |
mSolving = true; |
190 | 200 |
RubikObject object = act.getObject(); |
191 | 201 |
String objectString = object.retObjectString(); |
192 |
Solver solver = new Solver( act, objectString ); |
|
202 |
Solver solver = new Solver( act.getResources(), mCurrentObject, mCurrentObjectSize, objectString );
|
|
193 | 203 |
solver.start(); |
194 | 204 |
} |
195 | 205 |
} |
... | ... | |
265 | 275 |
|
266 | 276 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
267 | 277 |
|
268 |
public void setSolved( final RubikActivity act, final int numMoves, final String moves)
|
|
278 |
public void setSolved(final String moves) |
|
269 | 279 |
{ |
270 | 280 |
mSolving = false; |
281 |
final RubikActivity act = mWeakAct.get(); |
|
271 | 282 |
|
272 |
act.runOnUiThread(new Runnable()
|
|
283 |
if( act!=null )
|
|
273 | 284 |
{ |
274 |
@Override |
|
275 |
public void run() |
|
285 |
act.runOnUiThread(new Runnable() |
|
276 | 286 |
{ |
277 |
RubikState.switchState(act,RubikState.SOLU); |
|
278 |
RubikStateSolution solution = (RubikStateSolution) RubikState.SOLU.getStateClass(); |
|
279 |
solution.setupMoves(act, numMoves, moves); |
|
280 |
} |
|
281 |
}); |
|
287 |
@Override |
|
288 |
public void run() |
|
289 |
{ |
|
290 |
RubikState.switchState(act,RubikState.SOLU); |
|
291 |
RubikStateSolution solution = (RubikStateSolution) RubikState.SOLU.getStateClass(); |
|
292 |
solution.setupMoves(act, moves); |
|
293 |
} |
|
294 |
}); |
|
295 |
} |
|
282 | 296 |
} |
283 | 297 |
|
284 | 298 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
285 | 299 |
|
286 |
public void displayErrorDialog( final RubikActivity act, String message)
|
|
300 |
public void displayErrorDialog( String message) |
|
287 | 301 |
{ |
288 | 302 |
mSolving = false; |
303 |
RubikActivity act = mWeakAct.get(); |
|
289 | 304 |
|
290 |
RubikDialogSolverError dialog = new RubikDialogSolverError(); |
|
291 |
Bundle bundle = new Bundle(); |
|
292 |
bundle.putString("error", message ); |
|
293 |
dialog.setArguments(bundle); |
|
294 |
dialog.show( act.getSupportFragmentManager(), null); |
|
305 |
if( act!=null ) |
|
306 |
{ |
|
307 |
RubikDialogSolverError dialog = new RubikDialogSolverError(); |
|
308 |
Bundle bundle = new Bundle(); |
|
309 |
bundle.putString("error", message ); |
|
310 |
dialog.setArguments(bundle); |
|
311 |
dialog.show( act.getSupportFragmentManager(), null); |
|
312 |
} |
|
295 | 313 |
} |
296 | 314 |
} |
src/main/res/values/strings.xml | ||
---|---|---|
39 | 39 |
<string name="credits1">Open Source app developed using the Distorted graphics library. Licensed under GPL version 2 or - at your option - any later version.</string> |
40 | 40 |
<string name="credits2">Download code, take a look at tutorials, learn how to add your own graphics effect, learn how to code your own object, contribute a Pretty Pattern, implement your own solver, or report a bug: \n\n<a href="https://distorted.org/redmine/projects/magic-cube/wiki">Distorted.org</a></string> |
41 | 41 |
|
42 |
<string name="error1">There are not exactly 9 facelets of each color!</string> |
|
43 |
<string name="error2">Not all 12 edges exist exactly once!</string> |
|
44 |
<string name="error3">One edge has to be flipped!</string> |
|
45 |
<string name="error4">Not all 8 corners exist exactly once!</string> |
|
46 |
<string name="error5">One corner has to be twisted!</string> |
|
47 |
<string name="error6">Two corners or two edges have to be exchanged!</string> |
|
48 |
<string name="error7">No solution exists for the given maximum move number!</string> |
|
49 |
<string name="error8">Timeout, no solution found in 20 seconds!</string> |
|
50 |
<string name="error9">Solver interrupted!</string> |
|
42 |
<string name="solver_generic_error1">Solver for this object and size not implemented yet!</string> |
|
43 |
|
|
44 |
<string name="solver_cube3_error1">There are not exactly 9 facelets of each color!</string> |
|
45 |
<string name="solver_cube3_error2">Not all 12 edges exist exactly once!</string> |
|
46 |
<string name="solver_cube3_error3">One edge has to be flipped!</string> |
|
47 |
<string name="solver_cube3_error4">Not all 8 corners exist exactly once!</string> |
|
48 |
<string name="solver_cube3_error5">One corner has to be twisted!</string> |
|
49 |
<string name="solver_cube3_error6">Two corners or two edges have to be exchanged!</string> |
|
50 |
<string name="solver_cube3_error7">No solution exists for the given maximum move number!</string> |
|
51 |
<string name="solver_cube3_error8">Timeout, no solution found in 20 seconds!</string> |
|
52 |
<string name="solver_cube3_error9">Solver interrupted!</string> |
|
51 | 53 |
|
52 | 54 |
<string name="ms_placeholder">%1$d ms</string> |
53 | 55 |
<string name="sc_placeholder">Scramble %1$d</string> |
Also available in: Unified diff
Progress making the Solver state more abstract.