Project

General

Profile

« Previous | Next » 

Revision 5660465b

Added by Leszek Koltunski about 4 years ago

More support for the 3x3x3 Solver: more of the actual 3x3x3 solver mechanism.

View differences:

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

  
22 22
import android.content.SharedPreferences;
23
import android.content.res.Resources;
23 24
import android.graphics.Bitmap;
24 25
import android.graphics.Canvas;
25 26
import android.graphics.Paint;
......
50 51
  private static Bitmap[] mBitmap;
51 52
  private ImageButton[] mColorButton;
52 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
    }
53 119

  
54 120
///////////////////////////////////////////////////////////////////////////////////////////////////
55 121

  
......
62 128

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

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

  
67 135
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
......
91 159
    }
92 160

  
93 161
///////////////////////////////////////////////////////////////////////////////////////////////////
94
//createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top, int side);
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
///////////////////////////////////////////////////////////////////////////////////////////////////
95 171

  
96 172
  private void setupBitmaps(float scale)
97 173
    {
......
165 241
      @Override
166 242
      public void onClick(View v)
167 243
        {
168
        RubikState.switchState(act,RubikState.SOLU);
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
          }
169 251
        }
170 252
      });
171 253
    }

Also available in: Unified diff