1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2024 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Magic Cube. //
|
5
|
// //
|
6
|
// Magic Cube is proprietary software licensed under an EULA which you should have received //
|
7
|
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html //
|
8
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
9
|
|
10
|
package org.distorted.solvers;
|
11
|
|
12
|
import android.content.res.Resources;
|
13
|
|
14
|
import org.distorted.objectlib.algsolvers.SolutionListener;
|
15
|
import org.distorted.objectlib.algsolvers.SolvedObject;
|
16
|
import org.distorted.objectlib.algsolvers.implemented.PhasedSolver3x3Beginner;
|
17
|
import org.distorted.objectlib.algsolvers.implemented.PhasedSolverAbstract;
|
18
|
import org.distorted.objectlib.helpers.OperatingSystemInterface;
|
19
|
import org.distorted.objectlib.main.TwistyObject;
|
20
|
import org.distorted.solverui.ScreenList;
|
21
|
import org.distorted.solverui.ScreenPhasedSolution;
|
22
|
import org.distorted.solverui.ScreenSolver;
|
23
|
|
24
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
25
|
|
26
|
public abstract class SolverAlgorithmic implements SolvingInterface, SolutionListener
|
27
|
{
|
28
|
private final SolvedObject mSolvedObject;
|
29
|
private final PhasedSolverAbstract mSolver;
|
30
|
private final OperatingSystemInterface mOS;
|
31
|
private final Resources mRes;
|
32
|
private final TwistyObject mObject;
|
33
|
private long mTime;
|
34
|
private ScreenSolver mScreen;
|
35
|
|
36
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
37
|
// PUBLIC API
|
38
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
39
|
|
40
|
public SolverAlgorithmic(OperatingSystemInterface os, Resources res, TwistyObject object)
|
41
|
{
|
42
|
mOS = os;
|
43
|
mRes = res;
|
44
|
mObject = object;
|
45
|
mSolver = new PhasedSolver3x3Beginner();
|
46
|
mSolvedObject = mSolver.getObject();
|
47
|
}
|
48
|
|
49
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
50
|
|
51
|
abstract int[] validatePosition(TwistyObject object);
|
52
|
abstract String getError(Resources res);
|
53
|
|
54
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
55
|
|
56
|
public void solve(ScreenSolver screen)
|
57
|
{
|
58
|
mScreen = screen;
|
59
|
int[] quats = validatePosition(mObject);
|
60
|
|
61
|
if( quats!=null )
|
62
|
{
|
63
|
int numPhases = mSolver.getNumPhases();
|
64
|
String[] names = new String[numPhases];
|
65
|
for(int p=0; p<numPhases; p++) names[p] = mSolver.getPhaseName(p);
|
66
|
ScreenPhasedSolution solScreen = (ScreenPhasedSolution) ScreenList.PHAS.getScreenClass();
|
67
|
solScreen.updateNames(names);
|
68
|
mTime = System.currentTimeMillis();
|
69
|
mSolver.solution(this,quats);
|
70
|
}
|
71
|
else
|
72
|
{
|
73
|
String error = getError(mRes);
|
74
|
screen.displayImpossibleDialog(error);
|
75
|
}
|
76
|
}
|
77
|
|
78
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
79
|
|
80
|
public void receiveSolution(int[] solution, int phaseNumber)
|
81
|
{
|
82
|
if( solution==null )
|
83
|
{
|
84
|
String message = "Phase "+phaseNumber+": FAIL";
|
85
|
System.out.println(message);
|
86
|
}
|
87
|
else
|
88
|
{
|
89
|
int numMoves = solution[0];
|
90
|
int[][] moves = new int[numMoves][];
|
91
|
int index = 0;
|
92
|
|
93
|
for(int m=1; m<=numMoves; m++)
|
94
|
moves[index++] = mSolvedObject.findMove(solution[m]);
|
95
|
|
96
|
int[][] subphases = mSolver.getSubPhases(phaseNumber);
|
97
|
mScreen.setSolved(moves,phaseNumber,subphases);
|
98
|
}
|
99
|
|
100
|
long time = System.currentTimeMillis();
|
101
|
long diff = time - mTime;
|
102
|
mTime = time;
|
103
|
|
104
|
System.out.println("Phase "+phaseNumber+" solved in "+diff+"ms. Moves: "+(solution==null ? 0:solution[0]));
|
105
|
}
|
106
|
}
|
107
|
|