Project

General

Profile

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

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

1 10b7e306 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 1d581993 Leszek Koltunski
// Info about a scramble state of any bandaged cuboid.
24
25
import org.distorted.objectlib.helpers.ObjectSignature;
26 10b7e306 Leszek Koltunski
27 95123ad0 Leszek Koltunski
public class ScrambleStateBandagedCuboid
28 10b7e306 Leszek Koltunski
{
29 1d581993 Leszek Koltunski
  public static int MAX_SUPPORTED_SIZE = 5;
30
31 0e311558 Leszek Koltunski
  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 1d581993 Leszek Koltunski
  private final ObjectSignature[] mMoves;
37
  private final ObjectSignature mSignature;
38 338e42aa Leszek Koltunski
  private final int[] mLayer, mTurns, mSize;
39 1d581993 Leszek Koltunski
  private final int mNumMoves;
40
  private final int mStartX, mStartY, mStartZ;
41
  private final boolean[][] mIsUnblocked;
42 10b7e306 Leszek Koltunski
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44
45 1d581993 Leszek Koltunski
  public ScrambleStateBandagedCuboid(int x, int y, int z, ObjectSignature signature)
46 10b7e306 Leszek Koltunski
    {
47 1d581993 Leszek Koltunski
    mLayer = new int[3];
48
    mTurns = new int[3];
49 338e42aa Leszek Koltunski
    mSize  = new int[3];
50 1d581993 Leszek Koltunski
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 338e42aa Leszek Koltunski
    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 1d581993 Leszek Koltunski
69
    mNumMoves = xMoves + yMoves + zMoves;
70
71
    mStartX = 0;
72
    mStartY = xMoves;
73
    mStartZ = xMoves + yMoves;
74
75
    mSignature = signature;
76
    mMoves = createMoves();
77 10b7e306 Leszek Koltunski
    }
78
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80
81 1d581993 Leszek Koltunski
  public ObjectSignature getSignature()
82 5f54927b Leszek Koltunski
    {
83 1d581993 Leszek Koltunski
    return mSignature;
84 5f54927b Leszek Koltunski
    }
85
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87
88 1d581993 Leszek Koltunski
  public ObjectSignature getMove(int index)
89 5f54927b Leszek Koltunski
    {
90 1d581993 Leszek Koltunski
    return (index>=0 && index<mNumMoves) ? mMoves[index] : null;
91 5f54927b Leszek Koltunski
    }
92
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94
95 1d581993 Leszek Koltunski
  public void removeMoves(ObjectSignature signature)
96 5f54927b Leszek Koltunski
    {
97 1d581993 Leszek Koltunski
    for(int m=0; m<mNumMoves; m++)
98 671a53a2 Leszek Koltunski
      if( mMoves[m]!=null && signature.isEqual(mMoves[m]) ) mMoves[m]=null;
99 5f54927b Leszek Koltunski
    }
100
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
103 0e311558 Leszek Koltunski
  public int numAxis()
104 5f54927b Leszek Koltunski
    {
105
    int num = 0;
106
107 1d581993 Leszek Koltunski
    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 5f54927b Leszek Koltunski
111
    return num;
112
    }
113
114 0e311558 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
115
116
  private int numXMoves()
117
    {
118
    int num=0;
119 1d581993 Leszek Koltunski
    for(int x=mStartX; x<mStartY; x++) if( mMoves[x]!=null ) num++;
120 0e311558 Leszek Koltunski
    return num;
121
    }
122
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124
125
  private int numYMoves()
126
    {
127
    int num=0;
128 1d581993 Leszek Koltunski
    for(int y=mStartY; y<mStartZ; y++) if( mMoves[y]!=null ) num++;
129 0e311558 Leszek Koltunski
    return num;
130
    }
131
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133
134
  private int numZMoves()
135
    {
136
    int num=0;
137 1d581993 Leszek Koltunski
    for(int z=mStartZ; z<mNumMoves; z++) if( mMoves[z]!=null ) num++;
138 0e311558 Leszek Koltunski
    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 1d581993 Leszek Koltunski
    return numXMoves()+numYMoves()+numZMoves();
160 0e311558 Leszek Koltunski
    }
161
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
164
  public int getNthMove(int n, int excludedAxis)
165
    {
166
    int num = 0;
167
168 1d581993 Leszek Koltunski
    if( excludedAxis!=0 )
169 0e311558 Leszek Koltunski
      {
170 1d581993 Leszek Koltunski
      for(int m=mStartX; m<mStartY; m++)
171
        if( mMoves[m]!=null )
172 10b7e306 Leszek Koltunski
          {
173 1d581993 Leszek Koltunski
          if( num==n ) return m;
174
          num++;
175 10b7e306 Leszek Koltunski
          }
176
      }
177
178 1d581993 Leszek Koltunski
    if( excludedAxis!=1 )
179 10b7e306 Leszek Koltunski
      {
180 1d581993 Leszek Koltunski
      for(int m=mStartY; m<mStartZ; m++)
181
        if( mMoves[m]!=null )
182 9abfdef3 Leszek Koltunski
          {
183 1d581993 Leszek Koltunski
          if( num==n ) return m;
184
          num++;
185 9abfdef3 Leszek Koltunski
          }
186 39d97e73 Leszek Koltunski
      }
187 07c29a03 Leszek Koltunski
188 1d581993 Leszek Koltunski
    if( excludedAxis!=2 )
189 10b7e306 Leszek Koltunski
      {
190 1d581993 Leszek Koltunski
      for(int m=mStartZ; m<mNumMoves; m++)
191
        if( mMoves[m]!=null )
192 d12d901a Leszek Koltunski
          {
193 1d581993 Leszek Koltunski
          if( num==n ) return m;
194
          num++;
195 d12d901a Leszek Koltunski
          }
196
      }
197 10b7e306 Leszek Koltunski
198 1d581993 Leszek Koltunski
    return -1;
199 10b7e306 Leszek Koltunski
    }
200
201 3a5aa558 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 10b7e306 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
282
283 1d581993 Leszek Koltunski
  private ObjectSignature[] createMoves()
284 10b7e306 Leszek Koltunski
    {
285 1d581993 Leszek Koltunski
    ObjectSignature[] ret = new ObjectSignature[mNumMoves];
286 10b7e306 Leszek Koltunski
    int index = 0;
287
288
    for(int axis=0; axis<3; axis++)
289 1d581993 Leszek Koltunski
      for(int layer=0; layer<mLayer[axis]; layer++)
290 efeca8ef Leszek Koltunski
        {
291 1d581993 Leszek Koltunski
        mIsUnblocked[axis][layer] = mSignature.isUnblockedFromLeft(axis,layer);
292 efeca8ef Leszek Koltunski
        }
293 10b7e306 Leszek Koltunski
294 1d581993 Leszek Koltunski
    for(int axis=0; axis<3; axis++)
295
      if( mLayer[axis]>1 )
296 338e42aa Leszek Koltunski
        for(int turn=1; turn<=mTurns[axis]; turn++)
297 10b7e306 Leszek Koltunski
          {
298 1d581993 Leszek Koltunski
          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 10b7e306 Leszek Koltunski
          }
318
319
    return ret;
320
    }
321
322 338e42aa Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 5f54927b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
355
356 d12d901a Leszek Koltunski
  private void printMoves()
357 10b7e306 Leszek Koltunski
    {
358 1d581993 Leszek Koltunski
    for(int i=0; i<mNumMoves; i++)
359 d12d901a Leszek Koltunski
      {
360 efeca8ef Leszek Koltunski
      android.util.Log.e("D", "move "+i+" : "+(mMoves[i]!=null ? " "+mMoves[i].getString() : " NULL") );
361 d12d901a Leszek Koltunski
      }
362 10b7e306 Leszek Koltunski
    }
363
364
///////////////////////////////////////////////////////////////////////////////////////////////////
365
366 d12d901a Leszek Koltunski
  private static String printBits(long id)
367 10b7e306 Leszek Koltunski
    {
368 d12d901a Leszek Koltunski
    String ret = "[";
369
    boolean first = true;
370 10b7e306 Leszek Koltunski
371 d12d901a Leszek Koltunski
    for(int i=0; i<64; i++)
372 10b7e306 Leszek Koltunski
      {
373 d12d901a Leszek Koltunski
      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 10b7e306 Leszek Koltunski
      }
381
382 d12d901a Leszek Koltunski
    return ret + "]";
383 10b7e306 Leszek Koltunski
    }
384
}