Project

General

Profile

« Previous | Next » 

Revision f0336037

Added by Leszek Koltunski over 4 years ago

Make the Solver state more abstract.

View differences:

src/main/java/org/distorted/objects/RubikCube.java
225 225
    {
226 226
    return 1.0f;
227 227
    }
228

  
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

  
231
  public String retString()
232
    {
233
    String ret="UUUUUUUUURRRRRRRRRFFFFFFFFFDDDDDDDDDLLLLLLLLLBBBBBBBBB";
234
/*
235
		int color;
236
		int F=retColor(FRONT , 1,1);
237
		int B=retColor(BACK  , 1,1);
238
		int L=retColor(LEFT  , 1,1);
239
		int R=retColor(RIGHT , 1,1);
240
		int U=retColor(TOP   , 1,1);
241
		int D=retColor(BOTTOM, 1,1);
242

  
243
		for(int face in {TOP,RIGHT,FRONT,BOTTOM,LEFT,BACK} )
244
		  for(int row=0; row<mSize; row++)
245
			  for(int col=0; col<mSize; col++)
246
			    {
247
				  color = retColor(TOP,col,row);
248

  
249
				  if(color==F) ret+="F";
250
				  if(color==B) ret+="B";
251
				  if(color==L) ret+="L";
252
				  if(color==R) ret+="R";
253
				  if(color==U) ret+="U";
254
				  if(color==D) ret+="D";
255
			    }
256
*/
257
    return ret;
258
    }
228 259
}
src/main/java/org/distorted/objects/RubikObject.java
567 567
    return mList;
568 568
    }
569 569

  
570
///////////////////////////////////////////////////////////////////////////////////////////////////
571

  
572
  public String retObjectString()
573
    {
574
    return retString();
575
    }
576

  
570 577
///////////////////////////////////////////////////////////////////////////////////////////////////
571 578

  
572 579
  abstract float getScreenRatio();
......
574 581
  abstract Static3D[] getCubitPositions(int size);
575 582
  abstract float[] getLegalQuats();
576 583
  abstract int getNumFaces();
584
  abstract String retString();
577 585
  abstract MeshBase createCubitMesh(int cubit, int vertices);
578 586
  abstract void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top, int side);
579 587
  public abstract Static3D[] getRotationAxis();
src/main/java/org/distorted/objects/RubikPyraminx.java
319 319

  
320 320
    return ((float)size)/(size-row);
321 321
    }
322

  
323
///////////////////////////////////////////////////////////////////////////////////////////////////
324
// TODO
325

  
326
  public String retString()
327
    {
328
    return "";
329
    }
322 330
}
src/main/java/org/distorted/solvers/Solver.java
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.solvers;
21

  
22
import android.content.res.Resources;
23

  
24
import org.distorted.main.R;
25
import org.distorted.main.RubikActivity;
26
import org.distorted.states.RubikState;
27
import org.distorted.states.RubikStateSolver;
28

  
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30

  
31
public class Solver implements Runnable
32
{
33
  private String mObjectPosition;
34
  private RubikActivity mAct;
35

  
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

  
38
  public Solver(RubikActivity act, String position )
39
    {
40
    mObjectPosition = position;
41
    mAct            = act;
42
    }
43

  
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

  
46
  public void start()
47
    {
48
    Thread thr = new Thread(this);
49
    thr.start();
50
    }
51

  
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

  
54
  public void interrupt()
55
    {
56
    org.distorted.solvers.cube3.Search.interrupt();
57
    }
58

  
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

  
61
  public void run()
62
    {
63
    Resources res = mAct.getResources();
64
    RubikStateSolver solver = (RubikStateSolver) RubikState.SVER.getStateClass();
65
    String result;
66

  
67
    if( !org.distorted.solvers.cube3.Search.prepare(res) )
68
      result= "Error 9";
69
    else
70
      result = org.distorted.solvers.cube3.Search.solution(mObjectPosition, 24, 20);
71

  
72
    if (result.contains("Error"))
73
      {
74
      switch (result.charAt(result.length() - 1))
75
        {
76
        case '1': result = res.getString(R.string.error1); break;
77
        case '2': result = res.getString(R.string.error2); break;
78
        case '3': result = res.getString(R.string.error3); break;
79
        case '4': result = res.getString(R.string.error4); break;
80
        case '5': result = res.getString(R.string.error5); break;
81
        case '6': result = res.getString(R.string.error6); break;
82
        case '7': result = res.getString(R.string.error7); break;
83
        case '8': result = res.getString(R.string.error8); break;
84
        case '9': result = res.getString(R.string.error9); break;
85
        }
86

  
87
      solver.displayErrorDialog(mAct,result);
88
      }
89
    else
90
      {
91
      solver.setSolved( mAct, org.distorted.solvers.cube3.Search.numMoves(), result);
92
      }
93
    }
94
}  
95

  
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