Project

General

Profile

« Previous | Next » 

Revision 7f84a768

Added by Leszek Koltunski about 4 years ago

Progress with the 3x3x3 Solver.

Hopefully the only thing that remains to be done is to implement the RubikStateSolver.retCubeString().

View differences:

src/main/java/org/distorted/patterns/RubikPattern.java
30 30

  
31 31
public class RubikPattern
32 32
{
33
  private static final int DURATION_MILLIS = 800;
33
  private static final int DURATION_MILLIS = 750;
34 34

  
35 35
  private int[] numCategories   = new int[NUM_OBJECTS];
36 36
  private int[] currentCategory = new int[NUM_OBJECTS];
......
217 217
      numMove = moveStr.length()/4;
218 218
      moves   = new int[numMove][3];
219 219
      curMove = numMove;
220

  
221
      int digit0, digit1, digit2;
222

  
223
      for(int i=0; i<numMove; i++)
224
        {
225
        digit0 = moveStr.charAt(4*i+1)-'0';
226
        digit1 = moveStr.charAt(4*i+2)-'0';
227
        digit2 = moveStr.charAt(4*i+3)-'0';
228

  
229
        moves[i][0] = (10*digit0+digit1)/32;
230
        moves[i][1] = (10*digit0+digit1)%32;
231
        moves[i][2] = 2-digit2;
232
        }
233

  
220
      parseMoves(moves,numMove,moveStr);
234 221
      moveStr = null;
235 222
      mInitialized = true;
236 223
      }
......
266 253
      {
267 254
      if( !mInitialized ) initialize();
268 255

  
269
      curMove++;
270

  
271 256
      if( mCanRotate )
272 257
        {
258
        curMove++;
259

  
273 260
        if( curMove>numMove )
274 261
          {
275 262
          curMove= 0;
......
297 284
      else
298 285
        {
299 286
        android.util.Log.e("pattern", "failed to make Move!");
300
        curMove--;
301 287
        }
302 288
      }
303 289

  
......
307 293
      {
308 294
      if( !mInitialized ) initialize();
309 295

  
310
      curMove--;
311

  
312 296
      if( mCanRotate )
313 297
        {
298
        curMove--;
299

  
314 300
        if( curMove<0 )
315 301
          {
316 302
          curMove=numMove;
......
338 324
      else
339 325
        {
340 326
        android.util.Log.e("pattern", "failed to back Move!");
341
        curMove++;
342 327
        }
343 328
      }
344 329

  
......
405 390
    return mThis;
406 391
    }
407 392

  
393
///////////////////////////////////////////////////////////////////////////////////////////////////
394

  
395
  public static void parseMoves(int[][] result, int numMoves, String moves)
396
    {
397
    int digit0, digit1, digit2;
398

  
399
    for(int i=0; i<numMoves; i++)
400
      {
401
      digit0 = moves.charAt(4*i+1)-'0';
402
      digit1 = moves.charAt(4*i+2)-'0';
403
      digit2 = moves.charAt(4*i+3)-'0';
404

  
405
      result[i][0] = (10*digit0+digit1)/32;
406
      result[i][1] = (10*digit0+digit1)%32;
407
      result[i][2] = 2-digit2;
408
      }
409
    }
410

  
408 411
///////////////////////////////////////////////////////////////////////////////////////////////////
409 412

  
410 413
  public int getNumCategories(int tab)
src/main/java/org/distorted/solvers/cube3/Search.java
159 159
	 *@param timeOut
160 160
	 *          defines the maximum computing time of the method in seconds. If it does not return with a solution, it returns with
161 161
	 *          an error code.
162
	 * 
163
	 * @param useSeparator
164
	 *          determines if a " . " separates the phase1 and phase2 parts of the solver string like in F' R B R L2 F .
165
	 *          U2 U D for example.<br>
166 162
	 * @return The solution string or an error code:<br>
167 163
	 *         Error 1: There is not exactly one facelet of each colour<br>
168 164
	 *         Error 2: Not all 12 edges exist exactly once<br>
......
177 173
	    {
178 174
		int s;
179 175

  
180
		Log.d("solution", facelets);
181
		
182 176
		mInterrupted = false;
183 177
		
184 178
		// +++++++++++++++++++++check for wrong input +++++++++++++++++++++++++++++
src/main/java/org/distorted/states/RubikStateSolution.java
31 31

  
32 32
import org.distorted.main.R;
33 33
import org.distorted.main.RubikActivity;
34
import org.distorted.main.RubikPostRender;
34 35
import org.distorted.objects.RubikObject;
36
import org.distorted.patterns.RubikPattern;
35 37

  
36 38
///////////////////////////////////////////////////////////////////////////////////////////////////
37 39

  
38
public class RubikStateSolution extends RubikStateAbstract
40
public class RubikStateSolution extends RubikStateAbstract implements RubikPostRender.ActionFinishedListener
39 41
  {
42
  private static final int DURATION_MILLIS = 750;
43

  
40 44
  private Button mBackButton;
41 45
  private ImageButton mPrevButton, mNextButton;
42 46
  private TextView mMovesText;
47
  private int[][] mMoves;
48
  private int mCurrMove, mNumMoves;
49
  private boolean mCanRotate;
43 50

  
44 51
///////////////////////////////////////////////////////////////////////////////////////////////////
45 52

  
......
99 106
      @Override
100 107
      public void onClick(View v)
101 108
        {
102

  
109
        RubikPostRender post = act.getPostRender();
110
        backMove(post);
111
        mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
103 112
        }
104 113
      });
105 114
    }
......
120 129
      @Override
121 130
      public void onClick(View v)
122 131
        {
123

  
132
        RubikPostRender post = act.getPostRender();
133
        makeMove(post);
134
        mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
124 135
        }
125 136
      });
126 137
    }
......
137 148
    mMovesText.setLayoutParams(params);
138 149
    mMovesText.setPadding(padding,0,padding,0);
139 150
    mMovesText.setGravity(Gravity.CENTER);
140
    mMovesText.setText(act.getString(R.string.mo_placeholder,0,0));
151
    mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
141 152
    }
142 153

  
143 154
///////////////////////////////////////////////////////////////////////////////////////////////////
......
161 172
      });
162 173
    }
163 174

  
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

  
177
  private void makeMove(RubikPostRender post)
178
    {
179
    if( mCanRotate )
180
      {
181
      mCurrMove++;
182

  
183
      if( mCurrMove>mNumMoves )
184
        {
185
        mCurrMove= 0;
186
        post.initializeObject(null);
187
        }
188
      else
189
        {
190
        int axis     = mMoves[mCurrMove-1][0];
191
		    int rowBitmap= mMoves[mCurrMove-1][1];
192
		    int bareAngle= mMoves[mCurrMove-1][2];
193
        int angle    = bareAngle*(360/post.getObject().getBasicAngle());
194
        int numRot   = Math.abs(bareAngle);
195

  
196
        if( angle!=0 )
197
          {
198
          mCanRotate = false;
199
          post.addRotation(this, axis, rowBitmap, angle, numRot*DURATION_MILLIS);
200
          }
201
        else
202
          {
203
          android.util.Log.e("solution", "error: solution contains angle 0");
204
          }
205
        }
206
      }
207
    else
208
      {
209
      android.util.Log.e("solution", "failed to make move!");
210
      }
211
    }
212

  
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

  
215
  private void backMove(RubikPostRender post)
216
    {
217
    if( mCanRotate )
218
      {
219
      mCurrMove--;
220

  
221
      if( mCurrMove<0 )
222
        {
223
        mCurrMove=mNumMoves;
224
        post.initializeObject(mMoves);
225
        }
226
      else
227
        {
228
        int axis     = mMoves[mCurrMove][0];
229
		    int rowBitmap= mMoves[mCurrMove][1];
230
		    int bareAngle= mMoves[mCurrMove][2];
231
        int angle    = bareAngle*(360/post.getObject().getBasicAngle());
232
        int numRot   = Math.abs(bareAngle);
233

  
234
        if( angle!=0 )
235
          {
236
          mCanRotate = false;
237
          post.addRotation(this, axis, rowBitmap, -angle, numRot*DURATION_MILLIS);
238
          }
239
        else
240
          {
241
          android.util.Log.e("solution", "error: solution contains angle 0");
242
          }
243
        }
244
      }
245
    else
246
      {
247
      android.util.Log.e("solution", "failed to back move!");
248
      }
249
    }
250

  
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252

  
253
  void setupMoves(final RubikActivity act, int numMoves, String moves)
254
    {
255
    android.util.Log.e("solution", "got "+numMoves+" moves: "+moves);
256

  
257
    mCanRotate= true;
258
    mCurrMove = 0;
259
    mNumMoves = numMoves;
260
    mMoves    = new int[mNumMoves][3];
261

  
262
    RubikPattern.parseMoves(mMoves,numMoves,moves);
263

  
264
    mMovesText.setText(act.getString(R.string.mo_placeholder,mCurrMove,mNumMoves));
265
    }
266

  
164 267
///////////////////////////////////////////////////////////////////////////////////////////////////
165 268

  
166 269
  public void savePreferences(SharedPreferences.Editor editor)
......
177 280
    {
178 281

  
179 282
    }
283

  
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

  
286
  public void onActionFinished(final long effectID)
287
    {
288
    mCanRotate = true;
289
    }
180 290
  }
src/main/java/org/distorted/states/RubikStateSolver.java
134 134

  
135 135
  void leaveState(RubikActivity act)
136 136
    {
137
    RubikPostRender post = act.getPostRender();
138
    post.resetAllTextureMaps();
137

  
139 138
    }
140 139

  
141 140
///////////////////////////////////////////////////////////////////////////////////////////////////
......
177 176
    }
178 177

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

  
182
  private void setSolved( RubikActivity act, int numMoves, String moves)
180
  private void setSolved( final RubikActivity act, final int numMoves, final String moves)
183 181
    {
184
    RubikState.switchState(act,RubikState.SOLU);
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
      });
185 192
    }
186 193

  
187 194
///////////////////////////////////////////////////////////////////////////////////////////////////
......
261 268
        {
262 269
        if( !mSolving )
263 270
          {
264
          String cubeString = "INVALID"; // TODO: obtain a valid cube string
271
          String cubeString = retCubeString();
265 272
          Solver solver = new Solver(cubeString, act );
266 273
          solver.start();
267 274
          mSolving = true;
......
286 293
      @Override
287 294
      public void onClick(View v)
288 295
        {
296
        RubikPostRender post = act.getPostRender();
297
        post.resetAllTextureMaps();
289 298
        RubikState.goBack(act);
290 299
        }
291 300
      });
......
310 319
      }
311 320
    }
312 321

  
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

  
313 354
///////////////////////////////////////////////////////////////////////////////////////////////////
314 355

  
315 356
  public void savePreferences(SharedPreferences.Editor editor)

Also available in: Unified diff