Project

General

Profile

« Previous | Next » 

Revision b498f3f6

Added by Leszek Koltunski about 4 years ago

Introduce RubikPatternsList. Now, if one wants to contribute patterns for a new object, all one has to be is give the list of strings describing the patterns and add them to the List.

View differences:

src/main/java/org/distorted/patterns/RubikPattern.java
20 20
package org.distorted.patterns;
21 21

  
22 22
import org.distorted.magic.RubikPostRender;
23
import org.distorted.object.RubikObject;
24 23

  
25 24
import java.util.ArrayList;
26 25
import java.util.List;
27 26

  
27
import static org.distorted.patterns.RubikPatternList.NUM_OBJECTS;
28

  
28 29
///////////////////////////////////////////////////////////////////////////////////////////////////
29 30

  
30 31
public class RubikPattern
31 32
{
32 33
  private static final int DURATION_MILLIS = 800;
33 34

  
34
  public static final int MIN_CUBE  = 2;
35
  public static final int MAX_CUBE  = 5;
36
  public static final int NUM_CUBES = MAX_CUBE-MIN_CUBE+1;
37

  
38
  private int[] numCategories   = new int[NUM_CUBES];
39
  private int[] currentCategory = new int[NUM_CUBES];
40
  private int[] currentScrollPos= new int[NUM_CUBES];
35
  private int[] numCategories   = new int[NUM_OBJECTS];
36
  private int[] currentCategory = new int[NUM_OBJECTS];
37
  private int[] currentScrollPos= new int[NUM_OBJECTS];
41 38

  
42 39
  private List<List<Category>> mCategories;
43 40
  private static RubikPattern mThis;
......
55 52

  
56 53
    Category(String[] lines)
57 54
      {
58
      mLines=lines;
59
      numPatterns=lines.length-1;
60
      mPatterns = new ArrayList<>();
55
      mLines       = lines;
56
      numPatterns  = lines.length-1;
61 57
      mInitialized = false;
62 58
      }
63 59

  
......
69 65
      String moves, name;
70 66
      Pattern pattern;
71 67

  
68
      mPatterns = new ArrayList<>();
69

  
72 70
      for(int i=0; i<numPatterns; i++)
73 71
        {
74 72
        colon = mLines[i+1].indexOf(":");
......
204 202

  
205 203
  /////////////////////////////////////////////////////////////
206 204

  
207
    Pattern(String n, String m)
205
    Pattern(String name, String moves)
208 206
      {
209
      nameStr = n;
210
      moveStr = m;
207
      nameStr      = name;
208
      moveStr      = moves;
211 209
      mCanRotate   = true;
212 210
      mInitialized = false;
213 211
      }
......
269 267
      if( !mInitialized ) initialize();
270 268

  
271 269
      curMove++;
272
      RubikObject object = post.getObject();
273 270

  
274 271
      if( mCanRotate )
275 272
        {
......
280 277
          }
281 278
        else
282 279
          {
283
          int axis     =moves[curMove-1][0];
284
		      int rowBitmap=moves[curMove-1][1];
285
		      int bareAngle=moves[curMove-1][2];
286
          int angle    = bareAngle*(360/object.getBasicAngle());
280
          int axis     = moves[curMove-1][0];
281
		      int rowBitmap= moves[curMove-1][1];
282
		      int bareAngle= moves[curMove-1][2];
283
          int angle    = bareAngle*(360/post.getObject().getBasicAngle());
287 284
          int numRot   = Math.abs(bareAngle);
288 285

  
289 286
          if( angle!=0 )
......
311 308
      if( !mInitialized ) initialize();
312 309

  
313 310
      curMove--;
314
      RubikObject object = post.getObject();
315 311

  
316 312
      if( mCanRotate )
317 313
        {
......
322 318
          }
323 319
        else
324 320
          {
325
          int axis     =moves[curMove][0];
326
		      int rowBitmap=moves[curMove][1];
327
		      int bareAngle=moves[curMove][2];
328
          int angle    = bareAngle*(360/object.getBasicAngle());
321
          int axis     = moves[curMove][0];
322
		      int rowBitmap= moves[curMove][1];
323
		      int bareAngle= moves[curMove][2];
324
          int angle    = bareAngle*(360/post.getObject().getBasicAngle());
329 325
          int numRot   = Math.abs(bareAngle);
330 326

  
331 327
          if( angle!=0 )
......
371 367
    {
372 368
    mCategories = new ArrayList<>();
373 369

  
374
    initializeCategories(0, RubikPatternCube2.patterns);
375
    initializeCategories(1, RubikPatternCube3.patterns);
376
    initializeCategories(2, RubikPatternCube4.patterns);
377
    initializeCategories(3, RubikPatternCube5.patterns);
370
    for(int i=0; i<NUM_OBJECTS; i++)
371
      {
372
      List<Category> list = new ArrayList<>();
373
      String[][] patStrings = RubikPatternList.getPatterns(i);
374

  
375
      for(String[] lines: patStrings) list.add(new Category(lines));
376

  
377
      mCategories.add(list);
378
      numCategories[i]=patStrings.length;
379
      }
378 380
    }
379 381

  
380 382
///////////////////////////////////////////////////////////////////////////////////////////////////
381 383

  
382
  private void initializeCategories(int num, String[][] pat)
384
  private Category getCategory(int tab, int cat)
383 385
    {
384
    List<Category> list = new ArrayList<>();
385
    Category cat;
386

  
387
    for(String[] lines: pat)
386
    if( tab>=0 && tab<NUM_OBJECTS && cat>=0 && cat<numCategories[tab] )
388 387
      {
389
      cat = new Category(lines);
390
      list.add(cat);
388
      return mCategories.get(tab).get(cat);
391 389
      }
392 390

  
393
    mCategories.add(num,list);
394
    numCategories[num]=pat.length;
391
    return null;
395 392
    }
396 393

  
397 394
///////////////////////////////////////////////////////////////////////////////////////////////////
......
412 409

  
413 410
  public int getNumCategories(int tab)
414 411
    {
415
    return tab>=0 && tab<NUM_CUBES ? numCategories[tab] : 0;
416
    }
417

  
418
///////////////////////////////////////////////////////////////////////////////////////////////////
419

  
420
  public String getCategoryName(int tab, int cat)
421
    {
422
    if( tab>=0 && tab<NUM_CUBES && cat>=0 && cat< numCategories[tab] )
423
      {
424
      Category c = mCategories.get(tab).get(cat);
425
      return c!=null ? c.getName() : null;
426
      }
427

  
428
    return null;
412
    return tab>=0 && tab<NUM_OBJECTS ? numCategories[tab] : 0;
429 413
    }
430 414

  
431 415
///////////////////////////////////////////////////////////////////////////////////////////////////
432 416

  
433 417
  public void rememberState(int tab, int cat, int scrollPos)
434 418
    {
435
    if( tab>=0 && tab<NUM_CUBES )
419
    if( tab>=0 && tab<NUM_OBJECTS )
436 420
      {
437 421
      currentCategory[tab] = cat;
438 422
      currentScrollPos[tab]= scrollPos;
......
443 427

  
444 428
  public int recallCategory(int tab)
445 429
    {
446
    return tab>=0 && tab<NUM_CUBES ? currentCategory[tab] : 0;
430
    return tab>=0 && tab<NUM_OBJECTS ? currentCategory[tab] : 0;
447 431
    }
448 432

  
449 433
///////////////////////////////////////////////////////////////////////////////////////////////////
450 434

  
451 435
  public int recallScrollPos(int tab)
452 436
    {
453
    return tab>=0 && tab<NUM_CUBES ? currentScrollPos[tab] : 0;
437
    return tab>=0 && tab<NUM_OBJECTS ? currentScrollPos[tab] : 0;
454 438
    }
455 439

  
456 440
///////////////////////////////////////////////////////////////////////////////////////////////////
457 441

  
458
  public String getPatternName(int tab, int cat, int pat)
442
  public String getCategoryName(int tab, int cat)
459 443
    {
460
    if( tab>=0 && tab<NUM_CUBES && cat>=0 && cat< numCategories[tab] )
461
      {
462
      Category c = mCategories.get(tab).get(cat);
463
      return c!=null ? c.getPatternName(pat) : null;
464
      }
444
    Category c = getCategory(tab,cat);
445
    return c!=null ? c.getName() : null;
446
    }
465 447

  
466
    return null;
448
///////////////////////////////////////////////////////////////////////////////////////////////////
449

  
450
  public String getPatternName(int tab, int cat, int pat)
451
    {
452
    Category c = getCategory(tab,cat);
453
    return c!=null ? c.getPatternName(pat) : null;
467 454
    }
468 455

  
469 456
///////////////////////////////////////////////////////////////////////////////////////////////////
470 457

  
471 458
  public int getNumPatterns(int tab, int cat)
472 459
    {
473
    if( tab>=0 && tab<NUM_CUBES && cat>=0 && cat< numCategories[tab] )
474
      {
475
      Category c = mCategories.get(tab).get(cat);
476
      return c!=null ? c.getNumPatterns() : 0;
477
      }
478

  
479
    return 0;
460
    Category c = getCategory(tab,cat);
461
    return c!=null ? c.getNumPatterns() : 0;
480 462
    }
481 463

  
482 464
///////////////////////////////////////////////////////////////////////////////////////////////////
483 465

  
484 466
  public int getCurMove(int tab, int cat, int pat)
485 467
    {
486
    if( tab>=0 && tab<NUM_CUBES && cat>=0 && cat< numCategories[tab] )
487
      {
488
      Category c = mCategories.get(tab).get(cat);
489
      return c!=null ? c.getPatternCurMove(pat):0;
490
      }
491

  
492
    return 0;
468
    Category c = getCategory(tab,cat);
469
    return c!=null ? c.getPatternCurMove(pat) : 0;
493 470
    }
494 471

  
495 472
///////////////////////////////////////////////////////////////////////////////////////////////////
496 473

  
497 474
  public int getNumMoves(int tab, int cat, int pat)
498 475
    {
499
    if( tab>=0 && tab<NUM_CUBES && cat>=0 && cat< numCategories[tab] )
500
      {
501
      Category c = mCategories.get(tab).get(cat);
502
      return c!=null ? c.getPatternNumMove(pat):0;
503
      }
504

  
505
    return 0;
476
    Category c = getCategory(tab,cat);
477
    return c!=null ? c.getPatternNumMove(pat) : 0;
506 478
    }
507 479

  
508 480
///////////////////////////////////////////////////////////////////////////////////////////////////
509 481

  
510 482
  public void makeMove(RubikPostRender post, int tab, int cat, int pat)
511 483
    {
512
    if( tab>=0 && tab<NUM_CUBES && cat>=0 && cat< numCategories[tab] )
513
      {
514
      Category c = mCategories.get(tab).get(cat);
515
      if( c!=null ) c.makeMove(post,pat);
516
      }
484
    Category c = getCategory(tab,cat);
485
    if( c!=null ) c.makeMove(post,pat);
517 486
    }
518 487

  
519 488
///////////////////////////////////////////////////////////////////////////////////////////////////
520 489

  
521 490
  public void backMove(RubikPostRender post, int tab, int cat, int pat)
522 491
    {
523
    if( tab>=0 && tab<NUM_CUBES && cat>=0 && cat< numCategories[tab] )
524
      {
525
      Category c = mCategories.get(tab).get(cat);
526
      if( c!=null ) c.backMove(post,pat);
527
      }
492
    Category c = getCategory(tab,cat);
493
    if( c!=null ) c.backMove(post,pat);
528 494
    }
529 495

  
530 496
///////////////////////////////////////////////////////////////////////////////////////////////////
531 497

  
532 498
  public int[][] reInitialize(int tab, int cat, int pat)
533 499
    {
534
    if( tab>=0 && tab<NUM_CUBES && cat>=0 && cat< numCategories[tab] )
535
      {
536
      Category c = mCategories.get(tab).get(cat);
537
      if( c!=null ) return c.reInitialize(pat);
538
      }
539

  
540
    return null;
500
    Category c = getCategory(tab,cat);
501
    return c!=null ? c.reInitialize(pat) : null;
541 502
    }
542 503
}

Also available in: Unified diff