1 |
7289fd6c
|
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 |
1f9772f3
|
Leszek Koltunski
|
package org.distorted.states;
|
21 |
7289fd6c
|
Leszek Koltunski
|
|
22 |
|
|
import android.content.SharedPreferences;
|
23 |
5660465b
|
Leszek Koltunski
|
import android.content.res.Resources;
|
24 |
7289fd6c
|
Leszek Koltunski
|
import android.graphics.Bitmap;
|
25 |
|
|
import android.graphics.Canvas;
|
26 |
|
|
import android.graphics.Paint;
|
27 |
473611ee
|
Leszek Koltunski
|
import android.graphics.PorterDuff;
|
28 |
|
|
import android.graphics.drawable.Drawable;
|
29 |
ca292407
|
Leszek Koltunski
|
import android.os.Bundle;
|
30 |
473611ee
|
Leszek Koltunski
|
import android.support.v4.content.ContextCompat;
|
31 |
7289fd6c
|
Leszek Koltunski
|
import android.util.DisplayMetrics;
|
32 |
|
|
import android.view.View;
|
33 |
|
|
import android.widget.Button;
|
34 |
|
|
import android.widget.ImageButton;
|
35 |
|
|
import android.widget.LinearLayout;
|
36 |
|
|
|
37 |
ca292407
|
Leszek Koltunski
|
import org.distorted.dialogs.RubikDialogSolverError;
|
38 |
1f9772f3
|
Leszek Koltunski
|
import org.distorted.main.R;
|
39 |
|
|
import org.distorted.main.RubikActivity;
|
40 |
473611ee
|
Leszek Koltunski
|
import org.distorted.main.RubikPostRender;
|
41 |
1f9772f3
|
Leszek Koltunski
|
import org.distorted.objects.RubikObjectList;
|
42 |
7289fd6c
|
Leszek Koltunski
|
|
43 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
44 |
|
|
|
45 |
|
|
public class RubikStateSolver extends RubikStateAbstract
|
46 |
|
|
{
|
47 |
|
|
private static final int NUM_FACES = 6;
|
48 |
|
|
private static final int BITMAP_SIZE = 35;
|
49 |
|
|
|
50 |
|
|
private static final int[] FACE_COLORS = new int[]
|
51 |
|
|
{
|
52 |
|
|
0xffffff00, 0xffffffff,
|
53 |
|
|
0xff0000ff, 0xff00ff00,
|
54 |
|
|
0xffff0000, 0xffb5651d
|
55 |
|
|
};
|
56 |
|
|
|
57 |
|
|
private static Bitmap[] mBitmap;
|
58 |
|
|
private ImageButton[] mColorButton;
|
59 |
264af0ad
|
Leszek Koltunski
|
private Button mBackButton, mSolveButton;
|
60 |
5660465b
|
Leszek Koltunski
|
private boolean mSolving;
|
61 |
1f9772f3
|
Leszek Koltunski
|
private int mCurrentColor;
|
62 |
5660465b
|
Leszek Koltunski
|
|
63 |
|
|
private class Solver implements Runnable
|
64 |
|
|
{
|
65 |
|
|
private String mCubeString;
|
66 |
|
|
private RubikActivity mAct;
|
67 |
|
|
|
68 |
|
|
///////////////////////////////////////////////////
|
69 |
|
|
|
70 |
|
|
Solver(String str, RubikActivity act)
|
71 |
efe00e0f
|
Leszek Koltunski
|
{
|
72 |
|
|
mCubeString = str;
|
73 |
|
|
mAct = act;
|
74 |
|
|
}
|
75 |
5660465b
|
Leszek Koltunski
|
|
76 |
|
|
///////////////////////////////////////////////////
|
77 |
|
|
|
78 |
|
|
public void start()
|
79 |
|
|
{
|
80 |
efe00e0f
|
Leszek Koltunski
|
Thread thr = new Thread(this);
|
81 |
|
|
thr.start();
|
82 |
5660465b
|
Leszek Koltunski
|
}
|
83 |
|
|
|
84 |
|
|
///////////////////////////////////////////////////
|
85 |
|
|
|
86 |
|
|
public void interrupt()
|
87 |
|
|
{
|
88 |
efe00e0f
|
Leszek Koltunski
|
org.distorted.solvers.cube3.Search.interrupt();
|
89 |
5660465b
|
Leszek Koltunski
|
}
|
90 |
|
|
|
91 |
|
|
///////////////////////////////////////////////////
|
92 |
|
|
|
93 |
|
|
public void run()
|
94 |
efe00e0f
|
Leszek Koltunski
|
{
|
95 |
|
|
Resources res = mAct.getResources();
|
96 |
|
|
String result;
|
97 |
5660465b
|
Leszek Koltunski
|
|
98 |
efe00e0f
|
Leszek Koltunski
|
if( !org.distorted.solvers.cube3.Search.prepare(res) )
|
99 |
|
|
result= "Error 9";
|
100 |
|
|
else
|
101 |
|
|
result = org.distorted.solvers.cube3.Search.solution(mCubeString, 24, 20);
|
102 |
5660465b
|
Leszek Koltunski
|
|
103 |
ca292407
|
Leszek Koltunski
|
mSolving = false;
|
104 |
|
|
|
105 |
efe00e0f
|
Leszek Koltunski
|
if (result.contains("Error"))
|
106 |
|
|
{
|
107 |
|
|
switch (result.charAt(result.length() - 1))
|
108 |
|
|
{
|
109 |
|
|
case '1': result = res.getString(R.string.error1); break;
|
110 |
|
|
case '2': result = res.getString(R.string.error2); break;
|
111 |
|
|
case '3': result = res.getString(R.string.error3); break;
|
112 |
|
|
case '4': result = res.getString(R.string.error4); break;
|
113 |
|
|
case '5': result = res.getString(R.string.error5); break;
|
114 |
|
|
case '6': result = res.getString(R.string.error6); break;
|
115 |
|
|
case '7': result = res.getString(R.string.error7); break;
|
116 |
|
|
case '8': result = res.getString(R.string.error8); break;
|
117 |
|
|
case '9': result = res.getString(R.string.error9); break;
|
118 |
|
|
}
|
119 |
5660465b
|
Leszek Koltunski
|
|
120 |
ca292407
|
Leszek Koltunski
|
RubikDialogSolverError dialog = new RubikDialogSolverError();
|
121 |
|
|
Bundle bundle = new Bundle();
|
122 |
|
|
bundle.putString("error", result );
|
123 |
|
|
dialog.setArguments(bundle);
|
124 |
|
|
dialog.show( mAct.getSupportFragmentManager(), null);
|
125 |
efe00e0f
|
Leszek Koltunski
|
}
|
126 |
|
|
else
|
127 |
5660465b
|
Leszek Koltunski
|
{
|
128 |
|
|
setSolved( mAct, org.distorted.solvers.cube3.Search.numMoves(), result);
|
129 |
|
|
}
|
130 |
efe00e0f
|
Leszek Koltunski
|
}
|
131 |
5660465b
|
Leszek Koltunski
|
}
|
132 |
7289fd6c
|
Leszek Koltunski
|
|
133 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
134 |
|
|
|
135 |
|
|
void leaveState(RubikActivity act)
|
136 |
|
|
{
|
137 |
7f84a768
|
Leszek Koltunski
|
|
138 |
7289fd6c
|
Leszek Koltunski
|
}
|
139 |
|
|
|
140 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
141 |
|
|
|
142 |
|
|
void enterState(final RubikActivity act)
|
143 |
|
|
{
|
144 |
5660465b
|
Leszek Koltunski
|
mSolving = false;
|
145 |
|
|
|
146 |
7289fd6c
|
Leszek Koltunski
|
act.changeObject(RubikObjectList.CUBE,3,null);
|
147 |
473611ee
|
Leszek Koltunski
|
RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
|
148 |
|
|
play.setObjectAndSize(RubikObjectList.CUBE,3);
|
149 |
7289fd6c
|
Leszek Koltunski
|
|
150 |
|
|
DisplayMetrics metrics = act.getResources().getDisplayMetrics();
|
151 |
|
|
final float scale = metrics.density;
|
152 |
|
|
|
153 |
|
|
// TOP ////////////////////////////
|
154 |
|
|
LinearLayout layoutTop = act.findViewById(R.id.upperBar);
|
155 |
|
|
layoutTop.removeAllViews();
|
156 |
|
|
|
157 |
|
|
if( mBitmap ==null ) setupBitmaps(scale);
|
158 |
|
|
if( mColorButton==null ) setupColorButtons(act,scale);
|
159 |
|
|
|
160 |
473611ee
|
Leszek Koltunski
|
markButton(act);
|
161 |
|
|
|
162 |
7289fd6c
|
Leszek Koltunski
|
for(ImageButton button: mColorButton) layoutTop.addView(button);
|
163 |
|
|
|
164 |
|
|
// BOT ////////////////////////////
|
165 |
264af0ad
|
Leszek Koltunski
|
if( mSolveButton==null ) setupSolveButton(act,scale);
|
166 |
|
|
|
167 |
|
|
LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
|
168 |
|
|
layoutLeft.removeAllViews();
|
169 |
|
|
layoutLeft.addView(mSolveButton);
|
170 |
|
|
|
171 |
7289fd6c
|
Leszek Koltunski
|
if( mBackButton==null ) setupBackButton(act,scale);
|
172 |
|
|
|
173 |
|
|
LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
|
174 |
|
|
layoutRight.removeAllViews();
|
175 |
|
|
layoutRight.addView(mBackButton);
|
176 |
|
|
}
|
177 |
|
|
|
178 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
179 |
5660465b
|
Leszek Koltunski
|
|
180 |
7f84a768
|
Leszek Koltunski
|
private void setSolved( final RubikActivity act, final int numMoves, final String moves)
|
181 |
5660465b
|
Leszek Koltunski
|
{
|
182 |
7f84a768
|
Leszek Koltunski
|
act.runOnUiThread(new Runnable()
|
183 |
|
|
{
|
184 |
|
|
@Override
|
185 |
|
|
public void run()
|
186 |
|
|
{
|
187 |
|
|
RubikState.switchState(act,RubikState.SOLU);
|
188 |
|
|
RubikStateSolution solution = (RubikStateSolution) RubikState.SOLU.getStateClass();
|
189 |
|
|
solution.setupMoves(act, numMoves, moves);
|
190 |
|
|
}
|
191 |
|
|
});
|
192 |
5660465b
|
Leszek Koltunski
|
}
|
193 |
|
|
|
194 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
195 |
7289fd6c
|
Leszek Koltunski
|
|
196 |
|
|
private void setupBitmaps(float scale)
|
197 |
|
|
{
|
198 |
|
|
final int SIZE = (int)(scale*BITMAP_SIZE);
|
199 |
|
|
final float R = SIZE*0.10f;
|
200 |
|
|
final float M = SIZE*0.05f;
|
201 |
|
|
|
202 |
|
|
mBitmap = new Bitmap[NUM_FACES];
|
203 |
|
|
|
204 |
|
|
Paint paint = new Paint();
|
205 |
|
|
paint.setColor(0xff008800);
|
206 |
|
|
paint.setStyle(Paint.Style.FILL);
|
207 |
|
|
|
208 |
|
|
paint.setAntiAlias(true);
|
209 |
|
|
paint.setTextAlign(Paint.Align.CENTER);
|
210 |
|
|
paint.setStyle(Paint.Style.FILL);
|
211 |
|
|
|
212 |
|
|
for(int i=0; i<NUM_FACES; i++)
|
213 |
|
|
{
|
214 |
|
|
mBitmap[i] = Bitmap.createBitmap(SIZE, SIZE, Bitmap.Config.ARGB_8888);
|
215 |
|
|
Canvas canvas = new Canvas(mBitmap[i]);
|
216 |
|
|
|
217 |
|
|
paint.setColor(0xff000000);
|
218 |
|
|
canvas.drawRect(0, 0, SIZE, SIZE, paint);
|
219 |
|
|
|
220 |
|
|
paint.setColor(FACE_COLORS[i]);
|
221 |
|
|
canvas.drawRoundRect( M, M, SIZE-M, SIZE-M, R, R, paint);
|
222 |
|
|
}
|
223 |
|
|
}
|
224 |
|
|
|
225 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
226 |
|
|
|
227 |
|
|
private void setupColorButtons(final RubikActivity act, final float scale)
|
228 |
|
|
{
|
229 |
|
|
mColorButton = new ImageButton[NUM_FACES];
|
230 |
|
|
|
231 |
|
|
for(int i=0; i<NUM_FACES; i++)
|
232 |
|
|
{
|
233 |
|
|
final int ii = i;
|
234 |
|
|
int padding = (int)(3*scale + 0.5f);
|
235 |
|
|
LinearLayout.LayoutParams objectParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
|
236 |
|
|
mColorButton[i] = new ImageButton(act);
|
237 |
|
|
mColorButton[i].setLayoutParams(objectParams);
|
238 |
|
|
mColorButton[i].setPadding(padding,0,padding,0);
|
239 |
|
|
mColorButton[i].setImageBitmap(mBitmap[i]);
|
240 |
|
|
|
241 |
|
|
mColorButton[i].setOnClickListener( new View.OnClickListener()
|
242 |
|
|
{
|
243 |
|
|
@Override
|
244 |
|
|
public void onClick(View view)
|
245 |
|
|
{
|
246 |
473611ee
|
Leszek Koltunski
|
mCurrentColor = ii;
|
247 |
|
|
markButton(act);
|
248 |
7289fd6c
|
Leszek Koltunski
|
}
|
249 |
|
|
});
|
250 |
|
|
}
|
251 |
|
|
}
|
252 |
|
|
|
253 |
264af0ad
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
254 |
|
|
|
255 |
|
|
private void setupSolveButton(final RubikActivity act, final float scale)
|
256 |
|
|
{
|
257 |
|
|
int padding = (int)(3*scale + 0.5f);
|
258 |
|
|
LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
|
259 |
|
|
mSolveButton = new Button(act);
|
260 |
|
|
mSolveButton.setLayoutParams(backParams);
|
261 |
|
|
mSolveButton.setPadding(padding,0,padding,0);
|
262 |
|
|
mSolveButton.setText(R.string.solve);
|
263 |
|
|
|
264 |
|
|
mSolveButton.setOnClickListener( new View.OnClickListener()
|
265 |
|
|
{
|
266 |
|
|
@Override
|
267 |
|
|
public void onClick(View v)
|
268 |
|
|
{
|
269 |
5660465b
|
Leszek Koltunski
|
if( !mSolving )
|
270 |
|
|
{
|
271 |
7f84a768
|
Leszek Koltunski
|
String cubeString = retCubeString();
|
272 |
5660465b
|
Leszek Koltunski
|
Solver solver = new Solver(cubeString, act );
|
273 |
|
|
solver.start();
|
274 |
|
|
mSolving = true;
|
275 |
|
|
}
|
276 |
264af0ad
|
Leszek Koltunski
|
}
|
277 |
|
|
});
|
278 |
|
|
}
|
279 |
|
|
|
280 |
7289fd6c
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
281 |
|
|
|
282 |
|
|
private void setupBackButton(final RubikActivity act, final float scale)
|
283 |
|
|
{
|
284 |
|
|
int padding = (int)(3*scale + 0.5f);
|
285 |
|
|
LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
|
286 |
|
|
mBackButton = new Button(act);
|
287 |
|
|
mBackButton.setLayoutParams(backParams);
|
288 |
|
|
mBackButton.setPadding(padding,0,padding,0);
|
289 |
|
|
mBackButton.setText(R.string.back);
|
290 |
|
|
|
291 |
|
|
mBackButton.setOnClickListener( new View.OnClickListener()
|
292 |
|
|
{
|
293 |
|
|
@Override
|
294 |
|
|
public void onClick(View v)
|
295 |
|
|
{
|
296 |
7f84a768
|
Leszek Koltunski
|
RubikPostRender post = act.getPostRender();
|
297 |
|
|
post.resetAllTextureMaps();
|
298 |
7289fd6c
|
Leszek Koltunski
|
RubikState.goBack(act);
|
299 |
|
|
}
|
300 |
|
|
});
|
301 |
|
|
}
|
302 |
|
|
|
303 |
473611ee
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
304 |
|
|
|
305 |
|
|
private void markButton(RubikActivity act)
|
306 |
|
|
{
|
307 |
|
|
for(int b=0; b<NUM_FACES; b++)
|
308 |
|
|
{
|
309 |
|
|
Drawable d = mColorButton[b].getBackground();
|
310 |
|
|
|
311 |
|
|
if( b==mCurrentColor )
|
312 |
|
|
{
|
313 |
|
|
d.setColorFilter(ContextCompat.getColor(act,R.color.red), PorterDuff.Mode.MULTIPLY);
|
314 |
|
|
}
|
315 |
|
|
else
|
316 |
|
|
{
|
317 |
|
|
d.clearColorFilter();
|
318 |
|
|
}
|
319 |
|
|
}
|
320 |
|
|
}
|
321 |
|
|
|
322 |
7f84a768
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
323 |
|
|
// TODO
|
324 |
|
|
|
325 |
4262a13b
|
Leszek Koltunski
|
private String retCubeString()
|
326 |
7f84a768
|
Leszek Koltunski
|
{
|
327 |
4262a13b
|
Leszek Koltunski
|
String ret="UUUUUUUUURRRRRRRRRFFFFFFFFFDDDDDDDDDLLLLLLLLLBBBBBBBBB";
|
328 |
7f84a768
|
Leszek Koltunski
|
/*
|
329 |
|
|
int color;
|
330 |
|
|
int F=retColor(FRONT , 1,1);
|
331 |
|
|
int B=retColor(BACK , 1,1);
|
332 |
|
|
int L=retColor(LEFT , 1,1);
|
333 |
|
|
int R=retColor(RIGHT , 1,1);
|
334 |
|
|
int U=retColor(TOP , 1,1);
|
335 |
|
|
int D=retColor(BOTTOM, 1,1);
|
336 |
|
|
|
337 |
|
|
for(int face in {TOP,RIGHT,FRONT,BOTTOM,LEFT,BACK} )
|
338 |
|
|
for(int row=0; row<mSize; row++)
|
339 |
|
|
for(int col=0; col<mSize; col++)
|
340 |
|
|
{
|
341 |
|
|
color = retColor(TOP,col,row);
|
342 |
|
|
|
343 |
|
|
if(color==F) ret+="F";
|
344 |
|
|
if(color==B) ret+="B";
|
345 |
|
|
if(color==L) ret+="L";
|
346 |
|
|
if(color==R) ret+="R";
|
347 |
|
|
if(color==U) ret+="U";
|
348 |
|
|
if(color==D) ret+="D";
|
349 |
|
|
}
|
350 |
|
|
*/
|
351 |
4262a13b
|
Leszek Koltunski
|
return ret;
|
352 |
|
|
}
|
353 |
7f84a768
|
Leszek Koltunski
|
|
354 |
7289fd6c
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
355 |
|
|
|
356 |
|
|
public void savePreferences(SharedPreferences.Editor editor)
|
357 |
|
|
{
|
358 |
|
|
mColorButton = null;
|
359 |
|
|
mBackButton = null;
|
360 |
811b0446
|
Leszek Koltunski
|
mSolveButton = null;
|
361 |
1f9772f3
|
Leszek Koltunski
|
|
362 |
|
|
editor.putInt("stateSolver_color", mCurrentColor);
|
363 |
7289fd6c
|
Leszek Koltunski
|
}
|
364 |
|
|
|
365 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
366 |
|
|
|
367 |
|
|
public void restorePreferences(SharedPreferences preferences)
|
368 |
|
|
{
|
369 |
1f9772f3
|
Leszek Koltunski
|
mCurrentColor = preferences.getInt("stateSolver_color", 0);
|
370 |
7289fd6c
|
Leszek Koltunski
|
}
|
371 |
473611ee
|
Leszek Koltunski
|
|
372 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
373 |
|
|
|
374 |
|
|
public int getCurrentColor()
|
375 |
|
|
{
|
376 |
|
|
return mCurrentColor;
|
377 |
|
|
}
|
378 |
7289fd6c
|
Leszek Koltunski
|
}
|