Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStateSolver.java @ 473611ee

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.support.v4.content.ContextCompat;
30
import android.util.DisplayMetrics;
31
import android.view.View;
32
import android.widget.Button;
33
import android.widget.ImageButton;
34
import android.widget.LinearLayout;
35

    
36
import org.distorted.main.R;
37
import org.distorted.main.RubikActivity;
38
import org.distorted.main.RubikPostRender;
39
import org.distorted.objects.RubikObjectList;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
public class RubikStateSolver extends RubikStateAbstract
44
  {
45
  private static final int NUM_FACES   =  6;
46
  private static final int BITMAP_SIZE = 35;
47

    
48
  private static final int[] FACE_COLORS = new int[]
49
         {
50
           0xffffff00, 0xffffffff,
51
           0xff0000ff, 0xff00ff00,
52
           0xffff0000, 0xffb5651d
53
         };
54

    
55
  private static Bitmap[] mBitmap;
56
  private ImageButton[] mColorButton;
57
  private Button mBackButton, mSolveButton;
58
  private boolean mSolving;
59
  private int mCurrentColor;
60

    
61
  private class Solver implements Runnable
62
    {
63
    private String mCubeString;
64
    private RubikActivity mAct;
65

    
66
    ///////////////////////////////////////////////////
67

    
68
    Solver(String str, RubikActivity act)
69
   	  {
70
	    mCubeString = str;
71
	    mAct        = act;
72
   	  }
73

    
74
    ///////////////////////////////////////////////////
75

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

    
82
    ///////////////////////////////////////////////////
83

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

    
89
    ///////////////////////////////////////////////////
90

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

    
96
	    if( !org.distorted.solvers.cube3.Search.prepare(res) )
97
	      result= "Error 9";
98
	    else
99
	      result = org.distorted.solvers.cube3.Search.solution(mCubeString, 24, 20);
100

    
101
	    if (result.contains("Error"))
102
	      {
103
	      switch (result.charAt(result.length() - 1))
104
	        {
105
		      case '1': result = res.getString(R.string.error1); break;
106
		      case '2': result = res.getString(R.string.error2); break;
107
		      case '3': result = res.getString(R.string.error3); break;
108
		      case '4': result = res.getString(R.string.error4); break;
109
		      case '5': result = res.getString(R.string.error5); break;
110
		      case '6': result = res.getString(R.string.error6); break;
111
		      case '7': result = res.getString(R.string.error7); break;
112
		      case '8': result = res.getString(R.string.error8); break;
113
		      case '9': result = res.getString(R.string.error9); break;
114
		      }
115

    
116
	      // TODO: pop up an error dialog.
117
	      }
118
	    else
119
        {
120
        setSolved( mAct, org.distorted.solvers.cube3.Search.numMoves(), result);
121
        }
122
	    }
123
    }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
  void leaveState(RubikActivity act)
128
    {
129
    RubikPostRender post = act.getPostRender();
130
    post.resetAllTextureMaps();
131
    }
132

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

    
135
  void enterState(final RubikActivity act)
136
    {
137
    mSolving = false;
138

    
139
    act.changeObject(RubikObjectList.CUBE,3,null);
140
    RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
141
    play.setObjectAndSize(RubikObjectList.CUBE,3);
142

    
143
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
144
    final float scale = metrics.density;
145

    
146
    // TOP ////////////////////////////
147
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
148
    layoutTop.removeAllViews();
149

    
150
    if( mBitmap     ==null ) setupBitmaps(scale);
151
    if( mColorButton==null ) setupColorButtons(act,scale);
152

    
153
    markButton(act);
154

    
155
    for(ImageButton button: mColorButton) layoutTop.addView(button);
156

    
157
    // BOT ////////////////////////////
158
    if( mSolveButton==null ) setupSolveButton(act,scale);
159

    
160
    LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
161
    layoutLeft.removeAllViews();
162
    layoutLeft.addView(mSolveButton);
163

    
164
    if( mBackButton==null ) setupBackButton(act,scale);
165

    
166
    LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
167
    layoutRight.removeAllViews();
168
    layoutRight.addView(mBackButton);
169
    }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172

    
173
  private void setSolved( RubikActivity act, int numMoves, String moves)
174
    {
175
    mSolving = false;
176

    
177
    RubikState.switchState(act,RubikState.SOLU);
178
    }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

    
182
  private void setupBitmaps(float scale)
183
    {
184
    final int SIZE = (int)(scale*BITMAP_SIZE);
185
    final float R = SIZE*0.10f;
186
    final float M = SIZE*0.05f;
187

    
188
    mBitmap = new Bitmap[NUM_FACES];
189

    
190
    Paint paint = new Paint();
191
    paint.setColor(0xff008800);
192
    paint.setStyle(Paint.Style.FILL);
193

    
194
    paint.setAntiAlias(true);
195
    paint.setTextAlign(Paint.Align.CENTER);
196
    paint.setStyle(Paint.Style.FILL);
197

    
198
    for(int i=0; i<NUM_FACES; i++)
199
      {
200
      mBitmap[i] = Bitmap.createBitmap(SIZE, SIZE, Bitmap.Config.ARGB_8888);
201
      Canvas canvas = new Canvas(mBitmap[i]);
202

    
203
      paint.setColor(0xff000000);
204
      canvas.drawRect(0, 0, SIZE, SIZE, paint);
205

    
206
      paint.setColor(FACE_COLORS[i]);
207
      canvas.drawRoundRect( M, M, SIZE-M, SIZE-M, R, R, paint);
208
      }
209
    }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

    
213
  private void setupColorButtons(final RubikActivity act, final float scale)
214
    {
215
    mColorButton = new ImageButton[NUM_FACES];
216

    
217
    for(int i=0; i<NUM_FACES; i++)
218
      {
219
      final int ii = i;
220
      int padding = (int)(3*scale + 0.5f);
221
      LinearLayout.LayoutParams objectParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
222
      mColorButton[i] = new ImageButton(act);
223
      mColorButton[i].setLayoutParams(objectParams);
224
      mColorButton[i].setPadding(padding,0,padding,0);
225
      mColorButton[i].setImageBitmap(mBitmap[i]);
226

    
227
      mColorButton[i].setOnClickListener( new View.OnClickListener()
228
        {
229
        @Override
230
        public void onClick(View view)
231
          {
232
          mCurrentColor = ii;
233
          markButton(act);
234
          }
235
        });
236
      }
237
    }
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

    
241
  private void setupSolveButton(final RubikActivity act, final float scale)
242
    {
243
    int padding = (int)(3*scale + 0.5f);
244
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
245
    mSolveButton = new Button(act);
246
    mSolveButton.setLayoutParams(backParams);
247
    mSolveButton.setPadding(padding,0,padding,0);
248
    mSolveButton.setText(R.string.solve);
249

    
250
    mSolveButton.setOnClickListener( new View.OnClickListener()
251
      {
252
      @Override
253
      public void onClick(View v)
254
        {
255
        if( !mSolving )
256
          {
257
          String cubeString = "INVALID"; // TODO: obtain a valid cube string
258
          Solver solver = new Solver(cubeString, act );
259
          solver.start();
260
          mSolving = true;
261
          }
262
        }
263
      });
264
    }
265

    
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267

    
268
  private void setupBackButton(final RubikActivity act, final float scale)
269
    {
270
    int padding = (int)(3*scale + 0.5f);
271
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
272
    mBackButton = new Button(act);
273
    mBackButton.setLayoutParams(backParams);
274
    mBackButton.setPadding(padding,0,padding,0);
275
    mBackButton.setText(R.string.back);
276

    
277
    mBackButton.setOnClickListener( new View.OnClickListener()
278
      {
279
      @Override
280
      public void onClick(View v)
281
        {
282
        RubikState.goBack(act);
283
        }
284
      });
285
    }
286

    
287
///////////////////////////////////////////////////////////////////////////////////////////////////
288

    
289
  private void markButton(RubikActivity act)
290
    {
291
    for(int b=0; b<NUM_FACES; b++)
292
      {
293
      Drawable d = mColorButton[b].getBackground();
294

    
295
      if( b==mCurrentColor )
296
        {
297
        d.setColorFilter(ContextCompat.getColor(act,R.color.red), PorterDuff.Mode.MULTIPLY);
298
        }
299
      else
300
        {
301
        d.clearColorFilter();
302
        }
303
      }
304
    }
305

    
306
///////////////////////////////////////////////////////////////////////////////////////////////////
307

    
308
  public void savePreferences(SharedPreferences.Editor editor)
309
    {
310
    mColorButton = null;
311
    mBackButton  = null;
312
    mSolveButton = null;
313

    
314
    editor.putInt("stateSolver_color", mCurrentColor);
315
    }
316

    
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318

    
319
  public void restorePreferences(SharedPreferences preferences)
320
    {
321
    mCurrentColor = preferences.getInt("stateSolver_color", 0);
322
    }
323

    
324
///////////////////////////////////////////////////////////////////////////////////////////////////
325

    
326
  public int getCurrentColor()
327
    {
328
    return mCurrentColor;
329
    }
330
  }
(7-7/8)