Project

General

Profile

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

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

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

    
77
    mMoves = createMoves();
78

    
79

    
80
android.util.Log.d("D", "sig: "+mSignature.getString() );
81
printMoves();
82

    
83
    }
84

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

    
87
  public ObjectSignature getSignature()
88
    {
89
    return mSignature;
90
    }
91

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93

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

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
  public void removeMoves(ObjectSignature signature)
102
    {
103
    for(int m=0; m<mNumMoves; m++)
104
      if( signature.isEqual(mMoves[m]) ) mMoves[m]=null;
105
    }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

    
109
  public int numAxis()
110
    {
111
    int num = 0;
112

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

    
117
    return num;
118
    }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

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

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

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

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

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

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

    
149
  public int numMoves()
150
    {
151
    return numXMoves()+numYMoves()+numZMoves();
152
    }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155

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

    
165
    return numXMoves()+numYMoves()+numZMoves();
166
    }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

    
170
  public int getNthMove(int n, int excludedAxis)
171
    {
172
    int num = 0;
173

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

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

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

    
204
    return -1;
205
    }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

    
209
  private ObjectSignature[] createMoves()
210
    {
211
    ObjectSignature[] ret = new ObjectSignature[mNumMoves];
212
    int index = 0;
213

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

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

    
227
          for(int layer=0; layer<mLayer[axis]; layer++)
228
            {
229
            if( mIsUnblocked[axis][layer] )
230
              {
231
              if( layer>0 ) allLayersLocked = false;
232
              ret[index] = mSignature.turn(axis,layer,turn);
233
              }
234
            else
235
              {
236
              ret[index] = ret[index-1].turn(axis,layer,turn);
237
              ret[index-1] = null;
238
              }
239

    
240
            index++;
241
            }
242

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

    
246
    return ret;
247
    }
248

    
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250

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

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

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

    
275
      moveIndex -= size;
276
      }
277

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

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

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

    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292

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

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

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

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