Project

General

Profile

Download (11.2 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / objects / TwistyBandaged3Plate.java @ 68ce0d53

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 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.objects;
21

    
22
import android.content.res.Resources;
23

    
24
import org.distorted.library.main.DistortedEffects;
25
import org.distorted.library.main.DistortedTexture;
26
import org.distorted.library.mesh.MeshSquare;
27
import org.distorted.library.type.Static4D;
28
import org.distorted.main.R;
29

    
30
import java.util.Random;
31

    
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33

    
34
class TwistyBandaged3Plate extends TwistyBandagedAbstract
35
{
36
  private int mCurrState;
37
  private boolean mUseX;
38
  private boolean mUseY;
39
  private boolean mUseZ;
40

    
41
  private static class State
42
    {
43
    private final int mNumX, mNumY, mNumZ;
44
    private final int[] mInfo;
45
    private final int[] mTmp;
46
    private final int LEN = 4;
47

    
48
    State(int[] x, int[] y, int[] z)
49
      {
50
      mTmp = new int[LEN];
51

    
52
      mNumX = x==null ? 0 : x.length/(LEN-1);
53
      mNumY = y==null ? 0 : y.length/(LEN-1);
54
      mNumZ = z==null ? 0 : z.length/(LEN-1);
55

    
56
      mInfo = new int[LEN*(mNumX+mNumY+mNumZ)];
57
      int start = 0;
58

    
59
      for(int i=0; i<mNumX; i++)
60
        {
61
        mInfo[LEN*i   + start] = 0;
62
        mInfo[LEN*i+1 + start] = x[(LEN-1)*i  ];
63
        mInfo[LEN*i+2 + start] = x[(LEN-1)*i+1];
64
        mInfo[LEN*i+3 + start] = x[(LEN-1)*i+2];
65
        }
66

    
67
      start = LEN*mNumX;
68

    
69
      for(int i=0; i<mNumY; i++)
70
        {
71
        mInfo[LEN*i   + start] = 1;
72
        mInfo[LEN*i+1 + start] = y[(LEN-1)*i  ];
73
        mInfo[LEN*i+2 + start] = y[(LEN-1)*i+1];
74
        mInfo[LEN*i+3 + start] = y[(LEN-1)*i+2];
75
        }
76

    
77
      start = LEN*(mNumX+mNumY);
78

    
79
      for(int i=0; i<mNumZ; i++)
80
        {
81
        mInfo[LEN*i   + start] = 2;
82
        mInfo[LEN*i+1 + start] = z[(LEN-1)*i  ];
83
        mInfo[LEN*i+2 + start] = z[(LEN-1)*i+1];
84
        mInfo[LEN*i+3 + start] = z[(LEN-1)*i+2];
85
        }
86
      }
87

    
88
    private int getIndex(int num, boolean useX, boolean useY, boolean useZ)
89
      {
90
      int current= -1, total= mNumX + mNumY + mNumZ;
91

    
92
      for(int i=0; i<total; i++)
93
        {
94
        if( (mInfo[LEN*i]==0 && useX) || (mInfo[LEN*i]==1 && useY) || (mInfo[LEN*i]==2 && useZ) )
95
          {
96
          if( ++current==num ) return i;
97
          }
98
        }
99

    
100
      return -1;
101
      }
102

    
103
    int getTotal(boolean useX, boolean useY, boolean useZ)
104
      {
105
      int total = 0;
106

    
107
      if( useX ) total += mNumX;
108
      if( useY ) total += mNumY;
109
      if( useZ ) total += mNumZ;
110

    
111
      return total;
112
      }
113

    
114
    int[] getInfo(int num, boolean useX, boolean useY, boolean useZ)
115
      {
116
      int index = getIndex(num,useX,useY,useZ);
117

    
118
      mTmp[0] = mInfo[LEN*index  ];   // axis
119
      mTmp[1] = mInfo[LEN*index+1];   // row
120
      mTmp[2] = mInfo[LEN*index+2];   // angle
121
      mTmp[3] = mInfo[LEN*index+3];   // next state
122

    
123
      return mTmp;
124
      }
125
    }
126

    
127
  // The 16 'significant' states of the 3Plate bandaged cube.
128
  // One State means one arrangement of the three 2x2 'plates'. Such State precisely defines which
129
  // rotations of the Cube are possible.
130
  // There are 27 such states in total, but 2 of them are unreachable from the initial State, and
131
  // 9 more and 'insignificant' - i.e. States which only permit rotation along a single axis.
132
  // When doing an automatic scramble, we never want to enter such 'insignificant' states because
133
  // that would mean we'd have to do two rotations in a row along the same axis.
134
  //
135
  // 4th State's first 'x' array being '2,-1,10, 2, 2,13' means the following:
136
  // if we are in the 4th state, and make move (2,-1) [i.e. rotation along the X axis, 2nd row, -1 angle]
137
  // then we will land in state 10. If we make move (2,2), we will land in state 13. There are no other
138
  // 'x' moves that lead to a 'significant' state.
139

    
140
  private final State[] mStates = new State[]
141
      {
142
         new State( new int[] { 2,-1, 1, 2, 1, 6                  }, new int[] { 0,-1, 5, 0, 1, 3                  }, new int[] { 2,-1, 2, 2, 1, 4                  } ),
143
         new State( new int[] { 2, 1, 0                           }, null                                           , new int[] { 2, 1,10, 2, 2, 7                  } ),
144
         new State( null                                           , new int[] { 0,-1,11, 0, 2, 8                  }, new int[] { 2, 1, 0                           } ),
145
         new State( new int[] { 2, 1,12, 2, 2, 9                  }, new int[] { 0,-1, 0                           }, null                                            ),
146
         new State( new int[] { 2,-1,10, 2, 2,13                  }, null                                           , new int[] { 2,-1, 0                           } ),
147
         new State( null                                           , new int[] { 0, 1, 0                           }, new int[] { 2,-1,11, 2, 2,14                  } ),
148
         new State( new int[] { 2,-1, 0                           }, new int[] { 0, 1,12, 0, 2,15                  }, null                                            ),
149
         new State( null                                           , new int[] { 2,-2, 7, 2,-1, 7, 2, 1, 7, 2, 2, 7}, new int[] { 2,-1,10, 2, 2, 1                  } ),
150
         new State( new int[] { 0,-2, 8, 0,-1, 8, 0, 1, 8, 0, 2, 8}, new int[] { 0, 1,11, 0, 2, 2                  }, null                                            ),
151
         new State( new int[] { 2,-1,12, 2, 2, 3                  }, null                                           , new int[] { 0,-2, 9, 0,-1, 9, 0, 1, 9, 0, 2, 9} ),
152
         new State( new int[] { 2,-1,13, 2, 1, 4                  }, new int[] { 2,-2,10, 2,-1,10, 2, 1,10, 2, 2,10}, new int[] { 2,-1, 1, 2, 1, 7                  } ),
153
         new State( new int[] { 0,-2,11, 0,-1,11, 0, 1,11, 0, 2,11}, new int[] { 0,-1, 8, 0, 1, 2                  }, new int[] { 2,-1,14, 2, 1, 5                  } ),
154
         new State( new int[] { 2,-1, 3, 2, 1, 9                  }, new int[] { 0,-1, 6, 0, 1,15                  }, new int[] { 0,-2,12, 0,-1,12, 0, 1,12, 0, 2,12} ),
155
         new State( new int[] { 2, 1,10, 2, 2, 4                  }, new int[] { 2,-2,13, 2,-1,13, 2, 1,13, 2, 2,13}, null                                            ),
156
         new State( new int[] { 0,-2,14, 0,-1,14, 0, 1,14, 0, 2,14}, null                                           , new int[] { 2, 1,11, 2, 2, 5                  } ),
157
         new State( null                                           , new int[] { 0,-1,12, 0, 2, 6                  }, new int[] { 0,-2,15, 0,-1,15, 0, 1,15, 0, 2,15} )
158
      };
159

    
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161

    
162
  private static final float[][] POSITIONS = new float[][]
163
      {
164
       {-1.0f,  1.0f,  1.0f, -1.0f,  0.0f,  1.0f,  0.0f,  1.0f,  1.0f,  0.0f,  0.0f,  1.0f},
165
       { 1.0f,  0.0f, -1.0f,  1.0f,  0.0f,  0.0f,  1.0f,  1.0f, -1.0f,  1.0f,  1.0f,  0.0f},
166
       {-1.0f, -1.0f, -1.0f, -1.0f, -1.0f,  0.0f,  0.0f, -1.0f, -1.0f,  0.0f, -1.0f,  0.0f},
167
       { 1.0f,  1.0f,  1.0f},
168
       { 1.0f,  0.0f,  1.0f},
169
       { 1.0f, -1.0f,  1.0f},
170
       {-1.0f, -1.0f,  1.0f},
171
       { 0.0f, -1.0f,  1.0f},
172
       { 1.0f, -1.0f,  0.0f},
173
       { 1.0f, -1.0f, -1.0f},
174
       {-1.0f,  1.0f, -1.0f},
175
       {-1.0f,  1.0f,  0.0f},
176
       { 0.0f,  1.0f, -1.0f},
177
       { 0.0f,  1.0f,  0.0f},
178
       {-1.0f,  0.0f, -1.0f},
179
       {-1.0f,  0.0f,  0.0f},
180
       { 0.0f,  0.0f, -1.0f}
181
      };
182

    
183
  private static final int[] QUAT_INDICES = new int[] { 1, 3 };
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

    
187
  TwistyBandaged3Plate(int size, Static4D quat, DistortedTexture texture, MeshSquare mesh,
188
                       DistortedEffects effects, int[][] moves, Resources res, int scrWidth)
189
    {
190
    super(size, quat, texture, mesh, effects, moves, ObjectList.BAN3, res, scrWidth);
191
    }
192

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

    
195
  float[][] getPositions()
196
    {
197
    return POSITIONS;
198
    }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

    
202
  int[] getQuatIndices()
203
    {
204
    return QUAT_INDICES;
205
    }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208
// PUBLIC API
209

    
210
  public void randomizeNewScramble(int[][] scramble, Random rnd, int num)
211
    {
212
    if( num==0 )
213
      {
214
      mCurrState = 0;
215
      mUseX = true;
216
      mUseY = true;
217
      mUseZ = true;
218
      }
219

    
220
    int total = mStates[mCurrState].getTotal(mUseX,mUseY,mUseZ);
221
    int random= rnd.nextInt(total);
222
    int[] info= mStates[mCurrState].getInfo(random,mUseX,mUseY,mUseZ);
223

    
224
    scramble[num][0] = info[0];
225
    scramble[num][1] = info[1];
226
    scramble[num][2] = info[2];
227

    
228
    mCurrState = info[3];
229

    
230
    switch(info[0])
231
      {
232
      case 0: mUseX = false; mUseY = true ; mUseZ = true ; break;
233
      case 1: mUseX = true ; mUseY = false; mUseZ = true ; break;
234
      case 2: mUseX = true ; mUseY = true ; mUseZ = false; break;
235
      }
236

    
237
    //android.util.Log.e("D", (info[0]==0 ? "X" : (info[0]==1 ? "Y" : "Z")) + info[2] +" --> "+info[3]);
238
    }
239

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241

    
242
  public int getObjectName(int numLayers)
243
    {
244
    return R.string.bandaged_3plate;
245
    }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

    
249
  public int getInventor(int numLayers)
250
    {
251
    return R.string.bandaged_3plate_inventor;
252
    }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

    
256
  public int getComplexity(int numLayers)
257
    {
258
    return 8;
259
    }
260
}
(17-17/35)