1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2021 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.main;
|
21
|
|
22
|
import org.distorted.objectlib.helpers.ScrambleState;
|
23
|
|
24
|
import java.util.Random;
|
25
|
|
26
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
27
|
|
28
|
public class TwistyObjectScrambler
|
29
|
{
|
30
|
private final ScrambleState[] mStates;
|
31
|
private final int mType;
|
32
|
private final int mNumAxis;
|
33
|
private final int mNumLayers;
|
34
|
|
35
|
// type=0, i.e. main
|
36
|
private int mCurrState;
|
37
|
private int mIndexExcluded;
|
38
|
private int[][] mScrambleTable;
|
39
|
private int[] mNumOccurences;
|
40
|
|
41
|
// type=1, i.e. the exception: Square-1
|
42
|
private static final int BASIC_ANGLE = 12;
|
43
|
private static final int LAST_SL = 0; // automatic rotations: last rot was a 'slash' i.e. along ROT_AXIS[1]
|
44
|
private static final int LAST_UP = 1; // last rot was along ROT_AXIS[0], upper layer and forelast was a slash
|
45
|
private static final int LAST_LO = 2; // last rot was along ROT_AXIS[0], lower layer and forelast was a slash
|
46
|
private static final int LAST_UL = 3; // two last rots were along ROT_AXIS[0] (so the next must be a slash)
|
47
|
|
48
|
private int[][] mPermittedAngles;
|
49
|
private int[] mCornerQuat;
|
50
|
private int mPermittedUp, mPermittedDo;
|
51
|
private int[][] mBadCornerQuats;
|
52
|
private int mLastRot;
|
53
|
private int[][] mQuatMult;
|
54
|
|
55
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
56
|
|
57
|
TwistyObjectScrambler(int type, int numAxis, int numLayers, ScrambleState[] states)
|
58
|
{
|
59
|
mType = type;
|
60
|
mNumAxis = numAxis;
|
61
|
mNumLayers = numLayers;
|
62
|
mStates = states;
|
63
|
|
64
|
if( mType==1 )
|
65
|
{
|
66
|
mPermittedAngles = new int[2][BASIC_ANGLE];
|
67
|
mCornerQuat = new int[8];
|
68
|
mLastRot = LAST_SL;
|
69
|
}
|
70
|
}
|
71
|
|
72
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
73
|
// QUATS[i]*QUATS[j] = QUATS[QUAT_MULT[i][j]]
|
74
|
|
75
|
void initializeQuatMult()
|
76
|
{
|
77
|
mQuatMult = new int[][]
|
78
|
{
|
79
|
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,},
|
80
|
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 12,},
|
81
|
{ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 12, 13,},
|
82
|
{ 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 15, 16, 17, 18, 19, 20, 21, 22, 23, 12, 13, 14,},
|
83
|
{ 4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 16, 17, 18, 19, 20, 21, 22, 23, 12, 13, 14, 15,},
|
84
|
{ 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 17, 18, 19, 20, 21, 22, 23, 12, 13, 14, 15, 16,},
|
85
|
{ 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 18, 19, 20, 21, 22, 23, 12, 13, 14, 15, 16, 17,},
|
86
|
{ 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 6, 19, 20, 21, 22, 23, 12, 13, 14, 15, 16, 17, 18,},
|
87
|
{ 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 6, 7, 20, 21, 22, 23, 12, 13, 14, 15, 16, 17, 18, 19,},
|
88
|
{ 9, 10, 11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 21, 22, 23, 12, 13, 14, 15, 16, 17, 18, 19, 20,},
|
89
|
{ 10, 11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 22, 23, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,},
|
90
|
{ 11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 23, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,},
|
91
|
{ 12, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 0, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1,},
|
92
|
{ 13, 12, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 1, 0, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2,},
|
93
|
{ 14, 13, 12, 23, 22, 21, 20, 19, 18, 17, 16, 15, 2, 1, 0, 11, 10, 9, 8, 7, 6, 5, 4, 3,},
|
94
|
{ 15, 14, 13, 12, 23, 22, 21, 20, 19, 18, 17, 16, 3, 2, 1, 0, 11, 10, 9, 8, 7, 6, 5, 4,},
|
95
|
{ 16, 15, 14, 13, 12, 23, 22, 21, 20, 19, 18, 17, 4, 3, 2, 1, 0, 11, 10, 9, 8, 7, 6, 5,},
|
96
|
{ 17, 16, 15, 14, 13, 12, 23, 22, 21, 20, 19, 18, 5, 4, 3, 2, 1, 0, 11, 10, 9, 8, 7, 6,},
|
97
|
{ 18, 17, 16, 15, 14, 13, 12, 23, 22, 21, 20, 19, 6, 5, 4, 3, 2, 1, 0, 11, 10, 9, 8, 7,},
|
98
|
{ 19, 18, 17, 16, 15, 14, 13, 12, 23, 22, 21, 20, 7, 6, 5, 4, 3, 2, 1, 0, 11, 10, 9, 8,},
|
99
|
{ 20, 19, 18, 17, 16, 15, 14, 13, 12, 23, 22, 21, 8, 7, 6, 5, 4, 3, 2, 1, 0, 11, 10, 9,},
|
100
|
{ 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 23, 22, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 11, 10,},
|
101
|
{ 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 23, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 11,},
|
102
|
{ 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,}
|
103
|
};
|
104
|
}
|
105
|
|
106
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
107
|
|
108
|
private void initializeScrambling()
|
109
|
{
|
110
|
if( mScrambleTable ==null )
|
111
|
{
|
112
|
mScrambleTable = new int[mNumAxis][mNumLayers];
|
113
|
}
|
114
|
if( mNumOccurences ==null )
|
115
|
{
|
116
|
int max=0;
|
117
|
|
118
|
for (ScrambleState mState : mStates)
|
119
|
{
|
120
|
int tmp = mState.getTotal(-1);
|
121
|
if (max < tmp) max = tmp;
|
122
|
}
|
123
|
|
124
|
mNumOccurences = new int[max];
|
125
|
}
|
126
|
|
127
|
for(int i=0; i<mNumAxis; i++)
|
128
|
for(int j=0; j<mNumLayers; j++) mScrambleTable[i][j] = 0;
|
129
|
}
|
130
|
|
131
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
132
|
// PUBLIC API
|
133
|
|
134
|
private void randomizeNewScramble0(int[][] scramble, Random rnd, int curr, int total)
|
135
|
{
|
136
|
if( curr==0 )
|
137
|
{
|
138
|
mCurrState = 0;
|
139
|
mIndexExcluded =-1;
|
140
|
initializeScrambling();
|
141
|
}
|
142
|
|
143
|
int[] info= mStates[mCurrState].getRandom(rnd, mIndexExcluded, mScrambleTable, mNumOccurences);
|
144
|
|
145
|
scramble[curr][0] = info[0];
|
146
|
scramble[curr][1] = info[1];
|
147
|
scramble[curr][2] = info[2];
|
148
|
|
149
|
mCurrState = info[3];
|
150
|
mIndexExcluded = info[0];
|
151
|
}
|
152
|
|
153
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
154
|
|
155
|
private boolean cornerIsUp(int index)
|
156
|
{
|
157
|
return ((index<4) ^ (mCornerQuat[index]>=12));
|
158
|
}
|
159
|
|
160
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
161
|
|
162
|
private boolean cornerIsLeft(int index)
|
163
|
{
|
164
|
int q = mCornerQuat[index];
|
165
|
|
166
|
switch(index)
|
167
|
{
|
168
|
case 0:
|
169
|
case 4: return ((q>=3 && q<= 7) || (q>=18 && q<=22));
|
170
|
case 1:
|
171
|
case 5: return ((q>=6 && q<=10) || (q>=15 && q<=19));
|
172
|
case 2:
|
173
|
case 6: return ((q==0 || q==1 || (q>=9 && q<=11)) || (q>=12 && q<=16));
|
174
|
case 3:
|
175
|
case 7: return ((q>=0 && q<=4) || (q==12 || q==13 || (q>=21 && q<=23)));
|
176
|
}
|
177
|
|
178
|
return false;
|
179
|
}
|
180
|
|
181
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
182
|
|
183
|
private boolean quatIsBad(int quatIndex, int corner)
|
184
|
{
|
185
|
if( mBadCornerQuats ==null )
|
186
|
{
|
187
|
// quat indices that make corner cubits bandage the puzzle.
|
188
|
mBadCornerQuats = new int[][] { { 2, 8,17,23}, { 5,11,14,20} };
|
189
|
}
|
190
|
|
191
|
int index = (corner%2);
|
192
|
|
193
|
return ( quatIndex== mBadCornerQuats[index][0] ||
|
194
|
quatIndex== mBadCornerQuats[index][1] ||
|
195
|
quatIndex== mBadCornerQuats[index][2] ||
|
196
|
quatIndex== mBadCornerQuats[index][3] );
|
197
|
}
|
198
|
|
199
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
200
|
|
201
|
private boolean isPermittedDo(int angle)
|
202
|
{
|
203
|
if( mQuatMult==null ) initializeQuatMult();
|
204
|
|
205
|
for(int corner=0; corner<8; corner++)
|
206
|
{
|
207
|
if( !cornerIsUp(corner) )
|
208
|
{
|
209
|
int currQuat = mCornerQuat[corner];
|
210
|
int finalQuat= mQuatMult[angle][currQuat];
|
211
|
if( quatIsBad(finalQuat,corner) ) return false;
|
212
|
}
|
213
|
}
|
214
|
|
215
|
return true;
|
216
|
}
|
217
|
|
218
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
219
|
|
220
|
private boolean isPermittedUp(int angle)
|
221
|
{
|
222
|
if( mQuatMult==null ) initializeQuatMult();
|
223
|
|
224
|
for(int corner=0; corner<8; corner++)
|
225
|
{
|
226
|
if( cornerIsUp(corner) )
|
227
|
{
|
228
|
int currQuat = mCornerQuat[corner];
|
229
|
int finalQuat= mQuatMult[angle][currQuat];
|
230
|
if( quatIsBad(finalQuat,corner) ) return false;
|
231
|
}
|
232
|
}
|
233
|
|
234
|
return true;
|
235
|
}
|
236
|
|
237
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
238
|
|
239
|
private void computePermittedAngles()
|
240
|
{
|
241
|
mPermittedDo = 0;
|
242
|
|
243
|
for(int angle=0; angle<BASIC_ANGLE; angle++)
|
244
|
{
|
245
|
if( isPermittedDo(angle ) ) mPermittedAngles[0][mPermittedDo++] = angle;
|
246
|
}
|
247
|
|
248
|
mPermittedUp = 0;
|
249
|
|
250
|
for(int angle=0; angle<BASIC_ANGLE; angle++)
|
251
|
{
|
252
|
if( isPermittedUp(angle ) ) mPermittedAngles[1][mPermittedUp++] = angle;
|
253
|
}
|
254
|
}
|
255
|
|
256
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
257
|
|
258
|
private int getNextAngle(Random rnd, int layer)
|
259
|
{
|
260
|
int num = layer==0 ? mPermittedDo:mPermittedUp;
|
261
|
int index = rnd.nextInt(num);
|
262
|
int angle = mPermittedAngles[layer][index];
|
263
|
return angle<BASIC_ANGLE/2 ? -angle : BASIC_ANGLE-angle;
|
264
|
}
|
265
|
|
266
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
267
|
|
268
|
private int getNextAngleNotZero(Random rnd, int layer)
|
269
|
{
|
270
|
int num = layer==0 ? mPermittedDo:mPermittedUp;
|
271
|
int index = rnd.nextInt(num-1);
|
272
|
int angle = mPermittedAngles[layer][index+1];
|
273
|
return angle<BASIC_ANGLE/2 ? -angle : BASIC_ANGLE-angle;
|
274
|
}
|
275
|
|
276
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
277
|
|
278
|
private int makeQuat(int axis,int index)
|
279
|
{
|
280
|
if( axis==1 ) return 13;
|
281
|
if( index<0 ) index+=12;
|
282
|
return index;
|
283
|
}
|
284
|
|
285
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
286
|
|
287
|
private boolean cornerBelongs(int index, int axis, int layer)
|
288
|
{
|
289
|
if( axis==0 )
|
290
|
{
|
291
|
boolean up = cornerIsUp(index);
|
292
|
return ((up && layer==2) || (!up && layer==0));
|
293
|
}
|
294
|
else
|
295
|
{
|
296
|
boolean le = cornerIsLeft(index);
|
297
|
return ((le && layer==0) || (!le && layer==1));
|
298
|
}
|
299
|
}
|
300
|
|
301
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
302
|
|
303
|
private void updateCornerQuats(int[] rotInfo)
|
304
|
{
|
305
|
if( mQuatMult==null ) initializeQuatMult();
|
306
|
|
307
|
int axis = rotInfo[0];
|
308
|
int layer= rotInfo[1];
|
309
|
int index=-rotInfo[2];
|
310
|
|
311
|
int quat = makeQuat(axis,index);
|
312
|
|
313
|
for(int corner=0; corner<8; corner++)
|
314
|
{
|
315
|
if( cornerBelongs(corner,axis,layer) )
|
316
|
{
|
317
|
int curr = mCornerQuat[corner];
|
318
|
mCornerQuat[corner] = mQuatMult[quat][curr];
|
319
|
}
|
320
|
}
|
321
|
}
|
322
|
|
323
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
324
|
|
325
|
private void randomizeNewScramble1(int[][] scramble, Random rnd, int curr, int total)
|
326
|
{
|
327
|
int layer, nextAngle;
|
328
|
|
329
|
if( curr==0 )
|
330
|
{
|
331
|
for(int corner=0; corner<8; corner++) mCornerQuat[corner] = 0;
|
332
|
mLastRot = rnd.nextInt(4);
|
333
|
computePermittedAngles();
|
334
|
}
|
335
|
|
336
|
switch(mLastRot)
|
337
|
{
|
338
|
case LAST_SL: layer = rnd.nextInt(2);
|
339
|
nextAngle = getNextAngle(rnd,layer);
|
340
|
|
341
|
if( nextAngle==0 )
|
342
|
{
|
343
|
layer = 1-layer;
|
344
|
nextAngle = getNextAngleNotZero(rnd,layer);
|
345
|
}
|
346
|
|
347
|
scramble[curr][0] = 0;
|
348
|
scramble[curr][1] = 2*layer;
|
349
|
scramble[curr][2] = nextAngle;
|
350
|
mLastRot = layer==0 ? LAST_LO : LAST_UP;
|
351
|
updateCornerQuats(scramble[curr]);
|
352
|
break;
|
353
|
case LAST_LO:
|
354
|
case LAST_UP: layer = mLastRot==LAST_LO ? 1:0;
|
355
|
nextAngle = getNextAngle(rnd,layer);
|
356
|
|
357
|
if( nextAngle!=0 )
|
358
|
{
|
359
|
scramble[curr][0] = 0;
|
360
|
scramble[curr][1] = 2*layer;
|
361
|
scramble[curr][2] = nextAngle;
|
362
|
updateCornerQuats(scramble[curr]);
|
363
|
mLastRot = LAST_UL;
|
364
|
}
|
365
|
else
|
366
|
{
|
367
|
scramble[curr][0] = 1;
|
368
|
scramble[curr][1] = rnd.nextInt(2);
|
369
|
scramble[curr][2] = 1;
|
370
|
mLastRot = LAST_SL;
|
371
|
updateCornerQuats(scramble[curr]);
|
372
|
computePermittedAngles();
|
373
|
}
|
374
|
|
375
|
break;
|
376
|
case LAST_UL: scramble[curr][0] = 1;
|
377
|
scramble[curr][1] = rnd.nextInt(2);
|
378
|
scramble[curr][2] = 1;
|
379
|
mLastRot = LAST_SL;
|
380
|
updateCornerQuats(scramble[curr]);
|
381
|
computePermittedAngles();
|
382
|
break;
|
383
|
}
|
384
|
}
|
385
|
|
386
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
387
|
// PUBLIC API
|
388
|
|
389
|
public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
|
390
|
{
|
391
|
if( mType==0 ) randomizeNewScramble0(scramble, rnd, curr, total);
|
392
|
if( mType==1 ) randomizeNewScramble1(scramble, rnd, curr, total);
|
393
|
}
|
394
|
}
|