Project

General

Profile

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

magiccube / src / main / java / org / distorted / states / RubikStateSolver.java @ ca292407

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
    RubikPostRender post = act.getPostRender();
138
    post.resetAllTextureMaps();
139
    }
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142

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

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

    
151
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
152
    final float scale = metrics.density;
153

    
154
    // TOP ////////////////////////////
155
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
156
    layoutTop.removeAllViews();
157

    
158
    if( mBitmap     ==null ) setupBitmaps(scale);
159
    if( mColorButton==null ) setupColorButtons(act,scale);
160

    
161
    markButton(act);
162

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

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

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

    
172
    if( mBackButton==null ) setupBackButton(act,scale);
173

    
174
    LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
175
    layoutRight.removeAllViews();
176
    layoutRight.addView(mBackButton);
177
    }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180
// TODO
181

    
182
  private void setSolved( RubikActivity act, int numMoves, String moves)
183
    {
184
    RubikState.switchState(act,RubikState.SOLU);
185
    }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

    
189
  private void setupBitmaps(float scale)
190
    {
191
    final int SIZE = (int)(scale*BITMAP_SIZE);
192
    final float R = SIZE*0.10f;
193
    final float M = SIZE*0.05f;
194

    
195
    mBitmap = new Bitmap[NUM_FACES];
196

    
197
    Paint paint = new Paint();
198
    paint.setColor(0xff008800);
199
    paint.setStyle(Paint.Style.FILL);
200

    
201
    paint.setAntiAlias(true);
202
    paint.setTextAlign(Paint.Align.CENTER);
203
    paint.setStyle(Paint.Style.FILL);
204

    
205
    for(int i=0; i<NUM_FACES; i++)
206
      {
207
      mBitmap[i] = Bitmap.createBitmap(SIZE, SIZE, Bitmap.Config.ARGB_8888);
208
      Canvas canvas = new Canvas(mBitmap[i]);
209

    
210
      paint.setColor(0xff000000);
211
      canvas.drawRect(0, 0, SIZE, SIZE, paint);
212

    
213
      paint.setColor(FACE_COLORS[i]);
214
      canvas.drawRoundRect( M, M, SIZE-M, SIZE-M, R, R, paint);
215
      }
216
    }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

    
220
  private void setupColorButtons(final RubikActivity act, final float scale)
221
    {
222
    mColorButton = new ImageButton[NUM_FACES];
223

    
224
    for(int i=0; i<NUM_FACES; i++)
225
      {
226
      final int ii = i;
227
      int padding = (int)(3*scale + 0.5f);
228
      LinearLayout.LayoutParams objectParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
229
      mColorButton[i] = new ImageButton(act);
230
      mColorButton[i].setLayoutParams(objectParams);
231
      mColorButton[i].setPadding(padding,0,padding,0);
232
      mColorButton[i].setImageBitmap(mBitmap[i]);
233

    
234
      mColorButton[i].setOnClickListener( new View.OnClickListener()
235
        {
236
        @Override
237
        public void onClick(View view)
238
          {
239
          mCurrentColor = ii;
240
          markButton(act);
241
          }
242
        });
243
      }
244
    }
245

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

    
248
  private void setupSolveButton(final RubikActivity act, final float scale)
249
    {
250
    int padding = (int)(3*scale + 0.5f);
251
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
252
    mSolveButton = new Button(act);
253
    mSolveButton.setLayoutParams(backParams);
254
    mSolveButton.setPadding(padding,0,padding,0);
255
    mSolveButton.setText(R.string.solve);
256

    
257
    mSolveButton.setOnClickListener( new View.OnClickListener()
258
      {
259
      @Override
260
      public void onClick(View v)
261
        {
262
        if( !mSolving )
263
          {
264
          String cubeString = "INVALID"; // TODO: obtain a valid cube string
265
          Solver solver = new Solver(cubeString, act );
266
          solver.start();
267
          mSolving = true;
268
          }
269
        }
270
      });
271
    }
272

    
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274

    
275
  private void setupBackButton(final RubikActivity act, final float scale)
276
    {
277
    int padding = (int)(3*scale + 0.5f);
278
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
279
    mBackButton = new Button(act);
280
    mBackButton.setLayoutParams(backParams);
281
    mBackButton.setPadding(padding,0,padding,0);
282
    mBackButton.setText(R.string.back);
283

    
284
    mBackButton.setOnClickListener( new View.OnClickListener()
285
      {
286
      @Override
287
      public void onClick(View v)
288
        {
289
        RubikState.goBack(act);
290
        }
291
      });
292
    }
293

    
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295

    
296
  private void markButton(RubikActivity act)
297
    {
298
    for(int b=0; b<NUM_FACES; b++)
299
      {
300
      Drawable d = mColorButton[b].getBackground();
301

    
302
      if( b==mCurrentColor )
303
        {
304
        d.setColorFilter(ContextCompat.getColor(act,R.color.red), PorterDuff.Mode.MULTIPLY);
305
        }
306
      else
307
        {
308
        d.clearColorFilter();
309
        }
310
      }
311
    }
312

    
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314

    
315
  public void savePreferences(SharedPreferences.Editor editor)
316
    {
317
    mColorButton = null;
318
    mBackButton  = null;
319
    mSolveButton = null;
320

    
321
    editor.putInt("stateSolver_color", mCurrentColor);
322
    }
323

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

    
326
  public void restorePreferences(SharedPreferences preferences)
327
    {
328
    mCurrentColor = preferences.getInt("stateSolver_color", 0);
329
    }
330

    
331
///////////////////////////////////////////////////////////////////////////////////////////////////
332

    
333
  public int getCurrentColor()
334
    {
335
    return mCurrentColor;
336
    }
337
  }
(7-7/8)