Project

General

Profile

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

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

1 10b7e306 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6 babb7b08 Leszek Koltunski
// 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 10b7e306 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
9
10
package org.distorted.objectlib.scrambling;
11
12 e342c3f7 Leszek Koltunski
import org.distorted.objectlib.helpers.ObjectSignature;
13
14 10b7e306 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
15 1d581993 Leszek Koltunski
// Info about a scramble state of any bandaged cuboid.
16
17 95123ad0 Leszek Koltunski
public class ScrambleStateBandagedCuboid
18 10b7e306 Leszek Koltunski
{
19 818bca37 Leszek Koltunski
  public static int MAX_SUPPORTED_SIZE = 7;
20 1d581993 Leszek Koltunski
21 0e311558 Leszek Koltunski
  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 1d581993 Leszek Koltunski
  private final ObjectSignature[] mMoves;
27
  private final ObjectSignature mSignature;
28 338e42aa Leszek Koltunski
  private final int[] mLayer, mTurns, mSize;
29 1d581993 Leszek Koltunski
  private final int mNumMoves;
30
  private final int mStartX, mStartY, mStartZ;
31
  private final boolean[][] mIsUnblocked;
32 10b7e306 Leszek Koltunski
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34
35 e342c3f7 Leszek Koltunski
  public ScrambleStateBandagedCuboid(int x, int y, int z, ObjectSignature signature, BlacklistedSignatures blacklisted)
36 10b7e306 Leszek Koltunski
    {
37 1d581993 Leszek Koltunski
    mLayer = new int[3];
38
    mTurns = new int[3];
39 338e42aa Leszek Koltunski
    mSize  = new int[3];
40 1d581993 Leszek Koltunski
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 338e42aa Leszek Koltunski
    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 1d581993 Leszek Koltunski
59
    mNumMoves = xMoves + yMoves + zMoves;
60
61
    mStartX = 0;
62
    mStartY = xMoves;
63
    mStartZ = xMoves + yMoves;
64
65
    mSignature = signature;
66 e342c3f7 Leszek Koltunski
    mMoves = createMoves(blacklisted);
67 10b7e306 Leszek Koltunski
    }
68
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70
71 1d581993 Leszek Koltunski
  public ObjectSignature getSignature()
72 5f54927b Leszek Koltunski
    {
73 1d581993 Leszek Koltunski
    return mSignature;
74 5f54927b Leszek Koltunski
    }
75
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77
78 1d581993 Leszek Koltunski
  public ObjectSignature getMove(int index)
79 5f54927b Leszek Koltunski
    {
80 1d581993 Leszek Koltunski
    return (index>=0 && index<mNumMoves) ? mMoves[index] : null;
81 5f54927b Leszek Koltunski
    }
82
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84
85 1d581993 Leszek Koltunski
  public void removeMoves(ObjectSignature signature)
86 5f54927b Leszek Koltunski
    {
87 1d581993 Leszek Koltunski
    for(int m=0; m<mNumMoves; m++)
88 671a53a2 Leszek Koltunski
      if( mMoves[m]!=null && signature.isEqual(mMoves[m]) ) mMoves[m]=null;
89 5f54927b Leszek Koltunski
    }
90
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92
93 0e311558 Leszek Koltunski
  public int numAxis()
94 5f54927b Leszek Koltunski
    {
95
    int num = 0;
96
97 1d581993 Leszek Koltunski
    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 5f54927b Leszek Koltunski
101
    return num;
102
    }
103
104 0e311558 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
105
106
  private int numXMoves()
107
    {
108
    int num=0;
109 1d581993 Leszek Koltunski
    for(int x=mStartX; x<mStartY; x++) if( mMoves[x]!=null ) num++;
110 0e311558 Leszek Koltunski
    return num;
111
    }
112
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114
115
  private int numYMoves()
116
    {
117
    int num=0;
118 1d581993 Leszek Koltunski
    for(int y=mStartY; y<mStartZ; y++) if( mMoves[y]!=null ) num++;
119 0e311558 Leszek Koltunski
    return num;
120
    }
121
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123
124
  private int numZMoves()
125
    {
126
    int num=0;
127 1d581993 Leszek Koltunski
    for(int z=mStartZ; z<mNumMoves; z++) if( mMoves[z]!=null ) num++;
128 0e311558 Leszek Koltunski
    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 1d581993 Leszek Koltunski
    return numXMoves()+numYMoves()+numZMoves();
150 0e311558 Leszek Koltunski
    }
151
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153
154
  public int getNthMove(int n, int excludedAxis)
155
    {
156
    int num = 0;
157
158 1d581993 Leszek Koltunski
    if( excludedAxis!=0 )
159 0e311558 Leszek Koltunski
      {
160 1d581993 Leszek Koltunski
      for(int m=mStartX; m<mStartY; m++)
161
        if( mMoves[m]!=null )
162 10b7e306 Leszek Koltunski
          {
163 1d581993 Leszek Koltunski
          if( num==n ) return m;
164
          num++;
165 10b7e306 Leszek Koltunski
          }
166
      }
167
168 1d581993 Leszek Koltunski
    if( excludedAxis!=1 )
169 10b7e306 Leszek Koltunski
      {
170 1d581993 Leszek Koltunski
      for(int m=mStartY; m<mStartZ; m++)
171
        if( mMoves[m]!=null )
172 9abfdef3 Leszek Koltunski
          {
173 1d581993 Leszek Koltunski
          if( num==n ) return m;
174
          num++;
175 9abfdef3 Leszek Koltunski
          }
176 39d97e73 Leszek Koltunski
      }
177 07c29a03 Leszek Koltunski
178 1d581993 Leszek Koltunski
    if( excludedAxis!=2 )
179 10b7e306 Leszek Koltunski
      {
180 1d581993 Leszek Koltunski
      for(int m=mStartZ; m<mNumMoves; m++)
181
        if( mMoves[m]!=null )
182 d12d901a Leszek Koltunski
          {
183 1d581993 Leszek Koltunski
          if( num==n ) return m;
184
          num++;
185 d12d901a Leszek Koltunski
          }
186
      }
187 10b7e306 Leszek Koltunski
188 1d581993 Leszek Koltunski
    return -1;
189 10b7e306 Leszek Koltunski
    }
190
191 3a5aa558 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 10b7e306 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
272
273 e342c3f7 Leszek Koltunski
  private ObjectSignature[] createMoves(BlacklistedSignatures blacklisted)
274 10b7e306 Leszek Koltunski
    {
275 1d581993 Leszek Koltunski
    ObjectSignature[] ret = new ObjectSignature[mNumMoves];
276 10b7e306 Leszek Koltunski
    int index = 0;
277
278
    for(int axis=0; axis<3; axis++)
279 1d581993 Leszek Koltunski
      for(int layer=0; layer<mLayer[axis]; layer++)
280 efeca8ef Leszek Koltunski
        {
281 1d581993 Leszek Koltunski
        mIsUnblocked[axis][layer] = mSignature.isUnblockedFromLeft(axis,layer);
282 efeca8ef Leszek Koltunski
        }
283 10b7e306 Leszek Koltunski
284 1d581993 Leszek Koltunski
    for(int axis=0; axis<3; axis++)
285
      if( mLayer[axis]>1 )
286 338e42aa Leszek Koltunski
        for(int turn=1; turn<=mTurns[axis]; turn++)
287 10b7e306 Leszek Koltunski
          {
288 1d581993 Leszek Koltunski
          boolean allLayersLocked = true;
289 e342c3f7 Leszek Koltunski
          int begIndex = index;
290 1d581993 Leszek Koltunski
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 e342c3f7 Leszek Koltunski
          for(int i=begIndex; i<index; i++)
308
            {
309
            if( ret[i]!=null && blacklisted.contains(ret[i]) ) ret[i]=null;
310
            }
311
312 1d581993 Leszek Koltunski
          if( allLayersLocked ) ret[index-1] = null;
313 10b7e306 Leszek Koltunski
          }
314
315
    return ret;
316
    }
317
318 338e42aa Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 0cdd9b6c Leszek Koltunski
        scramble[1] = moveIndex % mSize[axis];
330 338e42aa Leszek Koltunski
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 5f54927b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
351
352 d12d901a Leszek Koltunski
  private void printMoves()
353 10b7e306 Leszek Koltunski
    {
354 1d581993 Leszek Koltunski
    for(int i=0; i<mNumMoves; i++)
355 d12d901a Leszek Koltunski
      {
356 efeca8ef Leszek Koltunski
      android.util.Log.e("D", "move "+i+" : "+(mMoves[i]!=null ? " "+mMoves[i].getString() : " NULL") );
357 d12d901a Leszek Koltunski
      }
358 10b7e306 Leszek Koltunski
    }
359
360
///////////////////////////////////////////////////////////////////////////////////////////////////
361
362 d12d901a Leszek Koltunski
  private static String printBits(long id)
363 10b7e306 Leszek Koltunski
    {
364 d12d901a Leszek Koltunski
    String ret = "[";
365
    boolean first = true;
366 10b7e306 Leszek Koltunski
367 d12d901a Leszek Koltunski
    for(int i=0; i<64; i++)
368 10b7e306 Leszek Koltunski
      {
369 d12d901a Leszek Koltunski
      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 10b7e306 Leszek Koltunski
      }
377
378 d12d901a Leszek Koltunski
    return ret + "]";
379 10b7e306 Leszek Koltunski
    }
380
}