Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / TwistyObjectScrambler.java @ a57e6870

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][];
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
      {
129
      int len = mNumLayers[i];
130
      mScrambleTable[i] = new int[len];
131
      for(int j=0; j<len; j++) mScrambleTable[i][j] = 0;
132
      }
133
    }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136
// PUBLIC API
137

    
138
  private void randomizeNewScramble0(int[][] scramble, Random rnd, int curr, int total)
139
    {
140
    if( curr==0 )
141
      {
142
      mCurrState     = 0;
143
      mIndexExcluded =-1;
144
      initializeScrambling();
145
      }
146

    
147
    int[] info= mStates[mCurrState].getRandom(rnd, mIndexExcluded, mScrambleTable, mNumOccurences);
148

    
149
    scramble[curr][0] = info[0];
150
    scramble[curr][1] = info[1];
151
    scramble[curr][2] = info[2];
152

    
153
    mCurrState     = info[3];
154
    mIndexExcluded = info[0];
155
    }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

    
159
  private boolean cornerIsUp(int index)
160
    {
161
    return ((index<4) ^ (mCornerQuat[index]>=12));
162
    }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
  private boolean cornerIsLeft(int index)
167
    {
168
    int q = mCornerQuat[index];
169

    
170
    switch(index)
171
      {
172
      case 0:
173
      case 4: return ((q>=3 && q<= 7) || (q>=18 && q<=22));
174
      case 1:
175
      case 5: return ((q>=6 && q<=10) || (q>=15 && q<=19));
176
      case 2:
177
      case 6: return ((q==0 || q==1 || (q>=9 && q<=11)) || (q>=12 && q<=16));
178
      case 3:
179
      case 7: return ((q>=0 && q<=4) || (q==12 || q==13 || (q>=21 && q<=23)));
180
      }
181

    
182
    return false;
183
    }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

    
187
  private boolean quatIsBad(int quatIndex, int corner)
188
    {
189
    if( mBadCornerQuats ==null )
190
      {
191
      // quat indices that make corner cubits bandage the puzzle.
192
      mBadCornerQuats = new int[][] { { 2, 8,17,23}, { 5,11,14,20} };
193
      }
194

    
195
    int index = (corner%2);
196

    
197
    return ( quatIndex== mBadCornerQuats[index][0] ||
198
             quatIndex== mBadCornerQuats[index][1] ||
199
             quatIndex== mBadCornerQuats[index][2] ||
200
             quatIndex== mBadCornerQuats[index][3]  );
201
    }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204

    
205
  private boolean isPermittedDo(int angle)
206
    {
207
    if( mQuatMult==null ) initializeQuatMult();
208

    
209
    for(int corner=0; corner<8; corner++)
210
      {
211
      if( !cornerIsUp(corner) )
212
        {
213
        int currQuat = mCornerQuat[corner];
214
        int finalQuat= mQuatMult[angle][currQuat];
215
        if( quatIsBad(finalQuat,corner) ) return false;
216
        }
217
      }
218

    
219
    return true;
220
    }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

    
224
  private boolean isPermittedUp(int angle)
225
    {
226
    if( mQuatMult==null ) initializeQuatMult();
227

    
228
    for(int corner=0; corner<8; corner++)
229
      {
230
      if( cornerIsUp(corner) )
231
        {
232
        int currQuat = mCornerQuat[corner];
233
        int finalQuat= mQuatMult[angle][currQuat];
234
        if( quatIsBad(finalQuat,corner) ) return false;
235
        }
236
      }
237

    
238
    return true;
239
    }
240

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

    
243
  private void computePermittedAngles()
244
    {
245
    mPermittedDo = 0;
246

    
247
    for(int angle=0; angle<BASIC_ANGLE; angle++)
248
      {
249
      if( isPermittedDo(angle ) ) mPermittedAngles[0][mPermittedDo++] = angle;
250
      }
251

    
252
    mPermittedUp = 0;
253

    
254
    for(int angle=0; angle<BASIC_ANGLE; angle++)
255
      {
256
      if( isPermittedUp(angle ) ) mPermittedAngles[1][mPermittedUp++] = angle;
257
      }
258
    }
259

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261

    
262
  private int getNextAngle(Random rnd, int layer)
263
    {
264
    int num = layer==0 ? mPermittedDo:mPermittedUp;
265
    int index = rnd.nextInt(num);
266
    int angle = mPermittedAngles[layer][index];
267
    return angle<BASIC_ANGLE/2 ? -angle : BASIC_ANGLE-angle;
268
    }
269

    
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271

    
272
  private int getNextAngleNotZero(Random rnd, int layer)
273
    {
274
    int num = layer==0 ? mPermittedDo:mPermittedUp;
275
    int index = rnd.nextInt(num-1);
276
    int angle = mPermittedAngles[layer][index+1];
277
    return angle<BASIC_ANGLE/2 ? -angle : BASIC_ANGLE-angle;
278
    }
279

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

    
282
  private int makeQuat(int axis,int index)
283
    {
284
    if( axis==1 ) return 13;
285
    if( index<0 ) index+=12;
286
    return index;
287
    }
288

    
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290

    
291
  private boolean cornerBelongs(int index, int axis, int layer)
292
    {
293
    if( axis==0 )
294
      {
295
      boolean up = cornerIsUp(index);
296
      return ((up && layer==2) || (!up && layer==0));
297
      }
298
    else
299
      {
300
      boolean le = cornerIsLeft(index);
301
      return ((le && layer==0) || (!le && layer==1));
302
      }
303
    }
304

    
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306

    
307
  private void updateCornerQuats(int[] rotInfo)
308
    {
309
    if( mQuatMult==null ) initializeQuatMult();
310

    
311
    int axis = rotInfo[0];
312
    int layer= rotInfo[1];
313
    int index=-rotInfo[2];
314

    
315
    int quat = makeQuat(axis,index);
316

    
317
    for(int corner=0; corner<8; corner++)
318
      {
319
      if( cornerBelongs(corner,axis,layer) )
320
        {
321
        int curr = mCornerQuat[corner];
322
        mCornerQuat[corner] = mQuatMult[quat][curr];
323
        }
324
      }
325
    }
326

    
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328

    
329
  private void randomizeNewScramble1(int[][] scramble, Random rnd, int curr, int total)
330
    {
331
    int layer, nextAngle;
332

    
333
    if( curr==0 )
334
      {
335
      for(int corner=0; corner<8; corner++) mCornerQuat[corner] = 0;
336
      mLastRot = rnd.nextInt(4);
337
      computePermittedAngles();
338
      }
339

    
340
    switch(mLastRot)
341
      {
342
      case LAST_SL: layer = rnd.nextInt(2);
343
                    nextAngle = getNextAngle(rnd,layer);
344

    
345
                    if( nextAngle==0 )
346
                      {
347
                      layer = 1-layer;
348
                      nextAngle = getNextAngleNotZero(rnd,layer);
349
                      }
350

    
351
                    scramble[curr][0] = 0;
352
                    scramble[curr][1] = 2*layer;
353
                    scramble[curr][2] = nextAngle;
354
                    mLastRot = layer==0 ? LAST_LO : LAST_UP;
355
                    updateCornerQuats(scramble[curr]);
356
                    break;
357
      case LAST_LO:
358
      case LAST_UP: layer = mLastRot==LAST_LO ? 1:0;
359
                    nextAngle = getNextAngle(rnd,layer);
360

    
361
                    if( nextAngle!=0 )
362
                      {
363
                      scramble[curr][0] = 0;
364
                      scramble[curr][1] = 2*layer;
365
                      scramble[curr][2] = nextAngle;
366
                      updateCornerQuats(scramble[curr]);
367
                      mLastRot = LAST_UL;
368
                      }
369
                    else
370
                      {
371
                      scramble[curr][0] = 1;
372
                      scramble[curr][1] = rnd.nextInt(2);
373
                      scramble[curr][2] = 1;
374
                      mLastRot = LAST_SL;
375
                      updateCornerQuats(scramble[curr]);
376
                      computePermittedAngles();
377
                      }
378

    
379
                    break;
380
      case LAST_UL: scramble[curr][0] = 1;
381
                    scramble[curr][1] = rnd.nextInt(2);
382
                    scramble[curr][2] = 1;
383
                    mLastRot = LAST_SL;
384
                    updateCornerQuats(scramble[curr]);
385
                    computePermittedAngles();
386
                    break;
387
      }
388
    }
389

    
390
///////////////////////////////////////////////////////////////////////////////////////////////////
391
// PUBLIC API
392

    
393
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
394
    {
395
    if( mType==0 ) randomizeNewScramble0(scramble, rnd, curr, total);
396
    if( mType==1 ) randomizeNewScramble1(scramble, rnd, curr, total);
397
    }
398
  }
(15-15/15)