Revision 222938b7
Added by Leszek Koltunski 12 months ago
| src/main/java/org/distorted/phasedsolver/SolverActivity.java | ||
|---|---|---|
| 31 | 31 |
|
| 32 | 32 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 33 | 33 |
|
| 34 |
public class SolverActivity extends Activity |
|
| 34 |
public class SolverActivity extends Activity implements SolutionListener
|
|
| 35 | 35 |
{
|
| 36 | 36 |
private InitAssets mAsset; |
| 37 | 37 |
private SharedPreferences mPreferences; |
| ... | ... | |
| 41 | 41 |
private String mMoves; |
| 42 | 42 |
private int mNumMoves; |
| 43 | 43 |
private Solver3x3Beginner mBeginner; |
| 44 |
private long mTime; |
|
| 44 | 45 |
|
| 45 | 46 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 46 | 47 |
|
| ... | ... | |
| 146 | 147 |
|
| 147 | 148 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 148 | 149 |
|
| 149 |
private void solve(int[] quats)
|
|
| 150 |
public void receiveSolution(int[] solution, int phase)
|
|
| 150 | 151 |
{
|
| 151 |
int[][] sol = mBeginner.solution(quats); |
|
| 152 |
int numPhases = sol.length; |
|
| 153 |
|
|
| 154 |
for(int p=0; p<numPhases; p++) |
|
| 152 |
if( solution==null ) |
|
| 155 | 153 |
{
|
| 156 |
int numMoves = sol[p]==null ? -1 : sol[p][0]; |
|
| 157 |
|
|
| 158 |
if( numMoves<0 ) |
|
| 159 |
{
|
|
| 160 |
String message = "Phase "+p+": FAIL"; |
|
| 161 |
Toast.makeText(this,message,Toast.LENGTH_LONG).show(); |
|
| 162 |
break; |
|
| 163 |
} |
|
| 154 |
String message = "Phase "+phase+": FAIL"; |
|
| 155 |
Toast.makeText(this,message,Toast.LENGTH_LONG).show(); |
|
| 164 | 156 |
} |
| 165 |
|
|
| 166 |
int[][][] moves = new int[numPhases][][]; |
|
| 167 |
|
|
| 168 |
for( int p=0; p<numPhases; p++ ) |
|
| 157 |
else |
|
| 169 | 158 |
{
|
| 170 |
int numMoves = sol[p][0];
|
|
| 171 |
moves[p] = new int[numMoves][];
|
|
| 159 |
int numMoves = solution[0];
|
|
| 160 |
int[][] moves = new int[numMoves][];
|
|
| 172 | 161 |
int index = 0; |
| 173 | 162 |
|
| 174 | 163 |
for(int m=1; m<=numMoves; m++) |
| 175 |
{
|
|
| 176 |
int mi = sol[p][m]; |
|
| 177 |
moves[p][index++] = mObject.findMove(mi); |
|
| 178 |
} |
|
| 164 |
moves[index++] = mObject.findMove(solution[m]); |
|
| 165 |
|
|
| 166 |
mLowerPane.setMoves(this,moves,phase); |
|
| 179 | 167 |
} |
| 180 | 168 |
|
| 181 |
mLowerPane.setMoves(this,moves); |
|
| 169 |
long time = System.currentTimeMillis(); |
|
| 170 |
long diff = time - mTime; |
|
| 171 |
mTime = time; |
|
| 172 |
|
|
| 173 |
System.out.println("Phase "+phase+" solved in "+diff);
|
|
| 182 | 174 |
} |
| 183 | 175 |
|
| 184 | 176 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 185 | 177 |
|
| 186 |
public void Solve(View v) |
|
| 178 |
public void Solve(View v) |
|
| 179 |
{
|
|
| 180 |
TwistyObject object = getControl().getObject(); |
|
| 181 |
int numCubits = 26; |
|
| 182 |
int[] qi = new int[numCubits]; |
|
| 183 |
for(int i=0; i<numCubits; i++) qi[i] = object.getCubitQuatIndex(i); |
|
| 184 |
final SolutionListener listener = this; |
|
| 185 |
mTime = System.currentTimeMillis(); |
|
| 186 |
|
|
| 187 |
runOnUiThread(new Runnable() |
|
| 187 | 188 |
{
|
| 188 |
TwistyObject object = getControl().getObject(); |
|
| 189 |
int numCubits = 26; |
|
| 190 |
int[] qi = new int[numCubits]; |
|
| 191 |
for(int i=0; i<numCubits; i++) qi[i] = object.getCubitQuatIndex(i); |
|
| 192 |
|
|
| 193 |
runOnUiThread(new Runnable() |
|
| 189 |
@Override |
|
| 190 |
public void run() |
|
| 194 | 191 |
{
|
| 195 |
@Override |
|
| 196 |
public void run() |
|
| 197 |
{
|
|
| 198 |
solve(qi); |
|
| 199 |
} |
|
| 200 |
}); |
|
| 201 |
} |
|
| 192 |
mBeginner.solution(listener, qi); |
|
| 193 |
} |
|
| 194 |
}); |
|
| 195 |
} |
|
| 202 | 196 |
|
| 203 | 197 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 204 | 198 |
|
| 205 |
public void Reset(View v)
|
|
| 206 |
{
|
|
| 207 |
ObjectControl control = getControl();
|
|
| 208 |
control.solveObject();
|
|
| 209 |
mLowerPane.clearMoves(this);
|
|
| 199 |
public void Reset(View v) |
|
| 200 |
{
|
|
| 201 |
ObjectControl control = getControl(); |
|
| 202 |
control.solveObject(); |
|
| 203 |
mLowerPane.clearMoves(this); |
|
| 210 | 204 |
|
| 211 |
mNumMoves = 0;
|
|
| 212 |
mMoves = "";
|
|
| 213 |
mText.setText(mMoves);
|
|
| 214 |
}
|
|
| 205 |
mNumMoves = 0; |
|
| 206 |
mMoves = ""; |
|
| 207 |
mText.setText(mMoves); |
|
| 208 |
} |
|
| 215 | 209 |
|
| 216 | 210 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 217 | 211 |
|
| 218 |
void addMove(int axis, int row, int angle) |
|
| 219 |
{
|
|
| 220 |
int move = mObject.findMove(axis,row,angle); |
|
| 221 |
mMoves += (" "+move);
|
|
| 222 |
mNumMoves++; |
|
| 223 |
|
|
| 224 |
if( mNumMoves>10 ) |
|
| 225 |
{
|
|
| 226 |
int space = mMoves.indexOf(' ');
|
|
| 227 |
mMoves = mMoves.substring(space+1); |
|
| 228 |
mNumMoves--; |
|
| 229 |
} |
|
| 212 |
void addMove(int axis, int row, int angle) |
|
| 213 |
{
|
|
| 214 |
int move = mObject.findMove(axis,row,angle); |
|
| 215 |
mMoves += (" "+move);
|
|
| 216 |
mNumMoves++; |
|
| 230 | 217 |
|
| 231 |
mText.setText(mMoves); |
|
| 218 |
if( mNumMoves>10 ) |
|
| 219 |
{
|
|
| 220 |
int space = mMoves.indexOf(' ');
|
|
| 221 |
mMoves = mMoves.substring(space+1); |
|
| 222 |
mNumMoves--; |
|
| 232 | 223 |
} |
| 224 |
|
|
| 225 |
mText.setText(mMoves); |
|
| 226 |
} |
|
| 233 | 227 |
} |
| src/main/java/org/distorted/phasedsolver/SolverLowerPane.java | ||
|---|---|---|
| 24 | 24 |
|
| 25 | 25 |
private final String[] mPhaseNames; |
| 26 | 26 |
private final TextView mText, mPhase; |
| 27 |
private final int mNumPhases; |
|
| 28 |
private final int[][][] mMoves; |
|
| 27 | 29 |
|
| 28 |
private int[][][] mMoves; |
|
| 29 |
private int mNumMoves,mCurrMove; |
|
| 30 |
private int mNumPhases,mCurrPhase; |
|
| 30 |
private int mNumMoves,mCurrMove,mCurrPhase; |
|
| 31 | 31 |
private boolean mCanMove; |
| 32 | 32 |
private ObjectControl mControl; |
| 33 | 33 |
|
| ... | ... | |
| 40 | 40 |
|
| 41 | 41 |
mPhaseNames = names; |
| 42 | 42 |
mNumPhases = names.length; |
| 43 |
|
|
| 43 |
mMoves = new int[mNumPhases][][]; |
|
| 44 | 44 |
mCanMove = true; |
| 45 |
setMoves(act,null); |
|
| 45 |
setMoves(act,null,0);
|
|
| 46 | 46 |
} |
| 47 | 47 |
|
| 48 | 48 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| ... | ... | |
| 59 | 59 |
else if( mCurrPhase>0 ) |
| 60 | 60 |
{
|
| 61 | 61 |
mCurrPhase--; |
| 62 |
mNumMoves = mMoves[mCurrPhase].length; |
|
| 62 |
mNumMoves = mMoves[mCurrPhase]==null ? 0 : mMoves[mCurrPhase].length;
|
|
| 63 | 63 |
int[][] moves = transformMoves(mMoves[mCurrPhase],0,mNumMoves, false); |
| 64 | 64 |
mControl = act.getControl(); |
| 65 | 65 |
mControl.applyScrambles(moves); |
| ... | ... | |
| 67 | 67 |
else |
| 68 | 68 |
{
|
| 69 | 69 |
mCurrPhase = mNumPhases-1; |
| 70 |
mNumMoves = mMoves[mCurrPhase].length; |
|
| 70 |
mNumMoves = mMoves[mCurrPhase]==null ? 0 : mMoves[mCurrPhase].length;
|
|
| 71 | 71 |
mCurrMove = mNumMoves; |
| 72 | 72 |
mControl = act.getControl(); |
| 73 | 73 |
|
| ... | ... | |
| 92 | 92 |
else if( mCurrPhase<mNumPhases-1 ) |
| 93 | 93 |
{
|
| 94 | 94 |
mCurrPhase++; |
| 95 |
mNumMoves = mMoves[mCurrPhase].length; |
|
| 95 |
mNumMoves = mMoves[mCurrPhase]==null ? 0 : mMoves[mCurrPhase].length;
|
|
| 96 | 96 |
mCurrMove = mNumMoves; |
| 97 | 97 |
int[][] moves = transformMoves(mMoves[mCurrPhase],0,mNumMoves, true); |
| 98 | 98 |
mControl = act.getControl(); |
| ... | ... | |
| 101 | 101 |
else |
| 102 | 102 |
{
|
| 103 | 103 |
mCurrPhase = 0; |
| 104 |
mNumMoves = mMoves[mCurrPhase].length; |
|
| 104 |
mNumMoves = mMoves[mCurrPhase]==null ? 0 : mMoves[mCurrPhase].length;
|
|
| 105 | 105 |
mCurrMove = 0; |
| 106 | 106 |
mControl = act.getControl(); |
| 107 | 107 |
int[][] moves = transformMoves(mMoves, false); |
| ... | ... | |
| 193 | 193 |
|
| 194 | 194 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 195 | 195 |
|
| 196 |
void setMoves(SolverActivity act, int[][][] moves)
|
|
| 196 |
void setMoves(SolverActivity act, int[][] moves, int phase)
|
|
| 197 | 197 |
{
|
| 198 |
mMoves = moves;
|
|
| 199 |
mCurrPhase= 0;
|
|
| 200 |
mNumMoves = moves==null || moves[0]==null ? 0 : moves[0].length;
|
|
| 198 |
mMoves[phase] = moves;
|
|
| 199 |
if( phase==0 ) mNumMoves = (moves==null ? 0 : moves.length);
|
|
| 200 |
mCurrPhase = 0;
|
|
| 201 | 201 |
mCurrMove = 0; |
| 202 | 202 |
|
| 203 | 203 |
setText(act); |
| ... | ... | |
| 207 | 207 |
|
| 208 | 208 |
void clearMoves(SolverActivity act) |
| 209 | 209 |
{
|
| 210 |
setMoves(act,null); |
|
| 210 |
setMoves(act,null,0);
|
|
| 211 | 211 |
} |
| 212 | 212 |
|
| 213 | 213 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| ... | ... | |
| 227 | 227 |
else if( mCurrPhase>0 ) |
| 228 | 228 |
{
|
| 229 | 229 |
mCurrPhase--; |
| 230 |
mNumMoves = mMoves[mCurrPhase].length; |
|
| 230 |
mNumMoves = mMoves[mCurrPhase]==null ? 0 : mMoves[mCurrPhase].length;
|
|
| 231 | 231 |
mCurrMove = mNumMoves; |
| 232 | 232 |
} |
| 233 | 233 |
else |
| 234 | 234 |
{
|
| 235 | 235 |
mCurrPhase = mNumPhases-1; |
| 236 |
mNumMoves = mMoves[mCurrPhase].length; |
|
| 236 |
mNumMoves = mMoves[mCurrPhase]==null ? 0 : mMoves[mCurrPhase].length;
|
|
| 237 | 237 |
mCurrMove = mNumMoves; |
| 238 | 238 |
mControl = act.getControl(); |
| 239 | 239 |
|
| ... | ... | |
| 262 | 262 |
else if( mCurrPhase<mNumPhases-1 ) |
| 263 | 263 |
{
|
| 264 | 264 |
mCurrPhase++; |
| 265 |
mNumMoves = mMoves[mCurrPhase].length; |
|
| 265 |
mNumMoves = mMoves[mCurrPhase]==null ? 0 : mMoves[mCurrPhase].length;
|
|
| 266 | 266 |
mCurrMove = 0; |
| 267 | 267 |
} |
| 268 | 268 |
else |
| 269 | 269 |
{
|
| 270 | 270 |
mCurrPhase = 0; |
| 271 |
mNumMoves = mMoves[mCurrPhase].length; |
|
| 271 |
mNumMoves = mMoves[mCurrPhase]==null ? 0 : mMoves[mCurrPhase].length;
|
|
| 272 | 272 |
mCurrMove = 0; |
| 273 | 273 |
mControl = act.getControl(); |
| 274 | 274 |
|
Also available in: Unified diff
Introduce SolutionListener interface which permits to send solutions phase-by-phase. Time time keeping to the app.