Project

General

Profile

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

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

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
android.util.Log.d("D", "sig: "+mSignature.getString() );
79
printMoves();
80
    }
81

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83

    
84
  public ObjectSignature getSignature()
85
    {
86
    return mSignature;
87
    }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

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

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

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

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

    
106
  public int numAxis()
107
    {
108
    int num = 0;
109

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

    
114
    return num;
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

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

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

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

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

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

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145

    
146
  public int numMoves()
147
    {
148
    return numXMoves()+numYMoves()+numZMoves();
149
    }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

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

    
162
    return numXMoves()+numYMoves()+numZMoves();
163
    }
164

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

    
167
  public int getNthMove(int n, int excludedAxis)
168
    {
169
    int num = 0;
170

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

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

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

    
201
    return -1;
202
    }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

    
206
  private ObjectSignature[] createMoves()
207
    {
208
    ObjectSignature[] ret = new ObjectSignature[mNumMoves];
209
    int index = 0;
210

    
211
    for(int axis=0; axis<3; axis++)
212
      for(int layer=0; layer<mLayer[axis]; layer++)
213
        {
214
        mIsUnblocked[axis][layer] = mSignature.isUnblockedFromLeft(axis,layer);
215
        android.util.Log.e("D", "unblocked from left: axis="+axis+" layer="+layer+" val="+mIsUnblocked[axis][layer]);
216
        }
217

    
218
    for(int axis=0; axis<3; axis++)
219
      if( mLayer[axis]>1 )
220
        for(int turn=1; turn<=mTurns[axis]; turn++)
221
          {
222
          boolean allLayersLocked = true;
223

    
224
          for(int layer=0; layer<mLayer[axis]; layer++)
225
            {
226
            if( mIsUnblocked[axis][layer] )
227
              {
228
              if( layer>0 ) allLayersLocked = false;
229
              ret[index] = mSignature.turn(axis,layer,turn);
230

    
231
              android.util.Log.d("D", "turning "+mSignature.getString());
232
              android.util.Log.d("D", "axis="+axis+" layer="+layer+" turn="+turn);
233
              android.util.Log.d("D", "result "+ret[index].getString());
234
              }
235
            else
236
              {
237
              ret[index] = ret[index-1].turn(axis,layer,turn);
238
              ret[index-1] = null;
239
              }
240

    
241
            index++;
242
            }
243

    
244
          if( allLayersLocked ) ret[index-1] = null;
245
          }
246

    
247
    return ret;
248
    }
249

    
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251

    
252
  public void fillOutScramble(int[] scramble, int moveIndex)
253
    {
254
    for(int axis=0; axis<3; axis++)
255
      {
256
      int size = mTurns[axis]*mSize[axis];
257

    
258
      if( moveIndex<size )
259
        {
260
        scramble[0] = axis;
261
        scramble[1] = moveIndex % mSize[axis];
262

    
263
        if( mTurns[axis]==3 )
264
          {
265
          switch(moveIndex/mSize[axis])
266
            {
267
            case 0: scramble[2] =-1; break;
268
            case 1: scramble[2] = 2; break;
269
            case 2: scramble[2] = 1; break;
270
            }
271
          }
272
        else scramble[2] = 1;
273
        return;
274
        }
275

    
276
      moveIndex -= size;
277
      }
278

    
279
    android.util.Log.e("D", "ERROR in fillOutScramble moveIndex="+moveIndex);
280
    }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283

    
284
  private void printMoves()
285
    {
286
    for(int i=0; i<mNumMoves; i++)
287
      {
288
      android.util.Log.e("D", "move "+i+" : "+(mMoves[i]!=null ? " "+mMoves[i].getString() : " NULL") );
289
      }
290
    }
291

    
292
///////////////////////////////////////////////////////////////////////////////////////////////////
293

    
294
  private static String printBits(long id)
295
    {
296
    String ret = "[";
297
    boolean first = true;
298

    
299
    for(int i=0; i<64; i++)
300
      {
301
      if( ( (id>>i)&0x1)==1 )
302
        {
303
        String num = (i<10 ? " "+i : ""+i);
304

    
305
        if( first ) { ret += num; first=false; }
306
        else          ret += (","+num);
307
        }
308
      }
309

    
310
    return ret + "]";
311
    }
312
}
(4-4/5)