Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStateSolver.java @ 7f84a768

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.states;
21

    
22
import android.content.SharedPreferences;
23
import android.content.res.Resources;
24
import android.graphics.Bitmap;
25
import android.graphics.Canvas;
26
import android.graphics.Paint;
27
import android.graphics.PorterDuff;
28
import android.graphics.drawable.Drawable;
29
import android.os.Bundle;
30
import android.support.v4.content.ContextCompat;
31
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
import org.distorted.dialogs.RubikDialogSolverError;
38
import org.distorted.main.R;
39
import org.distorted.main.RubikActivity;
40
import org.distorted.main.RubikPostRender;
41
import org.distorted.objects.RubikObjectList;
42

    
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
  private Button mBackButton, mSolveButton;
60
  private boolean mSolving;
61
  private int mCurrentColor;
62

    
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
   	  {
72
	    mCubeString = str;
73
	    mAct        = act;
74
   	  }
75

    
76
    ///////////////////////////////////////////////////
77

    
78
    public void start()
79
      {
80
	    Thread thr = new Thread(this);
81
	    thr.start();
82
      }
83

    
84
    ///////////////////////////////////////////////////
85

    
86
    public void interrupt()
87
      {
88
	    org.distorted.solvers.cube3.Search.interrupt();
89
      }
90

    
91
    ///////////////////////////////////////////////////
92

    
93
    public void run()
94
	    {
95
	    Resources res = mAct.getResources();
96
	    String result;
97

    
98
	    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

    
103
      mSolving = false;
104

    
105
	    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

    
120
        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
	      }
126
	    else
127
        {
128
        setSolved( mAct, org.distorted.solvers.cube3.Search.numMoves(), result);
129
        }
130
	    }
131
    }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
  void leaveState(RubikActivity act)
136
    {
137

    
138
    }
139

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

    
142
  void enterState(final RubikActivity act)
143
    {
144
    mSolving = false;
145

    
146
    act.changeObject(RubikObjectList.CUBE,3,null);
147
    RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
148
    play.setObjectAndSize(RubikObjectList.CUBE,3);
149

    
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
    markButton(act);
161

    
162
    for(ImageButton button: mColorButton) layoutTop.addView(button);
163

    
164
    // BOT ////////////////////////////
165
    if( mSolveButton==null ) setupSolveButton(act,scale);
166

    
167
    LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
168
    layoutLeft.removeAllViews();
169
    layoutLeft.addView(mSolveButton);
170

    
171
    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

    
180
  private void setSolved( final RubikActivity act, final int numMoves, final String moves)
181
    {
182
    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
    }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

    
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
          mCurrentColor = ii;
247
          markButton(act);
248
          }
249
        });
250
      }
251
    }
252

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
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
        if( !mSolving )
270
          {
271
          String cubeString = retCubeString();
272
          Solver solver = new Solver(cubeString, act );
273
          solver.start();
274
          mSolving = true;
275
          }
276
        }
277
      });
278
    }
279

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
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
        RubikPostRender post = act.getPostRender();
297
        post.resetAllTextureMaps();
298
        RubikState.goBack(act);
299
        }
300
      });
301
    }
302

    
303
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
323
// TODO
324

    
325
	private String retCubeString()
326
    {
327
		String ret="UUUUUUUUURRRRRRRRRFFFFFFFFFDDDDDDDDDLLLLLLLLLBBBBBBBBB";
328
/*
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
		return ret;
352
	  }
353

    
354
///////////////////////////////////////////////////////////////////////////////////////////////////
355

    
356
  public void savePreferences(SharedPreferences.Editor editor)
357
    {
358
    mColorButton = null;
359
    mBackButton  = null;
360
    mSolveButton = null;
361

    
362
    editor.putInt("stateSolver_color", mCurrentColor);
363
    }
364

    
365
///////////////////////////////////////////////////////////////////////////////////////////////////
366

    
367
  public void restorePreferences(SharedPreferences preferences)
368
    {
369
    mCurrentColor = preferences.getInt("stateSolver_color", 0);
370
    }
371

    
372
///////////////////////////////////////////////////////////////////////////////////////////////////
373

    
374
  public int getCurrentColor()
375
    {
376
    return mCurrentColor;
377
    }
378
  }
(7-7/8)