Project

General

Profile

« Previous | Next » 

Revision 4d2a5fd3

Added by Leszek Koltunski 3 months ago

Improve the Phased Solver App (show phases)

View differences:

src/main/java/org/distorted/phasedsolver/SolverActivity.java
54 54
    SolverSurfaceView view = findViewById(R.id.solverView);
55 55
    OSInterface os = view.getInterface();
56 56
    mAsset = new InitAssets(null, null, os);
57
    mLowerPane = new SolverLowerPane(this);
58 57
    mText = findViewById(R.id.solverMoves);
59 58
    mMoves = "";
60 59
    mNumMoves = 0;
61 60

  
62 61
    mBeginner = new Solver3x3Beginner();
63 62
    mObject = mBeginner.getObject();
63

  
64
    int numPhases = mBeginner.getNumPhases();
65
    String[] names = new String[numPhases];
66
    for(int p=0; p<numPhases; p++) names[p] = mBeginner.getPhaseName(p);
67

  
68
    mLowerPane = new SolverLowerPane(this,names);
64 69
    }
65 70

  
66 71
///////////////////////////////////////////////////////////////////////////////////////////////////
......
101 106
    super.onDestroy();
102 107
    }
103 108

  
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

  
111
  public void Left(View v)       { mLowerPane.backMove(this); }
112
  public void Right(View v)      { mLowerPane.nextMove(this); }
113
  public void PhaseLeft(View v)  { mLowerPane.backPhase(this); }
114
  public void PhaseRight(View v) { mLowerPane.nextPhase(this); }
115

  
104 116
///////////////////////////////////////////////////////////////////////////////////////////////////
105 117

  
106 118
  ObjectControl getControl()
......
138 150
    {
139 151
    int[][] sol = mBeginner.solution(quats);
140 152
    int numPhases = sol.length;
141
    int totalLength = 0;
142 153

  
143 154
    for(int p=0; p<numPhases; p++)
144 155
      {
......
148 159
        {
149 160
        String message = "Phase "+p+": FAIL";
150 161
        Toast.makeText(this,message,Toast.LENGTH_LONG).show();
151
        return;
162
        break;
152 163
        }
153

  
154
      totalLength += numMoves;
155 164
      }
156 165

  
157
    int[][] moves = new int[totalLength][];
158
    int index = 0;
166
    int[][][] moves = new int[numPhases][][];
159 167

  
160
    for( int[] s : sol )
168
    for( int p=0; p<numPhases; p++ )
161 169
      {
162
      int numMoves = s[0];
170
      int numMoves = sol[p][0];
171
      moves[p] = new int[numMoves][];
172
      int index = 0;
163 173

  
164 174
      for(int m=1; m<=numMoves; m++)
165 175
        {
166
        int mi = s[m];
167
        moves[index++] = mObject.findMove(mi);
176
        int mi = sol[p][m];
177
        moves[p][index++] = mObject.findMove(mi);
168 178
        }
169 179
      }
170 180

  
......
220 230

  
221 231
      mText.setText(mMoves);
222 232
      }
223

  
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225

  
226
    public void Left(View v)
227
      {
228
      mLowerPane.backMove(this);
229
      }
230

  
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

  
233
    public void Right(View v)
234
      {
235
      mLowerPane.makeMove(this);
236
      }
237 233
}
src/main/java/org/distorted/phasedsolver/SolverLowerPane.java
22 22
  private static final int MOVES_PLACE_1 = 101;
23 23
  private static final int MILLIS_PER_DEGREE = 6;
24 24

  
25
  private int[][] mMoves;
26
  private int mNumMoves;
27
  private int mCurrMove;
25
  private final String[] mPhaseNames;
26
  private final TextView mText, mPhase;
27

  
28
  private int[][][] mMoves;
29
  private int mNumMoves,mCurrMove;
30
  private int mNumPhases,mCurrPhase;
28 31
  private boolean mCanMove;
29
  private final TextView mText;
30 32
  private ObjectControl mControl;
31 33

  
32 34
///////////////////////////////////////////////////////////////////////////////////////////////////
33 35

  
34
  SolverLowerPane(SolverActivity act)
36
  SolverLowerPane(SolverActivity act, String[] names)
35 37
    {
36
    mText = act.findViewById(R.id.solverText);
38
    mText  = act.findViewById(R.id.solverText);
39
    mPhase = act.findViewById(R.id.solverPhaseName);
40

  
41
    mPhaseNames = names;
42
    mNumPhases = names.length;
43

  
37 44
    mCanMove = true;
38 45
    setMoves(act,null);
39 46
    }
40 47

  
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

  
50
  void backPhase(SolverActivity act)
51
    {
52

  
53
    }
54

  
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

  
57
  void nextPhase(SolverActivity act)
58
    {
59

  
60
    }
61

  
41 62
///////////////////////////////////////////////////////////////////////////////////////////////////
42 63

  
43 64
  private void setText(SolverActivity act)
......
47 68
      @Override
48 69
      public void run()
49 70
        {
71
        mPhase.setText(mPhaseNames[mCurrPhase]);
50 72
        mText.setText(mCurrMove+"/"+mNumMoves);
51 73
        }
52 74
      });
......
54 76

  
55 77
///////////////////////////////////////////////////////////////////////////////////////////////////
56 78

  
57
  private int[][] transformMoves(int[][] moves, boolean back)
79
  private int[][] transformMoves(int[][][] moves, boolean front)
58 80
    {
59 81
    int len = moves.length;
60
    int[][] ret = new int[len][];
61
    int mult = back ? 1:-1;
82
    int totalLen = 0;
83
    for (int[][] move : moves) totalLen += (move==null ? 0 : move.length);
84

  
85
    int[][] ret = new int[totalLen][];
86
    int mult = front ? 1:-1;
87
    int index = 0;
62 88

  
63 89
    for(int m=0; m<len; m++)
64 90
      {
65
      int[] mv = moves[back ? m : len-1-m];
66
      int[] rt = new int[3];
91
      int[][] mv = moves[front ? m : len-1-m];
92
      int l = (mv==null ? 0 : mv.length);
67 93

  
68
      rt[0] = mv[0];
69
      rt[1] = (1<<mv[1]);
70
      rt[2] = mult*mv[2];
71

  
72
      ret[m] = rt;
94
      for(int p=0; p<l; p++)
95
        {
96
        int[] mve = mv[front ? p : l-1-p];
97
        int[] rt = new int[3];
98
        rt[0] = mve[0];
99
        rt[1] = (1<<mve[1]);
100
        rt[2] = mult*mve[2];
101
        ret[index++] = rt;
102
        }
73 103
      }
74 104

  
75 105
    return ret;
......
77 107

  
78 108
///////////////////////////////////////////////////////////////////////////////////////////////////
79 109

  
80
  void setMoves(SolverActivity act, int[][] moves)
110
  void setMoves(SolverActivity act, int[][][] moves)
81 111
    {
82 112
    mMoves    = moves;
83
    mNumMoves = moves==null ? 0 : moves.length;
84
    mCurrMove = moves==null ?-1 : 0;
113
    mCurrPhase= 0;
114
    mNumMoves = moves==null || moves[0]==null ? 0 : moves[0].length;
115
    mCurrMove = 0;
85 116

  
86 117
    setText(act);
87 118
    }
......
102 133
      if( mCurrMove>0 )
103 134
        {
104 135
        mCanMove = false;
105
        int[] move = mMoves[--mCurrMove];
136
        int[] move = mMoves[mCurrPhase][--mCurrMove];
106 137
        mControl = act.getControl();
107 138
        mControl.blockTouch(MOVES_PLACE_0);
108 139
        mControl.addRotation(this, move[0], (1<<move[1]), -move[2], MILLIS_PER_DEGREE);
109 140
        }
141
      else if( mCurrPhase>0 )
142
        {
143
        mCurrPhase--;
144
        mNumMoves = mMoves[mCurrPhase].length;
145
        mCurrMove = mNumMoves;
146
        }
110 147
      else
111 148
        {
149
        mCurrPhase = mNumPhases-1;
150
        mNumMoves = mMoves[mCurrPhase].length;
112 151
        mCurrMove = mNumMoves;
113 152
        mControl = act.getControl();
114
        int[][] moves = transformMoves(mMoves,true);
153

  
154
        int[][] moves = transformMoves(mMoves, true);
115 155
        mControl.applyScrambles(moves);
116 156
        }
117 157

  
......
121 161

  
122 162
///////////////////////////////////////////////////////////////////////////////////////////////////
123 163

  
124
  void makeMove(SolverActivity act)
164
  void nextMove(SolverActivity act)
125 165
    {
126 166
    if( mMoves!=null && mCanMove )
127 167
      {
128 168
      if( mCurrMove<mNumMoves )
129 169
        {
130 170
        mCanMove = false;
131
        int[] move = mMoves[mCurrMove++];
171
        int[] move = mMoves[mCurrPhase][mCurrMove++];
132 172
        mControl = act.getControl();
133 173
        mControl.blockTouch(MOVES_PLACE_1);
134 174
        mControl.addRotation(this, move[0], (1<<move[1]), move[2], MILLIS_PER_DEGREE);
135 175
        }
176
      else if( mCurrPhase<mNumPhases-1 )
177
        {
178
        mCurrPhase++;
179
        mNumMoves = mMoves[mCurrPhase].length;
180
        mCurrMove = 0;
181
        }
136 182
      else
137 183
        {
184
        mCurrPhase = 0;
185
        mNumMoves = mMoves[mCurrPhase].length;
138 186
        mCurrMove = 0;
139 187
        mControl = act.getControl();
140
        int[][] moves = transformMoves(mMoves,false);
188

  
189
        int[][] moves = transformMoves(mMoves, false);
141 190
        mControl.applyScrambles(moves);
142 191
        }
143 192

  
src/main/res/layout/mainlayout.xml
48 48
        android:id="@+id/solverMoves"
49 49
        android:layout_below="@id/upperBar"
50 50
        android:layout_width="match_parent"
51
        android:layout_height="60dp"
51
        android:layout_height="50dp"
52
        android:background="@color/white"
53
        android:textColor="@color/black"
54
        android:layout_marginStart="4dp"
55
        android:layout_marginEnd="4dp"
52 56
        android:textAppearance="?android:attr/textAppearanceLarge"
53 57
        android:gravity="center_vertical|start"/>
54 58

  
......
70 74
            android:onClick="Left"
71 75
            android:layout_gravity="center_vertical"/>
72 76

  
73
        <TextView
74
            android:id="@+id/solverText"
77
        <LinearLayout
75 78
            android:layout_width="0dp"
76 79
            android:layout_height="match_parent"
77 80
            android:layout_weight="5.0"
78
            android:gravity="center"
79
            android:textSize="40sp"/>
81
            android:orientation="vertical"
82
            android:background="@android:color/transparent">
83

  
84
            <TextView
85
                android:id="@+id/solverPhaseName"
86
                android:layout_width="match_parent"
87
                android:layout_height="0dp"
88
                android:layout_weight="1"
89
                android:gravity="center"
90
                android:textSize="20sp"/>
91
            <TextView
92
                android:id="@+id/solverText"
93
                android:layout_width="match_parent"
94
                android:layout_height="0dp"
95
                android:layout_weight="2"
96
                android:gravity="center"
97
                android:textSize="30sp"/>
98

  
99
        </LinearLayout>
80 100

  
81 101
        <ImageButton
82 102
            android:id="@+id/solverRight"

Also available in: Unified diff