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;
|
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
|
|
50
|
mIsUnblocked = new boolean[3][MAX_SUPPORTED_SIZE];
|
51
|
|
52
|
mLayer[0] = x;
|
53
|
mLayer[1] = y;
|
54
|
mLayer[2] = z;
|
55
|
|
56
|
mTurns[0] = mLayer[1]==mLayer[2] ? 3:1;
|
57
|
mTurns[1] = mLayer[0]==mLayer[2] ? 3:1;
|
58
|
mTurns[2] = mLayer[0]==mLayer[1] ? 3:1;
|
59
|
|
60
|
int xMoves = mLayer[0]>1 ? mTurns[0]*mLayer[0] : 0;
|
61
|
int yMoves = mLayer[1]>1 ? mTurns[1]*mLayer[1] : 0;
|
62
|
int zMoves = mLayer[2]>1 ? mTurns[2]*mLayer[2] : 0;
|
63
|
|
64
|
mNumMoves = xMoves + yMoves + zMoves;
|
65
|
|
66
|
mStartX = 0;
|
67
|
mStartY = xMoves;
|
68
|
mStartZ = xMoves + yMoves;
|
69
|
|
70
|
mSignature = signature;
|
71
|
|
72
|
mMoves = createMoves();
|
73
|
|
74
|
|
75
|
android.util.Log.d("D", "sig: "+mSignature.getString() );
|
76
|
printMoves();
|
77
|
|
78
|
}
|
79
|
|
80
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
81
|
|
82
|
public ObjectSignature getSignature()
|
83
|
{
|
84
|
return mSignature;
|
85
|
}
|
86
|
|
87
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
88
|
|
89
|
public ObjectSignature getMove(int index)
|
90
|
{
|
91
|
return (index>=0 && index<mNumMoves) ? mMoves[index] : null;
|
92
|
}
|
93
|
|
94
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
95
|
|
96
|
public void removeMoves(ObjectSignature signature)
|
97
|
{
|
98
|
for(int m=0; m<mNumMoves; m++)
|
99
|
if( signature.isEqual(mMoves[m]) ) mMoves[m]=null;
|
100
|
}
|
101
|
|
102
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
103
|
|
104
|
public int numAxis()
|
105
|
{
|
106
|
int num = 0;
|
107
|
|
108
|
for(int x=mStartX; x<mStartY ; x++) if( mMoves[x]!=null ) { num++; break; }
|
109
|
for(int y=mStartY; y<mStartZ ; y++) if( mMoves[y]!=null ) { num++; break; }
|
110
|
for(int z=mStartZ; z<mNumMoves; z++) if( mMoves[z]!=null ) { num++; break; }
|
111
|
|
112
|
return num;
|
113
|
}
|
114
|
|
115
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
116
|
|
117
|
private int numXMoves()
|
118
|
{
|
119
|
int num=0;
|
120
|
for(int x=mStartX; x<mStartY; x++) if( mMoves[x]!=null ) num++;
|
121
|
return num;
|
122
|
}
|
123
|
|
124
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
125
|
|
126
|
private int numYMoves()
|
127
|
{
|
128
|
int num=0;
|
129
|
for(int y=mStartY; y<mStartZ; y++) if( mMoves[y]!=null ) num++;
|
130
|
return num;
|
131
|
}
|
132
|
|
133
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
134
|
|
135
|
private int numZMoves()
|
136
|
{
|
137
|
int num=0;
|
138
|
for(int z=mStartZ; z<mNumMoves; z++) if( mMoves[z]!=null ) num++;
|
139
|
return num;
|
140
|
}
|
141
|
|
142
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
143
|
|
144
|
public int numMoves()
|
145
|
{
|
146
|
return numXMoves()+numYMoves()+numZMoves();
|
147
|
}
|
148
|
|
149
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
150
|
|
151
|
public int numMoves(int excludedAxis)
|
152
|
{
|
153
|
switch(excludedAxis)
|
154
|
{
|
155
|
case AXIS_X: return numYMoves()+numZMoves();
|
156
|
case AXIS_Y: return numXMoves()+numZMoves();
|
157
|
case AXIS_Z: return numXMoves()+numYMoves();
|
158
|
}
|
159
|
|
160
|
return numXMoves()+numYMoves()+numZMoves();
|
161
|
}
|
162
|
|
163
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
164
|
|
165
|
public int getNthMove(int n, int excludedAxis)
|
166
|
{
|
167
|
int num = 0;
|
168
|
|
169
|
if( excludedAxis!=0 )
|
170
|
{
|
171
|
for(int m=mStartX; m<mStartY; m++)
|
172
|
if( mMoves[m]!=null )
|
173
|
{
|
174
|
if( num==n ) return m;
|
175
|
num++;
|
176
|
}
|
177
|
}
|
178
|
|
179
|
if( excludedAxis!=1 )
|
180
|
{
|
181
|
for(int m=mStartY; m<mStartZ; m++)
|
182
|
if( mMoves[m]!=null )
|
183
|
{
|
184
|
if( num==n ) return m;
|
185
|
num++;
|
186
|
}
|
187
|
}
|
188
|
|
189
|
if( excludedAxis!=2 )
|
190
|
{
|
191
|
for(int m=mStartZ; m<mNumMoves; m++)
|
192
|
if( mMoves[m]!=null )
|
193
|
{
|
194
|
if( num==n ) return m;
|
195
|
num++;
|
196
|
}
|
197
|
}
|
198
|
|
199
|
return -1;
|
200
|
}
|
201
|
|
202
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
203
|
|
204
|
private ObjectSignature[] createMoves()
|
205
|
{
|
206
|
ObjectSignature[] ret = new ObjectSignature[mNumMoves];
|
207
|
int index = 0;
|
208
|
|
209
|
for(int axis=0; axis<3; axis++)
|
210
|
for(int layer=0; layer<mLayer[axis]; layer++)
|
211
|
{
|
212
|
mIsUnblocked[axis][layer] = mSignature.isUnblockedFromLeft(axis,layer);
|
213
|
android.util.Log.e("D", "unblocked from left: axis="+axis+" layer="+layer+" val="+mIsUnblocked[axis][layer]);
|
214
|
}
|
215
|
|
216
|
for(int axis=0; axis<3; axis++)
|
217
|
if( mLayer[axis]>1 )
|
218
|
for(int turn=0; turn<mTurns[axis]; turn++)
|
219
|
{
|
220
|
boolean allLayersLocked = true;
|
221
|
|
222
|
for(int layer=0; layer<mLayer[axis]; layer++)
|
223
|
{
|
224
|
if( mIsUnblocked[axis][layer] )
|
225
|
{
|
226
|
if( layer>0 ) allLayersLocked = false;
|
227
|
ret[index] = mSignature.turn(axis,layer,turn);
|
228
|
}
|
229
|
else
|
230
|
{
|
231
|
ret[index] = ret[index-1].turn(axis,layer,turn);
|
232
|
ret[index-1] = null;
|
233
|
}
|
234
|
|
235
|
index++;
|
236
|
}
|
237
|
|
238
|
if( allLayersLocked ) ret[index-1] = null;
|
239
|
}
|
240
|
|
241
|
return ret;
|
242
|
}
|
243
|
|
244
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
245
|
|
246
|
private void printMoves()
|
247
|
{
|
248
|
for(int i=0; i<mNumMoves; i++)
|
249
|
{
|
250
|
android.util.Log.e("D", "move "+i+" : "+(mMoves[i]!=null ? " "+mMoves[i].getString() : " NULL") );
|
251
|
}
|
252
|
}
|
253
|
|
254
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
255
|
|
256
|
private static String printBits(long id)
|
257
|
{
|
258
|
String ret = "[";
|
259
|
boolean first = true;
|
260
|
|
261
|
for(int i=0; i<64; i++)
|
262
|
{
|
263
|
if( ( (id>>i)&0x1)==1 )
|
264
|
{
|
265
|
String num = (i<10 ? " "+i : ""+i);
|
266
|
|
267
|
if( first ) { ret += num; first=false; }
|
268
|
else ret += (","+num);
|
269
|
}
|
270
|
}
|
271
|
|
272
|
return ret + "]";
|
273
|
}
|
274
|
}
|