commit 222938b7284dfc16b88999f97d3078e1bb7d7e4c
Author: leszek <leszek@koltunski.pl>
Date:   Wed Dec 4 00:38:20 2024 +0100

    Introduce SolutionListener interface which permits to send solutions phase-by-phase. Time time keeping to the app.

diff --git a/src/main/java/org/distorted/phasedsolver/SolverActivity.java b/src/main/java/org/distorted/phasedsolver/SolverActivity.java
index ba3bba6..1c6c19c 100644
--- a/src/main/java/org/distorted/phasedsolver/SolverActivity.java
+++ b/src/main/java/org/distorted/phasedsolver/SolverActivity.java
@@ -31,7 +31,7 @@ import org.distorted.os.OSInterface;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-public class SolverActivity extends Activity
+public class SolverActivity extends Activity implements SolutionListener
 {
   private InitAssets mAsset;
   private SharedPreferences mPreferences;
@@ -41,6 +41,7 @@ public class SolverActivity extends Activity
   private String mMoves;
   private int mNumMoves;
   private Solver3x3Beginner mBeginner;
+  private long mTime;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
@@ -146,88 +147,81 @@ public class SolverActivity extends Activity
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  private void solve(int[] quats)
+  public void receiveSolution(int[] solution, int phase)
     {
-    int[][] sol = mBeginner.solution(quats);
-    int numPhases = sol.length;
-
-    for(int p=0; p<numPhases; p++)
+    if( solution==null )
       {
-      int numMoves = sol[p]==null ? -1 : sol[p][0];
-
-      if( numMoves<0 )
-        {
-        String message = "Phase "+p+": FAIL";
-        Toast.makeText(this,message,Toast.LENGTH_LONG).show();
-        break;
-        }
+      String message = "Phase "+phase+": FAIL";
+      Toast.makeText(this,message,Toast.LENGTH_LONG).show();
       }
-
-    int[][][] moves = new int[numPhases][][];
-
-    for( int p=0; p<numPhases; p++ )
+    else
       {
-      int numMoves = sol[p][0];
-      moves[p] = new int[numMoves][];
+      int numMoves = solution[0];
+      int[][] moves = new int[numMoves][];
       int index = 0;
 
       for(int m=1; m<=numMoves; m++)
-        {
-        int mi = sol[p][m];
-        moves[p][index++] = mObject.findMove(mi);
-        }
+        moves[index++] = mObject.findMove(solution[m]);
+
+      mLowerPane.setMoves(this,moves,phase);
       }
 
-    mLowerPane.setMoves(this,moves);
+    long time = System.currentTimeMillis();
+    long diff = time - mTime;
+    mTime = time;
+
+    System.out.println("Phase "+phase+" solved in "+diff);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    public void Solve(View v)
+  public void Solve(View v)
+    {
+    TwistyObject object = getControl().getObject();
+    int numCubits = 26;
+    int[] qi = new int[numCubits];
+    for(int i=0; i<numCubits; i++) qi[i] = object.getCubitQuatIndex(i);
+    final SolutionListener listener = this;
+    mTime = System.currentTimeMillis();
+
+    runOnUiThread(new Runnable()
       {
-      TwistyObject object = getControl().getObject();
-      int numCubits = 26;
-      int[] qi = new int[numCubits];
-      for(int i=0; i<numCubits; i++) qi[i] = object.getCubitQuatIndex(i);
-
-      runOnUiThread(new Runnable()
+      @Override
+      public void run()
         {
-        @Override
-        public void run()
-          {
-          solve(qi);
-          }
-        });
-      }
+        mBeginner.solution(listener, qi);
+        }
+      });
+    }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    public void Reset(View v)
-      {
-      ObjectControl control = getControl();
-      control.solveObject();
-      mLowerPane.clearMoves(this);
+  public void Reset(View v)
+    {
+    ObjectControl control = getControl();
+    control.solveObject();
+    mLowerPane.clearMoves(this);
 
-      mNumMoves = 0;
-      mMoves = "";
-      mText.setText(mMoves);
-      }
+    mNumMoves = 0;
+    mMoves = "";
+    mText.setText(mMoves);
+    }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-    void addMove(int axis, int row, int angle)
-      {
-      int move = mObject.findMove(axis,row,angle);
-      mMoves += (" "+move);
-      mNumMoves++;
-
-      if( mNumMoves>10 )
-        {
-        int space = mMoves.indexOf(' ');
-        mMoves = mMoves.substring(space+1);
-        mNumMoves--;
-        }
+  void addMove(int axis, int row, int angle)
+    {
+    int move = mObject.findMove(axis,row,angle);
+    mMoves += (" "+move);
+    mNumMoves++;
 
-      mText.setText(mMoves);
+    if( mNumMoves>10 )
+      {
+      int space = mMoves.indexOf(' ');
+      mMoves = mMoves.substring(space+1);
+      mNumMoves--;
       }
+
+    mText.setText(mMoves);
+    }
 }
diff --git a/src/main/java/org/distorted/phasedsolver/SolverLowerPane.java b/src/main/java/org/distorted/phasedsolver/SolverLowerPane.java
index fdc9c1f..74f37e4 100644
--- a/src/main/java/org/distorted/phasedsolver/SolverLowerPane.java
+++ b/src/main/java/org/distorted/phasedsolver/SolverLowerPane.java
@@ -24,10 +24,10 @@ public class SolverLowerPane implements MovesFinished
 
   private final String[] mPhaseNames;
   private final TextView mText, mPhase;
+  private final int mNumPhases;
+  private final int[][][] mMoves;
 
-  private int[][][] mMoves;
-  private int mNumMoves,mCurrMove;
-  private int mNumPhases,mCurrPhase;
+  private int mNumMoves,mCurrMove,mCurrPhase;
   private boolean mCanMove;
   private ObjectControl mControl;
 
@@ -40,9 +40,9 @@ public class SolverLowerPane implements MovesFinished
 
     mPhaseNames = names;
     mNumPhases = names.length;
-
+    mMoves = new int[mNumPhases][][];
     mCanMove = true;
-    setMoves(act,null);
+    setMoves(act,null,0);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -59,7 +59,7 @@ public class SolverLowerPane implements MovesFinished
     else if( mCurrPhase>0 )
       {
       mCurrPhase--;
-      mNumMoves = mMoves[mCurrPhase].length;
+      mNumMoves = mMoves[mCurrPhase]==null ? 0 : mMoves[mCurrPhase].length;
       int[][] moves = transformMoves(mMoves[mCurrPhase],0,mNumMoves, false);
       mControl = act.getControl();
       mControl.applyScrambles(moves);
@@ -67,7 +67,7 @@ public class SolverLowerPane implements MovesFinished
     else
       {
       mCurrPhase = mNumPhases-1;
-      mNumMoves = mMoves[mCurrPhase].length;
+      mNumMoves = mMoves[mCurrPhase]==null ? 0 : mMoves[mCurrPhase].length;
       mCurrMove = mNumMoves;
       mControl = act.getControl();
 
@@ -92,7 +92,7 @@ public class SolverLowerPane implements MovesFinished
     else if( mCurrPhase<mNumPhases-1 )
       {
       mCurrPhase++;
-      mNumMoves = mMoves[mCurrPhase].length;
+      mNumMoves = mMoves[mCurrPhase]==null ? 0 : mMoves[mCurrPhase].length;
       mCurrMove = mNumMoves;
       int[][] moves = transformMoves(mMoves[mCurrPhase],0,mNumMoves, true);
       mControl = act.getControl();
@@ -101,7 +101,7 @@ public class SolverLowerPane implements MovesFinished
     else
       {
       mCurrPhase = 0;
-      mNumMoves = mMoves[mCurrPhase].length;
+      mNumMoves = mMoves[mCurrPhase]==null ? 0 : mMoves[mCurrPhase].length;
       mCurrMove = 0;
       mControl = act.getControl();
       int[][] moves = transformMoves(mMoves, false);
@@ -193,11 +193,11 @@ public class SolverLowerPane implements MovesFinished
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  void setMoves(SolverActivity act, int[][][] moves)
+  void setMoves(SolverActivity act, int[][] moves, int phase)
     {
-    mMoves    = moves;
-    mCurrPhase= 0;
-    mNumMoves = moves==null || moves[0]==null ? 0 : moves[0].length;
+    mMoves[phase] = moves;
+    if( phase==0 ) mNumMoves = (moves==null ? 0 : moves.length);
+    mCurrPhase = 0;
     mCurrMove = 0;
 
     setText(act);
@@ -207,7 +207,7 @@ public class SolverLowerPane implements MovesFinished
 
   void clearMoves(SolverActivity act)
     {
-    setMoves(act,null);
+    setMoves(act,null,0);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -227,13 +227,13 @@ public class SolverLowerPane implements MovesFinished
       else if( mCurrPhase>0 )
         {
         mCurrPhase--;
-        mNumMoves = mMoves[mCurrPhase].length;
+        mNumMoves = mMoves[mCurrPhase]==null ? 0 : mMoves[mCurrPhase].length;
         mCurrMove = mNumMoves;
         }
       else
         {
         mCurrPhase = mNumPhases-1;
-        mNumMoves = mMoves[mCurrPhase].length;
+        mNumMoves = mMoves[mCurrPhase]==null ? 0 : mMoves[mCurrPhase].length;
         mCurrMove = mNumMoves;
         mControl = act.getControl();
 
@@ -262,13 +262,13 @@ public class SolverLowerPane implements MovesFinished
       else if( mCurrPhase<mNumPhases-1 )
         {
         mCurrPhase++;
-        mNumMoves = mMoves[mCurrPhase].length;
+        mNumMoves = mMoves[mCurrPhase]==null ? 0 : mMoves[mCurrPhase].length;
         mCurrMove = 0;
         }
       else
         {
         mCurrPhase = 0;
-        mNumMoves = mMoves[mCurrPhase].length;
+        mNumMoves = mMoves[mCurrPhase]==null ? 0 : mMoves[mCurrPhase].length;
         mCurrMove = 0;
         mControl = act.getControl();
 
