Project

General

Profile

« Previous | Next » 

Revision f0336037

Added by Leszek Koltunski about 4 years ago

Make the Solver state more abstract.

View differences:

src/main/java/org/distorted/states/RubikStateSolver.java
20 20
package org.distorted.states;
21 21

  
22 22
import android.content.SharedPreferences;
23
import android.content.res.Resources;
24 23
import android.graphics.Bitmap;
25 24
import android.graphics.Canvas;
26 25
import android.graphics.Paint;
......
38 37
import org.distorted.main.R;
39 38
import org.distorted.main.RubikActivity;
40 39
import org.distorted.main.RubikPostRender;
40
import org.distorted.objects.RubikObject;
41 41
import org.distorted.objects.RubikObjectList;
42
import org.distorted.solvers.Solver;
42 43

  
43 44
///////////////////////////////////////////////////////////////////////////////////////////////////
44 45

  
......
60 61
  private boolean mSolving;
61 62
  private int mCurrentColor;
62 63

  
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 64
///////////////////////////////////////////////////////////////////////////////////////////////////
134 65

  
135 66
  void leaveState(RubikActivity act)
......
175 106
    layoutRight.addView(mBackButton);
176 107
    }
177 108

  
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 109
///////////////////////////////////////////////////////////////////////////////////////////////////
195 110

  
196 111
  private void setupBitmaps(float scale)
......
268 183
        {
269 184
        if( !mSolving )
270 185
          {
271
          String cubeString = retCubeString();
272
          Solver solver = new Solver(cubeString, act );
273
          solver.start();
274 186
          mSolving = true;
187
          RubikObject object = act.getObject();
188
          String objectString = object.retObjectString();
189
          Solver solver = new Solver( act, objectString );
190
          solver.start();
275 191
          }
276 192
        }
277 193
      });
......
319 235
      }
320 236
    }
321 237

  
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 238
///////////////////////////////////////////////////////////////////////////////////////////////////
355 239

  
356 240
  public void savePreferences(SharedPreferences.Editor editor)
......
375 259
    {
376 260
    return mCurrentColor;
377 261
    }
262

  
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

  
265
  public void setSolved( final RubikActivity act, final int numMoves, final String moves)
266
    {
267
    mSolving = false;
268

  
269
    act.runOnUiThread(new Runnable()
270
      {
271
      @Override
272
      public void run()
273
        {
274
        RubikState.switchState(act,RubikState.SOLU);
275
        RubikStateSolution solution = (RubikStateSolution) RubikState.SOLU.getStateClass();
276
        solution.setupMoves(act, numMoves, moves);
277
        }
278
      });
279
    }
280

  
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282

  
283
  public void displayErrorDialog( final RubikActivity act, String message)
284
    {
285
    mSolving = false;
286

  
287
    RubikDialogSolverError dialog = new RubikDialogSolverError();
288
    Bundle bundle = new Bundle();
289
    bundle.putString("error", message );
290
    dialog.setArguments(bundle);
291
    dialog.show( act.getSupportFragmentManager(), null);
292
    }
378 293
  }

Also available in: Unified diff