Project

General

Profile

Download (9.11 KB) Statistics
| Branch: | Revision:

distorted-objectlib / src / main / java / org / distorted / objectlib / scrambling / ScrambleStateBandagedCuboid.java @ a72a4b6a

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 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.objectlib.scrambling;
21

    
22
///////////////////////////////////////////////////////////////////////////////////////////////////
23
// Info about a scramble state of any bandaged cuboid.
24

    
25
import org.distorted.objectlib.helpers.ObjectSignature;
26

    
27
public class ScrambleStateBandagedCuboid
28
{
29
  public static int MAX_SUPPORTED_SIZE = 5;
30

    
31
  public static final int AXIS_NONE = -1;
32
  public static final int AXIS_X = 0;
33
  public static final int AXIS_Y = 1;
34
  public static final int AXIS_Z = 2;
35

    
36
  private final ObjectSignature[] mMoves;
37
  private final ObjectSignature mSignature;
38
  private final int[] mLayer, mTurns, mSize;
39
  private final int mNumMoves;
40
  private final int mStartX, mStartY, mStartZ;
41
  private final boolean[][] mIsUnblocked;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
  public ScrambleStateBandagedCuboid(int x, int y, int z, ObjectSignature signature)
46
    {
47
    mLayer = new int[3];
48
    mTurns = new int[3];
49
    mSize  = new int[3];
50

    
51
    mIsUnblocked = new boolean[3][MAX_SUPPORTED_SIZE];
52

    
53
    mLayer[0] = x;
54
    mLayer[1] = y;
55
    mLayer[2] = z;
56

    
57
    mTurns[0] = mLayer[1]==mLayer[2] ? 3:1;
58
    mTurns[1] = mLayer[0]==mLayer[2] ? 3:1;
59
    mTurns[2] = mLayer[0]==mLayer[1] ? 3:1;
60

    
61
    mSize[0] = (mLayer[0]>1 ? mLayer[0] : 0);
62
    mSize[1] = (mLayer[1]>1 ? mLayer[1] : 0);
63
    mSize[2] = (mLayer[2]>1 ? mLayer[2] : 0);
64

    
65
    int xMoves = mTurns[0]*mSize[0];
66
    int yMoves = mTurns[1]*mSize[1];
67
    int zMoves = mTurns[2]*mSize[2];
68

    
69
    mNumMoves = xMoves + yMoves + zMoves;
70

    
71
    mStartX = 0;
72
    mStartY = xMoves;
73
    mStartZ = xMoves + yMoves;
74

    
75
    mSignature = signature;
76
    mMoves = createMoves();
77
    }
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

    
81
  public ObjectSignature getSignature()
82
    {
83
    return mSignature;
84
    }
85

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

    
88
  public ObjectSignature getMove(int index)
89
    {
90
    return (index>=0 && index<mNumMoves) ? mMoves[index] : null;
91
    }
92

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

    
95
  public void removeMoves(ObjectSignature signature)
96
    {
97
    for(int m=0; m<mNumMoves; m++)
98
      if( mMoves[m]!=null && signature.isEqual(mMoves[m]) ) mMoves[m]=null;
99
    }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102

    
103
  public int numAxis()
104
    {
105
    int num = 0;
106

    
107
    for(int x=mStartX; x<mStartY  ; x++) if( mMoves[x]!=null ) { num++; break; }
108
    for(int y=mStartY; y<mStartZ  ; y++) if( mMoves[y]!=null ) { num++; break; }
109
    for(int z=mStartZ; z<mNumMoves; z++) if( mMoves[z]!=null ) { num++; break; }
110

    
111
    return num;
112
    }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

    
116
  private int numXMoves()
117
    {
118
    int num=0;
119
    for(int x=mStartX; x<mStartY; x++) if( mMoves[x]!=null ) num++;
120
    return num;
121
    }
122

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124

    
125
  private int numYMoves()
126
    {
127
    int num=0;
128
    for(int y=mStartY; y<mStartZ; y++) if( mMoves[y]!=null ) num++;
129
    return num;
130
    }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

    
134
  private int numZMoves()
135
    {
136
    int num=0;
137
    for(int z=mStartZ; z<mNumMoves; z++) if( mMoves[z]!=null ) num++;
138
    return num;
139
    }
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142

    
143
  public int numMoves()
144
    {
145
    return numXMoves()+numYMoves()+numZMoves();
146
    }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

    
150
  public int numMoves(int excludedAxis)
151
    {
152
    switch(excludedAxis)
153
      {
154
      case AXIS_X: return numYMoves()+numZMoves();
155
      case AXIS_Y: return numXMoves()+numZMoves();
156
      case AXIS_Z: return numXMoves()+numYMoves();
157
      }
158

    
159
    return numXMoves()+numYMoves()+numZMoves();
160
    }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

    
164
  public int getNthMove(int n, int excludedAxis)
165
    {
166
    int num = 0;
167

    
168
    if( excludedAxis!=0 )
169
      {
170
      for(int m=mStartX; m<mStartY; m++)
171
        if( mMoves[m]!=null )
172
          {
173
          if( num==n ) return m;
174
          num++;
175
          }
176
      }
177

    
178
    if( excludedAxis!=1 )
179
      {
180
      for(int m=mStartY; m<mStartZ; m++)
181
        if( mMoves[m]!=null )
182
          {
183
          if( num==n ) return m;
184
          num++;
185
          }
186
      }
187

    
188
    if( excludedAxis!=2 )
189
      {
190
      for(int m=mStartZ; m<mNumMoves; m++)
191
        if( mMoves[m]!=null )
192
          {
193
          if( num==n ) return m;
194
          num++;
195
          }
196
      }
197

    
198
    return -1;
199
    }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
  private ObjectSignature[] createMoves()
204
    {
205
    ObjectSignature[] ret = new ObjectSignature[mNumMoves];
206
    int index = 0;
207

    
208
    for(int axis=0; axis<3; axis++)
209
      for(int layer=0; layer<mLayer[axis]; layer++)
210
        {
211
        mIsUnblocked[axis][layer] = mSignature.isUnblockedFromLeft(axis,layer);
212
        }
213

    
214
    for(int axis=0; axis<3; axis++)
215
      if( mLayer[axis]>1 )
216
        for(int turn=1; turn<=mTurns[axis]; turn++)
217
          {
218
          boolean allLayersLocked = true;
219

    
220
          for(int layer=0; layer<mLayer[axis]; layer++)
221
            {
222
            if( mIsUnblocked[axis][layer] )
223
              {
224
              if( layer>0 ) allLayersLocked = false;
225
              ret[index] = mSignature.turn(axis,layer,turn);
226
              }
227
            else
228
              {
229
              ret[index] = ret[index-1].turn(axis,layer,turn);
230
              ret[index-1] = null;
231
              }
232

    
233
            index++;
234
            }
235

    
236
          if( allLayersLocked ) ret[index-1] = null;
237
          }
238

    
239
    return ret;
240
    }
241

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

    
244
  public void fillOutScramble(int[] scramble, int moveIndex)
245
    {
246
    for(int axis=0; axis<3; axis++)
247
      {
248
      int size = mTurns[axis]*mSize[axis];
249

    
250
      if( moveIndex<size )
251
        {
252
        scramble[0] = axis;
253
        scramble[1] = moveIndex % mSize[axis];
254

    
255
        if( mTurns[axis]==3 )
256
          {
257
          switch(moveIndex/mSize[axis])
258
            {
259
            case 0: scramble[2] =-1; break;
260
            case 1: scramble[2] = 2; break;
261
            case 2: scramble[2] = 1; break;
262
            }
263
          }
264
        else scramble[2] = 1;
265
        return;
266
        }
267

    
268
      moveIndex -= size;
269
      }
270

    
271
    android.util.Log.e("D", "ERROR in fillOutScramble moveIndex="+moveIndex);
272
    }
273

    
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275

    
276
  private void printMoves()
277
    {
278
    for(int i=0; i<mNumMoves; i++)
279
      {
280
      android.util.Log.e("D", "move "+i+" : "+(mMoves[i]!=null ? " "+mMoves[i].getString() : " NULL") );
281
      }
282
    }
283

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

    
286
  private static String printBits(long id)
287
    {
288
    String ret = "[";
289
    boolean first = true;
290

    
291
    for(int i=0; i<64; i++)
292
      {
293
      if( ( (id>>i)&0x1)==1 )
294
        {
295
        String num = (i<10 ? " "+i : ""+i);
296

    
297
        if( first ) { ret += num; first=false; }
298
        else          ret += (","+num);
299
        }
300
      }
301

    
302
    return ret + "]";
303
    }
304
}
(4-4/5)