Project

General

Profile

« Previous | Next » 

Revision 3a5aa558

Added by Leszek Koltunski about 2 years ago

Fix scrambling the Danavi Block IV (and Burr Cube). Still looks a bit buggy, shouldn't have two consecutive moves along the same axis and layer and it seems to have those occasionally.

View differences:

src/main/java/org/distorted/objectlib/scrambling/ObjectScrambler.java
441 441
    mBandagedStates.add(nextState);
442 442
    }
443 443

  
444

  
444 445
///////////////////////////////////////////////////////////////////////////////////////////////////
445 446
// TYPE 2
446 447

  
447
  private boolean buildMove(int[][] scramble, Random rnd, int curr)
448
  private boolean buildMove2Cons(int[][] scramble, Random rnd, int curr)
448 449
    {
449 450
    ScrambleStateBandagedCuboid currState = mBandagedStates.get(curr);
450
    int indexExcluded = curr>0 ? scramble[curr-1][0] : ScrambleStateBandagedCuboid.AXIS_NONE;
451
    int numMoves = currState.numMoves(indexExcluded);
451
    boolean lock= (curr>=2 && scramble[curr-2][0]==scramble[curr-1][0] );
452
    int axisExcluded = lock ? scramble[curr-1][0] : ScrambleStateBandagedCuboid.AXIS_NONE;
453
    int layerExcluded= !lock && curr>=1 ? scramble[curr-1][1] : -1;
454
    int numMoves = currState.numMoves(axisExcluded,layerExcluded);
452 455

  
453 456
    while( numMoves==0 )
454 457
      {
......
458 461
        ScrambleStateBandagedCuboid prevState = mBandagedStates.get(curr-1);
459 462
        ObjectSignature signature = currState.getSignature();
460 463
        prevState.removeMoves(signature);
461
        boolean result = buildMove(scramble,rnd,curr-1);
464
        boolean result = buildMove2Cons(scramble,rnd,curr-1);
462 465
        if( !result ) return false;
463 466
        currState = mBandagedStates.get(curr);
464
        indexExcluded = scramble[curr-1][0];
465
        numMoves = currState.numMoves(indexExcluded);
467
        lock= (curr>=2 && scramble[curr-2][0]==scramble[curr-1][0] );
468
        axisExcluded = lock ? scramble[curr-1][0] : ScrambleStateBandagedCuboid.AXIS_NONE;
469
        layerExcluded= lock ? -1 : scramble[curr-1][1];
470
        numMoves = currState.numMoves(axisExcluded,layerExcluded);
471
        }
472
      else return false;
473
      }
474

  
475
    int randMove = rnd.nextInt(numMoves);
476
    int moveIndex = currState.getNthMove(randMove,axisExcluded,layerExcluded);
477
    ObjectSignature signature = currState.getMove(moveIndex);
478
    currState.fillOutScramble(scramble[curr],moveIndex);
479

  
480
    ScrambleStateBandagedCuboid nextState = new ScrambleStateBandagedCuboid(mNumLayers[0], mNumLayers[1], mNumLayers[2], signature);
481
    mBandagedStates.add(nextState);
482

  
483
    return true;
484
    }
485

  
486
///////////////////////////////////////////////////////////////////////////////////////////////////
487
// TYPE 2
488

  
489
  private boolean buildMove1Cons(int[][] scramble, Random rnd, int curr)
490
    {
491
    ScrambleStateBandagedCuboid currState = mBandagedStates.get(curr);
492
    int axisExcluded = curr>=1 ? scramble[curr-1][0] : ScrambleStateBandagedCuboid.AXIS_NONE;
493
    int numMoves = currState.numMoves(axisExcluded);
494

  
495
    while( numMoves==0 )
496
      {
497
      if( curr>0 )
498
        {
499
        mBandagedStates.remove(curr);
500
        ScrambleStateBandagedCuboid prevState = mBandagedStates.get(curr-1);
501
        ObjectSignature signature = currState.getSignature();
502
        prevState.removeMoves(signature);
503
        boolean result = buildMove1Cons(scramble,rnd,curr-1);
504
        if( !result ) return false;
505
        currState = mBandagedStates.get(curr);
506
        axisExcluded = scramble[curr-1][0];
507
        numMoves = currState.numMoves(axisExcluded);
466 508
        }
467 509
      else
468 510
        {
......
471 513
      }
472 514

  
473 515
    int randMove = rnd.nextInt(numMoves);
474
    int moveIndex = currState.getNthMove(randMove,indexExcluded);
516
    int moveIndex = currState.getNthMove(randMove,axisExcluded);
475 517
    ObjectSignature signature = currState.getMove(moveIndex);
476 518
    currState.fillOutScramble(scramble[curr],moveIndex);
477 519

  
......
495 537

  
496 538
    for(int curr=0; curr<total; curr++)
497 539
      {
498
      boolean result = buildMove(scramble,rnd,curr);
540
      boolean result = buildMove1Cons(scramble,rnd,curr);
499 541
      if( !result )
500 542
        {
501 543
        success = false;
......
503 545
        }
504 546
      }
505 547

  
548
    if( !success )
549
      {
550
      success = true;
551
      mBandagedStates.clear();
552
      state = new ScrambleStateBandagedCuboid(mNumLayers[0], mNumLayers[1], mNumLayers[2], signature);
553
      mBandagedStates.add(state);
554

  
555
      for(int curr=0; curr<total; curr++)
556
        {
557
        boolean result = buildMove2Cons(scramble,rnd,curr);
558
        if( !result )
559
          {
560
          success = false;
561
          break;
562
          }
563
        }
564
      }
565

  
506 566
    if( !success )
507 567
      {
508 568
      mBandagedStates.clear();
src/main/java/org/distorted/objectlib/scrambling/ScrambleStateBandagedCuboid.java
198 198
    return -1;
199 199
    }
200 200

  
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

  
203
  private int numXMoves(int excludedLayer)
204
    {
205
    int num=0;
206
    for(int x=mStartX; x<mStartY; x++) if( mMoves[x]!=null && (x-mStartX)%mLayer[0]!=excludedLayer ) num++;
207
    return num;
208
    }
209

  
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

  
212
  private int numYMoves(int excludedLayer)
213
    {
214
    int num=0;
215
    for(int y=mStartY; y<mStartZ; y++) if( mMoves[y]!=null && (y-mStartY)%mLayer[1]!=excludedLayer ) num++;
216
    return num;
217
    }
218

  
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

  
221
  private int numZMoves(int excludedLayer)
222
    {
223
    int num=0;
224
    for(int z=mStartZ; z<mNumMoves; z++) if( mMoves[z]!=null && (z-mStartZ)%mLayer[2]!=excludedLayer ) num++;
225
    return num;
226
    }
227

  
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

  
230
  public int numMoves(int excludedAxis, int excludedLayer)
231
    {
232
    switch(excludedAxis)
233
      {
234
      case AXIS_X: return numYMoves(excludedLayer)+numZMoves(excludedLayer);
235
      case AXIS_Y: return numXMoves(excludedLayer)+numZMoves(excludedLayer);
236
      case AXIS_Z: return numXMoves(excludedLayer)+numYMoves(excludedLayer);
237
      }
238

  
239
    return numXMoves(excludedLayer)+numYMoves(excludedLayer)+numZMoves(excludedLayer);
240
    }
241

  
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

  
244
  public int getNthMove(int n, int excludedAxis, int excludedLayer)
245
    {
246
    int num = 0;
247

  
248
    if( excludedAxis!=0 )
249
      {
250
      for(int m=mStartX; m<mStartY; m++)
251
        if( mMoves[m]!=null && (m-mStartX)%mLayer[0]!=excludedLayer )
252
          {
253
          if( num==n ) return m;
254
          num++;
255
          }
256
      }
257

  
258
    if( excludedAxis!=1 )
259
      {
260
      for(int m=mStartY; m<mStartZ; m++)
261
        if( mMoves[m]!=null && (m-mStartY)%mLayer[1]!=excludedLayer )
262
          {
263
          if( num==n ) return m;
264
          num++;
265
          }
266
      }
267

  
268
    if( excludedAxis!=2 )
269
      {
270
      for(int m=mStartZ; m<mNumMoves; m++)
271
        if( mMoves[m]!=null && (m-mStartZ)%mLayer[2]!=excludedLayer )
272
          {
273
          if( num==n ) return m;
274
          num++;
275
          }
276
      }
277

  
278
    return -1;
279
    }
280

  
201 281
///////////////////////////////////////////////////////////////////////////////////////////////////
202 282

  
203 283
  private ObjectSignature[] createMoves()

Also available in: Unified diff