Project

General

Profile

Download (15.2 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / patterns / RubikPattern.java @ 7e97ea45

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.patterns;
11

    
12
import java.util.ArrayList;
13
import java.util.List;
14

    
15
import org.distorted.objectlib.helpers.MovesFinished;
16
import org.distorted.objectlib.main.ObjectControl;
17

    
18
import static org.distorted.patterns.RubikPatternList.NUM_OBJECTS;
19

    
20
///////////////////////////////////////////////////////////////////////////////////////////////////
21

    
22
public class RubikPattern
23
{
24
  private static final int MILLIS_PER_DEGREE = 6;
25

    
26
  private final int[] numCategories    = new int[NUM_OBJECTS];
27
  private final int[] currentCategory  = new int[NUM_OBJECTS];
28
  private final int[] currentVisiblePos= new int[NUM_OBJECTS];
29
  private final int[] currentExpanded  = new int[NUM_OBJECTS];
30

    
31
  private final List<List<Category>> mCategories;
32
  private static RubikPattern mThis;
33

    
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35

    
36
  private static class Category
37
    {
38
    private final String[] mLines;
39
    private int numPatterns;
40
    private ArrayList<Pattern> mPatterns;
41
    private boolean mInitialized;
42

    
43
  /////////////////////////////////////////////////////////////
44

    
45
    Category(String[] lines)
46
      {
47
      mLines       = lines;
48
      numPatterns  = lines.length-1;
49
      mInitialized = false;
50
      }
51

    
52
  /////////////////////////////////////////////////////////////
53

    
54
    void initialize()
55
      {
56
      int colon;
57
      String moves, name;
58
      Pattern pattern;
59

    
60
      mPatterns = new ArrayList<>();
61

    
62
      for(int i=0; i<numPatterns; i++)
63
        {
64
        colon = mLines[i+1].indexOf(":");
65

    
66
        if( colon!=-1 )
67
          {
68
          moves   = mLines[i+1].substring(colon+1);
69
          name    = mLines[i+1].substring(0,colon);
70
          pattern = new Pattern(name,moves);
71

    
72
          mPatterns.add(pattern);
73
          }
74
        else
75
          {
76
          numPatterns--;
77
          }
78
        }
79

    
80
      mInitialized = true;
81
      }
82

    
83
  /////////////////////////////////////////////////////////////
84

    
85
    int getNumPatterns()
86
      {
87
      return numPatterns;
88
      }
89

    
90
  /////////////////////////////////////////////////////////////
91

    
92
    String getName()
93
      {
94
      return mLines[0];
95
      }
96

    
97
  /////////////////////////////////////////////////////////////
98

    
99
    String getPatternName(int pattern)
100
      {
101
      if( !mInitialized ) initialize();
102

    
103
      if( pattern>=0 && pattern<numPatterns )
104
        {
105
        Pattern p = mPatterns.get(pattern);
106
        return  p!=null ? p.getName():"";
107
        }
108

    
109
      return "";
110
      }
111

    
112
  /////////////////////////////////////////////////////////////
113

    
114
    int getPatternCurMove(int pattern)
115
      {
116
      if( !mInitialized ) initialize();
117

    
118
      if( pattern>=0 && pattern<numPatterns )
119
        {
120
        Pattern p = mPatterns.get(pattern);
121
        return  p!=null ? p.getCurMove():-1;
122
        }
123

    
124
      return -1;
125
      }
126

    
127
  /////////////////////////////////////////////////////////////
128

    
129
    int getPatternNumMove(int pattern)
130
      {
131
      if( !mInitialized ) initialize();
132

    
133
      if( pattern>=0 && pattern<numPatterns )
134
        {
135
        Pattern p = mPatterns.get(pattern);
136
        return  p!=null ? p.getNumMove():-1;
137
        }
138

    
139
      return -1;
140
      }
141

    
142
  /////////////////////////////////////////////////////////////
143

    
144
    void makeMove(ObjectControl control, int pattern)
145
      {
146
      if( !mInitialized ) initialize();
147

    
148
      if( pattern>=0 && pattern<numPatterns )
149
        {
150
        Pattern p = mPatterns.get(pattern);
151
        if( p!=null ) p.makeMove(control);
152
        }
153
      }
154

    
155
  /////////////////////////////////////////////////////////////
156

    
157
    void backMove(ObjectControl control, int pattern)
158
      {
159
      if( !mInitialized ) initialize();
160

    
161
      if( pattern>=0 && pattern<numPatterns )
162
        {
163
        Pattern p = mPatterns.get(pattern);
164
        if( p!=null ) p.backMove(control);
165
        }
166
      }
167

    
168
  /////////////////////////////////////////////////////////////
169

    
170
    int[][] reInitialize(int pattern)
171
      {
172
      if( !mInitialized ) initialize();
173

    
174
      if( pattern>=0 && pattern<numPatterns )
175
        {
176
        Pattern p = mPatterns.get(pattern);
177
        if( p!=null ) return p.reInitialize();
178
        }
179

    
180
      return null;
181
      }
182
    }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

    
186
  private static class Pattern implements MovesFinished
187
    {
188
    private final String nameStr;
189
    private String moveStr;
190
    private int[][] moves;
191
    private int curMove;
192
    private int numMove;
193
    private boolean mCanRotate;
194
    private boolean mInitialized;
195

    
196
  /////////////////////////////////////////////////////////////
197

    
198
    Pattern(String name, String moves)
199
      {
200
      nameStr      = name;
201
      moveStr      = expand(moves);
202
      mCanRotate   = true;
203
      mInitialized = false;
204
      }
205

    
206
  /////////////////////////////////////////////////////////////
207

    
208
    private String expandOne(String moves)
209
      {
210
      int right, left=-1;
211
      int len=moves.length();
212

    
213
      for(int ch=0; ch<len; ch++)
214
        {
215
        char c = moves.charAt(ch);
216

    
217
        if( c == '(' ) left=ch;
218
        if( c == ')' && left>=0 )
219
          {
220
          right = ch;
221
          String lStr = moves.substring(0, moves.charAt(left-1)==' ' ? left-1 : left);
222
          String mStr = moves.substring(left+1,right);
223
          String rStr = moves.substring(right+1);
224
          int number, space = rStr.indexOf(' ');
225
          if( mStr.charAt(0) != ' ') mStr = ' '+mStr;
226

    
227
          if( space>0 )
228
            {
229
            number = Integer.parseInt(rStr.substring(0,space));
230
            rStr = rStr.substring(space);
231
            }
232
          else if( space==0 )
233
            {
234
            number = 1;
235
            }
236
          else
237
            {
238
            number = Integer.parseInt(rStr);
239
            rStr = "";
240
            }
241

    
242
          StringBuilder builder = new StringBuilder();
243
          builder.append(lStr);
244
          for(int i=0; i<number; i++) builder.append(mStr);
245
          builder.append(rStr);
246
          return builder.toString();
247
          }
248
        }
249

    
250
      return moves;
251
      }
252

    
253
  /////////////////////////////////////////////////////////////
254

    
255
    private String expand(String moves)
256
      {
257
      String next,curr = moves;
258
      boolean expanded;
259

    
260
      do
261
        {
262
        next = expandOne(curr);
263
        expanded = (next.length() > curr.length());
264
        curr = next;
265
        }
266
      while(expanded);
267

    
268
      return curr;
269
      }
270

    
271
  /////////////////////////////////////////////////////////////
272

    
273
    private void initialize()
274
      {
275
      numMove = moveStr.length()/4;
276
      moves   = new int[numMove][3];
277
      curMove = numMove;
278
      parseMoves(moves,numMove,moveStr);
279
      moveStr = null;
280
      mInitialized = true;
281
      }
282

    
283
  /////////////////////////////////////////////////////////////
284

    
285
    String getName()
286
      {
287
      return nameStr;
288
      }
289

    
290
  /////////////////////////////////////////////////////////////
291

    
292
    int getNumMove()
293
      {
294
      if( !mInitialized ) initialize();
295

    
296
      return numMove;
297
      }
298

    
299
  /////////////////////////////////////////////////////////////
300

    
301
    int getCurMove()
302
      {
303
      if( !mInitialized ) initialize();
304

    
305
      return curMove;
306
      }
307

    
308
  /////////////////////////////////////////////////////////////
309

    
310
    void makeMove(ObjectControl control)
311
      {
312
      if( !mInitialized ) initialize();
313

    
314
      if( mCanRotate )
315
        {
316
        curMove++;
317

    
318
        if( curMove>numMove )
319
          {
320
          curMove= 0;
321
          control.initializeObject(null);
322
          }
323
        else
324
          {
325
          int axis      = moves[curMove-1][0];
326
		      int rowBitmap = moves[curMove-1][1];
327
		      int bareAngle = moves[curMove-1][2];
328

    
329
          if( bareAngle!=0 )
330
            {
331
            mCanRotate = false;
332
            control.addRotation(this, axis, rowBitmap, bareAngle, MILLIS_PER_DEGREE);
333
            }
334
          else
335
            {
336
            android.util.Log.e("pattern", "error: pattern "+nameStr+" move "+(curMove-1)+" angle 0");
337
            }
338
          }
339
        }
340
      else
341
        {
342
        android.util.Log.e("pattern", "failed to make Move!");
343
        }
344
      }
345

    
346
  /////////////////////////////////////////////////////////////
347

    
348
    void backMove(ObjectControl control)
349
      {
350
      if( !mInitialized ) initialize();
351

    
352
      if( mCanRotate )
353
        {
354
        curMove--;
355

    
356
        if( curMove<0 )
357
          {
358
          curMove=numMove;
359
          control.initializeObject(moves);
360
          }
361
        else
362
          {
363
          int axis      = moves[curMove][0];
364
		      int rowBitmap = moves[curMove][1];
365
		      int bareAngle = moves[curMove][2];
366

    
367
          if( bareAngle!=0 )
368
            {
369
            mCanRotate = false;
370
            control.addRotation(this, axis, rowBitmap, -bareAngle, MILLIS_PER_DEGREE);
371
            }
372
          else
373
            {
374
            android.util.Log.e("pattern", "error: pattern "+nameStr+" move "+curMove+" angle 0");
375
            }
376
          }
377
        }
378
      else
379
        {
380
        android.util.Log.e("pattern", "failed to back Move!");
381
        }
382
      }
383

    
384
  /////////////////////////////////////////////////////////////
385

    
386
    int[][] reInitialize()
387
      {
388
      if( !mInitialized ) initialize();
389

    
390
      mCanRotate = true;
391
      curMove = numMove;
392
      return moves;
393
      }
394

    
395
  /////////////////////////////////////////////////////////////
396

    
397
    public void onActionFinished(final long effectID)
398
      {
399
      mCanRotate = true;
400
      }
401
    }
402

    
403
///////////////////////////////////////////////////////////////////////////////////////////////////
404

    
405
  private RubikPattern()
406
    {
407
    mCategories = new ArrayList<>();
408

    
409
    for(int i=0; i<NUM_OBJECTS; i++)
410
      {
411
      List<Category> list = new ArrayList<>();
412
      String[][] patStrings = RubikPatternList.getPatterns(i);
413

    
414
      for(String[] lines: patStrings) list.add(new Category(lines));
415

    
416
      mCategories.add(list);
417
      numCategories[i]=patStrings.length;
418

    
419
      currentExpanded[i] = -1;
420
      }
421
    }
422

    
423
///////////////////////////////////////////////////////////////////////////////////////////////////
424

    
425
  private Category getCategory(int tab, int cat)
426
    {
427
    if( tab>=0 && tab<NUM_OBJECTS && cat>=0 && cat<numCategories[tab] )
428
      {
429
      return mCategories.get(tab).get(cat);
430
      }
431

    
432
    return null;
433
    }
434

    
435
///////////////////////////////////////////////////////////////////////////////////////////////////
436
// PUBLIC API
437
///////////////////////////////////////////////////////////////////////////////////////////////////
438

    
439
  public static RubikPattern getInstance()
440
    {
441
    if( mThis==null )
442
      {
443
      mThis = new RubikPattern();
444
      }
445

    
446
    return mThis;
447
    }
448

    
449
///////////////////////////////////////////////////////////////////////////////////////////////////
450

    
451
  public static void parseMoves(int[][] result, int numMoves, String moves)
452
    {
453
    int digit0, digit1, digit2, number;
454

    
455
    for(int i=0; i<numMoves; i++)
456
      {
457
      digit0 = moves.charAt(4*i+1)-'0';
458
      digit1 = moves.charAt(4*i+2)-'0';
459
      digit2 = moves.charAt(4*i+3)-'0';
460
      number = 100*digit0+10*digit1+digit2;
461

    
462
      int axis   = (number/32)%8;
463
      int bitmap = (number%32)  ;
464
      int angle  = (number/32)/8;
465

    
466
      switch(angle)
467
        {
468
        case 0: angle = 2; break;
469
        case 1: angle = 1; break;
470
        case 2: angle =-1; break;
471
        case 3: angle =-2; break;
472
        default: android.util.Log.e("D", "ERROR!! parsing move "+moves+" number="+number);
473
        }
474

    
475
      result[i][0] = axis;
476
      result[i][1] = bitmap;
477
      result[i][2] = angle;
478
      }
479
    }
480

    
481
///////////////////////////////////////////////////////////////////////////////////////////////////
482

    
483
  public int getNumCategories(int tab)
484
    {
485
    return tab>=0 && tab<NUM_OBJECTS ? numCategories[tab] : 0;
486
    }
487

    
488
///////////////////////////////////////////////////////////////////////////////////////////////////
489

    
490
  public void rememberState(int tab, int cat, int scrollPos, int expanded)
491
    {
492
    if( tab>=0 && tab<NUM_OBJECTS )
493
      {
494
      currentCategory[tab]  = cat;
495
      currentVisiblePos[tab]= scrollPos;
496
      currentExpanded[tab]  = expanded;
497
      }
498
    }
499

    
500
///////////////////////////////////////////////////////////////////////////////////////////////////
501

    
502
  public int recallCategory(int tab)
503
    {
504
    return tab>=0 && tab<NUM_OBJECTS ? currentCategory[tab] : 0;
505
    }
506

    
507
///////////////////////////////////////////////////////////////////////////////////////////////////
508

    
509
  public int recallVisiblePos(int tab)
510
    {
511
    return tab>=0 && tab<NUM_OBJECTS ? currentVisiblePos[tab] : 0;
512
    }
513

    
514
///////////////////////////////////////////////////////////////////////////////////////////////////
515

    
516
  public int recallExpanded(int tab)
517
    {
518
    return tab>=0 && tab<NUM_OBJECTS ? currentExpanded[tab] : -1;
519
    }
520

    
521
///////////////////////////////////////////////////////////////////////////////////////////////////
522

    
523
  public String getCategoryName(int tab, int cat)
524
    {
525
    Category c = getCategory(tab,cat);
526
    return c!=null ? c.getName() : null;
527
    }
528

    
529
///////////////////////////////////////////////////////////////////////////////////////////////////
530

    
531
  public String getPatternName(int tab, int cat, int pat)
532
    {
533
    Category c = getCategory(tab,cat);
534
    return c!=null ? c.getPatternName(pat) : null;
535
    }
536

    
537
///////////////////////////////////////////////////////////////////////////////////////////////////
538

    
539
  public int getNumPatterns(int tab, int cat)
540
    {
541
    Category c = getCategory(tab,cat);
542
    return c!=null ? c.getNumPatterns() : 0;
543
    }
544

    
545
///////////////////////////////////////////////////////////////////////////////////////////////////
546

    
547
  public int getCurMove(int tab, int cat, int pat)
548
    {
549
    Category c = getCategory(tab,cat);
550
    return c!=null ? c.getPatternCurMove(pat) : 0;
551
    }
552

    
553
///////////////////////////////////////////////////////////////////////////////////////////////////
554

    
555
  public int getNumMoves(int tab, int cat, int pat)
556
    {
557
    Category c = getCategory(tab,cat);
558
    return c!=null ? c.getPatternNumMove(pat) : 0;
559
    }
560

    
561
///////////////////////////////////////////////////////////////////////////////////////////////////
562

    
563
  public void makeMove(ObjectControl control, int tab, int cat, int pat)
564
    {
565
    Category c = getCategory(tab,cat);
566
    if( c!=null ) c.makeMove(control,pat);
567
    }
568

    
569
///////////////////////////////////////////////////////////////////////////////////////////////////
570

    
571
  public void backMove(ObjectControl control, int tab, int cat, int pat)
572
    {
573
    Category c = getCategory(tab,cat);
574
    if( c!=null ) c.backMove(control,pat);
575
    }
576

    
577
///////////////////////////////////////////////////////////////////////////////////////////////////
578

    
579
  public int[][] reInitialize(int tab, int cat, int pat)
580
    {
581
    Category c = getCategory(tab,cat);
582
    return c!=null ? c.reInitialize(pat) : null;
583
    }
584
}
(10-10/11)