Project

General

Profile

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

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

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 int numXMoves(int excludedLayer)
204
    {
205
    int num=0;
206
    for(int x=mStartX; x<mStartY; x++) if( mMoves[x]!=null && (x-mStartX)%mLayer[0]!=excludedLayer ) num++;
207
    return num;
208
    }
209

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

    
212
  private int numYMoves(int excludedLayer)
213
    {
214
    int num=0;
215
    for(int y=mStartY; y<mStartZ; y++) if( mMoves[y]!=null && (y-mStartY)%mLayer[1]!=excludedLayer ) num++;
216
    return num;
217
    }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

    
221
  private int numZMoves(int excludedLayer)
222
    {
223
    int num=0;
224
    for(int z=mStartZ; z<mNumMoves; z++) if( mMoves[z]!=null && (z-mStartZ)%mLayer[2]!=excludedLayer ) num++;
225
    return num;
226
    }
227

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

    
230
  public int numMoves(int excludedAxis, int excludedLayer)
231
    {
232
    switch(excludedAxis)
233
      {
234
      case AXIS_X: return numYMoves(excludedLayer)+numZMoves(excludedLayer);
235
      case AXIS_Y: return numXMoves(excludedLayer)+numZMoves(excludedLayer);
236
      case AXIS_Z: return numXMoves(excludedLayer)+numYMoves(excludedLayer);
237
      }
238

    
239
    return numXMoves(excludedLayer)+numYMoves(excludedLayer)+numZMoves(excludedLayer);
240
    }
241

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

    
244
  public int getNthMove(int n, int excludedAxis, int excludedLayer)
245
    {
246
    int num = 0;
247

    
248
    if( excludedAxis!=0 )
249
      {
250
      for(int m=mStartX; m<mStartY; m++)
251
        if( mMoves[m]!=null && (m-mStartX)%mLayer[0]!=excludedLayer )
252
          {
253
          if( num==n ) return m;
254
          num++;
255
          }
256
      }
257

    
258
    if( excludedAxis!=1 )
259
      {
260
      for(int m=mStartY; m<mStartZ; m++)
261
        if( mMoves[m]!=null && (m-mStartY)%mLayer[1]!=excludedLayer )
262
          {
263
          if( num==n ) return m;
264
          num++;
265
          }
266
      }
267

    
268
    if( excludedAxis!=2 )
269
      {
270
      for(int m=mStartZ; m<mNumMoves; m++)
271
        if( mMoves[m]!=null && (m-mStartZ)%mLayer[2]!=excludedLayer )
272
          {
273
          if( num==n ) return m;
274
          num++;
275
          }
276
      }
277

    
278
    return -1;
279
    }
280

    
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282

    
283
  private ObjectSignature[] createMoves()
284
    {
285
    ObjectSignature[] ret = new ObjectSignature[mNumMoves];
286
    int index = 0;
287

    
288
    for(int axis=0; axis<3; axis++)
289
      for(int layer=0; layer<mLayer[axis]; layer++)
290
        {
291
        mIsUnblocked[axis][layer] = mSignature.isUnblockedFromLeft(axis,layer);
292
        }
293

    
294
    for(int axis=0; axis<3; axis++)
295
      if( mLayer[axis]>1 )
296
        for(int turn=1; turn<=mTurns[axis]; turn++)
297
          {
298
          boolean allLayersLocked = true;
299

    
300
          for(int layer=0; layer<mLayer[axis]; layer++)
301
            {
302
            if( mIsUnblocked[axis][layer] )
303
              {
304
              if( layer>0 ) allLayersLocked = false;
305
              ret[index] = mSignature.turn(axis,layer,turn);
306
              }
307
            else
308
              {
309
              ret[index] = ret[index-1].turn(axis,layer,turn);
310
              ret[index-1] = null;
311
              }
312

    
313
            index++;
314
            }
315

    
316
          if( allLayersLocked ) ret[index-1] = null;
317
          }
318

    
319
    return ret;
320
    }
321

    
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323

    
324
  public void fillOutScramble(int[] scramble, int moveIndex)
325
    {
326
    for(int axis=0; axis<3; axis++)
327
      {
328
      int size = mTurns[axis]*mSize[axis];
329

    
330
      if( moveIndex<size )
331
        {
332
        scramble[0] = axis;
333
        scramble[1] = moveIndex % mSize[axis];
334

    
335
        if( mTurns[axis]==3 )
336
          {
337
          switch(moveIndex/mSize[axis])
338
            {
339
            case 0: scramble[2] =-1; break;
340
            case 1: scramble[2] = 2; break;
341
            case 2: scramble[2] = 1; break;
342
            }
343
          }
344
        else scramble[2] = 1;
345
        return;
346
        }
347

    
348
      moveIndex -= size;
349
      }
350

    
351
    android.util.Log.e("D", "ERROR in fillOutScramble moveIndex="+moveIndex);
352
    }
353

    
354
///////////////////////////////////////////////////////////////////////////////////////////////////
355

    
356
  private void printMoves()
357
    {
358
    for(int i=0; i<mNumMoves; i++)
359
      {
360
      android.util.Log.e("D", "move "+i+" : "+(mMoves[i]!=null ? " "+mMoves[i].getString() : " NULL") );
361
      }
362
    }
363

    
364
///////////////////////////////////////////////////////////////////////////////////////////////////
365

    
366
  private static String printBits(long id)
367
    {
368
    String ret = "[";
369
    boolean first = true;
370

    
371
    for(int i=0; i<64; i++)
372
      {
373
      if( ( (id>>i)&0x1)==1 )
374
        {
375
        String num = (i<10 ? " "+i : ""+i);
376

    
377
        if( first ) { ret += num; first=false; }
378
        else          ret += (","+num);
379
        }
380
      }
381

    
382
    return ret + "]";
383
    }
384
}
(4-4/5)