Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.objectlib.scrambling;
11

    
12
import org.distorted.objectlib.helpers.ObjectSignature;
13

    
14
///////////////////////////////////////////////////////////////////////////////////////////////////
15
// Info about a scramble state of any bandaged cuboid.
16

    
17
public class ScrambleStateBandagedCuboid
18
{
19
  public static int MAX_SUPPORTED_SIZE = 7;
20

    
21
  public static final int AXIS_NONE = -1;
22
  public static final int AXIS_X = 0;
23
  public static final int AXIS_Y = 1;
24
  public static final int AXIS_Z = 2;
25

    
26
  private final ObjectSignature[] mMoves;
27
  private final ObjectSignature mSignature;
28
  private final int[] mLayer, mTurns, mSize;
29
  private final int mNumMoves;
30
  private final int mStartX, mStartY, mStartZ;
31
  private final boolean[][] mIsUnblocked;
32

    
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34

    
35
  public ScrambleStateBandagedCuboid(int x, int y, int z, ObjectSignature signature, BlacklistedSignatures blacklisted)
36
    {
37
    mLayer = new int[3];
38
    mTurns = new int[3];
39
    mSize  = new int[3];
40

    
41
    mIsUnblocked = new boolean[3][MAX_SUPPORTED_SIZE];
42

    
43
    mLayer[0] = x;
44
    mLayer[1] = y;
45
    mLayer[2] = z;
46

    
47
    mTurns[0] = mLayer[1]==mLayer[2] ? 3:1;
48
    mTurns[1] = mLayer[0]==mLayer[2] ? 3:1;
49
    mTurns[2] = mLayer[0]==mLayer[1] ? 3:1;
50

    
51
    mSize[0] = (mLayer[0]>1 ? mLayer[0] : 0);
52
    mSize[1] = (mLayer[1]>1 ? mLayer[1] : 0);
53
    mSize[2] = (mLayer[2]>1 ? mLayer[2] : 0);
54

    
55
    int xMoves = mTurns[0]*mSize[0];
56
    int yMoves = mTurns[1]*mSize[1];
57
    int zMoves = mTurns[2]*mSize[2];
58

    
59
    mNumMoves = xMoves + yMoves + zMoves;
60

    
61
    mStartX = 0;
62
    mStartY = xMoves;
63
    mStartZ = xMoves + yMoves;
64

    
65
    mSignature = signature;
66
    mMoves = createMoves(blacklisted);
67
    }
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

    
71
  public ObjectSignature getSignature()
72
    {
73
    return mSignature;
74
    }
75

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

    
78
  public ObjectSignature getMove(int index)
79
    {
80
    return (index>=0 && index<mNumMoves) ? mMoves[index] : null;
81
    }
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

    
85
  public void removeMoves(ObjectSignature signature)
86
    {
87
    for(int m=0; m<mNumMoves; m++)
88
      if( mMoves[m]!=null && signature.isEqual(mMoves[m]) ) mMoves[m]=null;
89
    }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

    
93
  public int numAxis()
94
    {
95
    int num = 0;
96

    
97
    for(int x=mStartX; x<mStartY  ; x++) if( mMoves[x]!=null ) { num++; break; }
98
    for(int y=mStartY; y<mStartZ  ; y++) if( mMoves[y]!=null ) { num++; break; }
99
    for(int z=mStartZ; z<mNumMoves; z++) if( mMoves[z]!=null ) { num++; break; }
100

    
101
    return num;
102
    }
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

    
106
  private int numXMoves()
107
    {
108
    int num=0;
109
    for(int x=mStartX; x<mStartY; x++) if( mMoves[x]!=null ) num++;
110
    return num;
111
    }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114

    
115
  private int numYMoves()
116
    {
117
    int num=0;
118
    for(int y=mStartY; y<mStartZ; y++) if( mMoves[y]!=null ) num++;
119
    return num;
120
    }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

    
124
  private int numZMoves()
125
    {
126
    int num=0;
127
    for(int z=mStartZ; z<mNumMoves; z++) if( mMoves[z]!=null ) num++;
128
    return num;
129
    }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

    
133
  public int numMoves()
134
    {
135
    return numXMoves()+numYMoves()+numZMoves();
136
    }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

    
140
  public int numMoves(int excludedAxis)
141
    {
142
    switch(excludedAxis)
143
      {
144
      case AXIS_X: return numYMoves()+numZMoves();
145
      case AXIS_Y: return numXMoves()+numZMoves();
146
      case AXIS_Z: return numXMoves()+numYMoves();
147
      }
148

    
149
    return numXMoves()+numYMoves()+numZMoves();
150
    }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
  public int getNthMove(int n, int excludedAxis)
155
    {
156
    int num = 0;
157

    
158
    if( excludedAxis!=0 )
159
      {
160
      for(int m=mStartX; m<mStartY; m++)
161
        if( mMoves[m]!=null )
162
          {
163
          if( num==n ) return m;
164
          num++;
165
          }
166
      }
167

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

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

    
188
    return -1;
189
    }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

    
193
  private int numXMoves(int excludedLayer)
194
    {
195
    int num=0;
196
    for(int x=mStartX; x<mStartY; x++) if( mMoves[x]!=null && (x-mStartX)%mLayer[0]!=excludedLayer ) num++;
197
    return num;
198
    }
199

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

    
202
  private int numYMoves(int excludedLayer)
203
    {
204
    int num=0;
205
    for(int y=mStartY; y<mStartZ; y++) if( mMoves[y]!=null && (y-mStartY)%mLayer[1]!=excludedLayer ) num++;
206
    return num;
207
    }
208

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210

    
211
  private int numZMoves(int excludedLayer)
212
    {
213
    int num=0;
214
    for(int z=mStartZ; z<mNumMoves; z++) if( mMoves[z]!=null && (z-mStartZ)%mLayer[2]!=excludedLayer ) num++;
215
    return num;
216
    }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

    
220
  public int numMoves(int excludedAxis, int excludedLayer)
221
    {
222
    switch(excludedAxis)
223
      {
224
      case AXIS_X: return numYMoves(excludedLayer)+numZMoves(excludedLayer);
225
      case AXIS_Y: return numXMoves(excludedLayer)+numZMoves(excludedLayer);
226
      case AXIS_Z: return numXMoves(excludedLayer)+numYMoves(excludedLayer);
227
      }
228

    
229
    return numXMoves(excludedLayer)+numYMoves(excludedLayer)+numZMoves(excludedLayer);
230
    }
231

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

    
234
  public int getNthMove(int n, int excludedAxis, int excludedLayer)
235
    {
236
    int num = 0;
237

    
238
    if( excludedAxis!=0 )
239
      {
240
      for(int m=mStartX; m<mStartY; m++)
241
        if( mMoves[m]!=null && (m-mStartX)%mLayer[0]!=excludedLayer )
242
          {
243
          if( num==n ) return m;
244
          num++;
245
          }
246
      }
247

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

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

    
268
    return -1;
269
    }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
  private ObjectSignature[] createMoves(BlacklistedSignatures blacklisted)
274
    {
275
    ObjectSignature[] ret = new ObjectSignature[mNumMoves];
276
    int index = 0;
277

    
278
    for(int axis=0; axis<3; axis++)
279
      for(int layer=0; layer<mLayer[axis]; layer++)
280
        {
281
        mIsUnblocked[axis][layer] = mSignature.isUnblockedFromLeft(axis,layer);
282
        }
283

    
284
    for(int axis=0; axis<3; axis++)
285
      if( mLayer[axis]>1 )
286
        for(int turn=1; turn<=mTurns[axis]; turn++)
287
          {
288
          boolean allLayersLocked = true;
289
          int begIndex = index;
290

    
291
          for(int layer=0; layer<mLayer[axis]; layer++)
292
            {
293
            if( mIsUnblocked[axis][layer] )
294
              {
295
              if( layer>0 ) allLayersLocked = false;
296
              ret[index] = mSignature.turn(axis,layer,turn);
297
              }
298
            else
299
              {
300
              ret[index] = ret[index-1].turn(axis,layer,turn);
301
              ret[index-1] = null;
302
              }
303

    
304
            index++;
305
            }
306

    
307
          for(int i=begIndex; i<index; i++)
308
            {
309
            if( ret[i]!=null && blacklisted.contains(ret[i]) ) ret[i]=null;
310
            }
311

    
312
          if( allLayersLocked ) ret[index-1] = null;
313
          }
314

    
315
    return ret;
316
    }
317

    
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319

    
320
  public void fillOutScramble(int[] scramble, int moveIndex)
321
    {
322
    for(int axis=0; axis<3; axis++)
323
      {
324
      int size = mTurns[axis]*mSize[axis];
325

    
326
      if( moveIndex<size )
327
        {
328
        scramble[0] = axis;
329
        scramble[1] = moveIndex % mSize[axis];
330

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

    
344
      moveIndex -= size;
345
      }
346

    
347
    android.util.Log.e("D", "ERROR in fillOutScramble moveIndex="+moveIndex);
348
    }
349

    
350
///////////////////////////////////////////////////////////////////////////////////////////////////
351

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

    
360
///////////////////////////////////////////////////////////////////////////////////////////////////
361

    
362
  private static String printBits(long id)
363
    {
364
    String ret = "[";
365
    boolean first = true;
366

    
367
    for(int i=0; i<64; i++)
368
      {
369
      if( ( (id>>i)&0x1)==1 )
370
        {
371
        String num = (i<10 ? " "+i : ""+i);
372

    
373
        if( first ) { ret += num; first=false; }
374
        else          ret += (","+num);
375
        }
376
      }
377

    
378
    return ret + "]";
379
    }
380
}
(6-6/7)