Project

General

Profile

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

magiccube / src / main / java / org / distorted / uistate / RubikStateSolver.java @ 5660465b

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.uistate;
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.util.DisplayMetrics;
28
import android.view.View;
29
import android.widget.Button;
30
import android.widget.ImageButton;
31
import android.widget.LinearLayout;
32

    
33
import org.distorted.magic.R;
34
import org.distorted.magic.RubikActivity;
35
import org.distorted.object.RubikObjectList;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
public class RubikStateSolver extends RubikStateAbstract
40
  {
41
  private static final int NUM_FACES   =  6;
42
  private static final int BITMAP_SIZE = 35;
43

    
44
  private static final int[] FACE_COLORS = new int[]
45
         {
46
           0xffffff00, 0xffffffff,
47
           0xff0000ff, 0xff00ff00,
48
           0xffff0000, 0xffb5651d
49
         };
50

    
51
  private static Bitmap[] mBitmap;
52
  private ImageButton[] mColorButton;
53
  private Button mBackButton, mSolveButton;
54
  private boolean mSolving;
55

    
56
  private class Solver implements Runnable
57
    {
58
    private String mCubeString;
59
    private RubikActivity mAct;
60

    
61
    ///////////////////////////////////////////////////
62

    
63
    Solver(String str, RubikActivity act)
64
   	  {
65
	    mCubeString = str;
66
	    mAct        = act;
67
   	  }
68

    
69
    ///////////////////////////////////////////////////
70

    
71
    public void start()
72
      {
73
	    Thread thr = new Thread(this);
74
	    thr.start();
75
      }
76

    
77
    ///////////////////////////////////////////////////
78

    
79
    public void interrupt()
80
      {
81
	    org.distorted.solvers.cube3.Search.interrupt();
82
      }
83

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

    
86
    public void run()
87
	    {
88
	    Resources res = mAct.getResources();
89
	    String result;
90

    
91
	    if( !org.distorted.solvers.cube3.Search.prepare(res) )
92
	      result= "Error 9";
93
	    else
94
	      result = org.distorted.solvers.cube3.Search.solution(mCubeString, 24, 20);
95

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

    
111
	      // TODO: pop up an error dialog.
112
	      }
113
	    else
114
        {
115
        setSolved( mAct, org.distorted.solvers.cube3.Search.numMoves(), result);
116
        }
117
	    }
118
    }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

    
122
  void leaveState(RubikActivity act)
123
    {
124

    
125
    }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

    
129
  void enterState(final RubikActivity act)
130
    {
131
    mSolving = false;
132

    
133
    act.changeObject(RubikObjectList.CUBE,3,null);
134

    
135
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
136
    final float scale = metrics.density;
137

    
138
    // TOP ////////////////////////////
139
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
140
    layoutTop.removeAllViews();
141

    
142
    if( mBitmap     ==null ) setupBitmaps(scale);
143
    if( mColorButton==null ) setupColorButtons(act,scale);
144

    
145
    for(ImageButton button: mColorButton) layoutTop.addView(button);
146

    
147
    // BOT ////////////////////////////
148
    if( mSolveButton==null ) setupSolveButton(act,scale);
149

    
150
    LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
151
    layoutLeft.removeAllViews();
152
    layoutLeft.addView(mSolveButton);
153

    
154
    if( mBackButton==null ) setupBackButton(act,scale);
155

    
156
    LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
157
    layoutRight.removeAllViews();
158
    layoutRight.addView(mBackButton);
159
    }
160

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

    
163
  private void setSolved( RubikActivity act, int numMoves, String moves)
164
    {
165
    mSolving = false;
166

    
167
    RubikState.switchState(act,RubikState.SOLU);
168
    }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

    
172
  private void setupBitmaps(float scale)
173
    {
174
    final int SIZE = (int)(scale*BITMAP_SIZE);
175
    final float R = SIZE*0.10f;
176
    final float M = SIZE*0.05f;
177

    
178
    mBitmap = new Bitmap[NUM_FACES];
179

    
180
    Paint paint = new Paint();
181
    paint.setColor(0xff008800);
182
    paint.setStyle(Paint.Style.FILL);
183

    
184
    paint.setAntiAlias(true);
185
    paint.setTextAlign(Paint.Align.CENTER);
186
    paint.setStyle(Paint.Style.FILL);
187

    
188
    for(int i=0; i<NUM_FACES; i++)
189
      {
190
      mBitmap[i] = Bitmap.createBitmap(SIZE, SIZE, Bitmap.Config.ARGB_8888);
191
      Canvas canvas = new Canvas(mBitmap[i]);
192

    
193
      paint.setColor(0xff000000);
194
      canvas.drawRect(0, 0, SIZE, SIZE, paint);
195

    
196
      paint.setColor(FACE_COLORS[i]);
197
      canvas.drawRoundRect( M, M, SIZE-M, SIZE-M, R, R, paint);
198
      }
199
    }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
  private void setupColorButtons(final RubikActivity act, final float scale)
204
    {
205
    mColorButton = new ImageButton[NUM_FACES];
206

    
207
    for(int i=0; i<NUM_FACES; i++)
208
      {
209
      final int ii = i;
210
      int padding = (int)(3*scale + 0.5f);
211
      LinearLayout.LayoutParams objectParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
212
      mColorButton[i] = new ImageButton(act);
213
      mColorButton[i].setLayoutParams(objectParams);
214
      mColorButton[i].setPadding(padding,0,padding,0);
215
      mColorButton[i].setImageBitmap(mBitmap[i]);
216

    
217
      mColorButton[i].setOnClickListener( new View.OnClickListener()
218
        {
219
        @Override
220
        public void onClick(View view)
221
          {
222
          android.util.Log.e("solver", "button "+FACE_COLORS[ii]+" clicked");
223
          }
224
        });
225
      }
226
    }
227

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

    
230
  private void setupSolveButton(final RubikActivity act, final float scale)
231
    {
232
    int padding = (int)(3*scale + 0.5f);
233
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
234
    mSolveButton = new Button(act);
235
    mSolveButton.setLayoutParams(backParams);
236
    mSolveButton.setPadding(padding,0,padding,0);
237
    mSolveButton.setText(R.string.solve);
238

    
239
    mSolveButton.setOnClickListener( new View.OnClickListener()
240
      {
241
      @Override
242
      public void onClick(View v)
243
        {
244
        if( !mSolving )
245
          {
246
          String cubeString = "INVALID"; // TODO: obtain a valid cube string
247
          Solver solver = new Solver(cubeString, act );
248
          solver.start();
249
          mSolving = true;
250
          }
251
        }
252
      });
253
    }
254

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

    
257
  private void setupBackButton(final RubikActivity act, final float scale)
258
    {
259
    int padding = (int)(3*scale + 0.5f);
260
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
261
    mBackButton = new Button(act);
262
    mBackButton.setLayoutParams(backParams);
263
    mBackButton.setPadding(padding,0,padding,0);
264
    mBackButton.setText(R.string.back);
265

    
266
    mBackButton.setOnClickListener( new View.OnClickListener()
267
      {
268
      @Override
269
      public void onClick(View v)
270
        {
271
        RubikState.goBack(act);
272
        }
273
      });
274
    }
275

    
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277

    
278
  public void savePreferences(SharedPreferences.Editor editor)
279
    {
280
    mColorButton = null;
281
    mBackButton  = null;
282
    mSolveButton = null;
283
    }
284

    
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286

    
287
  public void restorePreferences(SharedPreferences preferences)
288
    {
289

    
290
    }
291
  }
(7-7/8)