Project

General

Profile

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

magiccube / src / main / java / org / distorted / patterns / RubikPattern.java @ 53f23b64

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 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.patterns;
21

    
22
import java.util.Vector;
23
import android.graphics.Paint;
24

    
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26

    
27
public class RubikPattern
28
{
29
  public static final int MIN_CUBE  = 2;
30
  public static final int MAX_CUBE  = 5;
31
  public static final int NUM_CUBES = MAX_CUBE-MIN_CUBE+1;
32

    
33
  private int[] numCategories = new int[NUM_CUBES];
34
  private static String buffer="";
35
  private static Paint mPaint = new Paint();
36
  private static int mWidth;
37

    
38
  private Vector<Category>[] mCategories;
39

    
40
  private static RubikPattern mThis;
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
  private class Category
45
    {
46
    private String name;
47
    private int numpatterns;
48
    private Vector<Pattern> patterns;
49
    private int curPattern;
50

    
51
    Category(String n)
52
      {
53
      name=n;
54
      curPattern=0;
55
      numpatterns=0;
56
      patterns = new Vector<>();
57
      }
58
    void addPattern(Pattern p)
59
      {
60
      patterns.addElement(p);
61
      numpatterns++;
62
      }
63
    int getNumPatterns()
64
    {
65
    return numpatterns;
66
    }
67
    String getName()
68
    {
69
    return name;
70
    }
71
    void next()
72
      {
73
      curPattern++;
74
      if( curPattern>=numpatterns )
75
        curPattern=0;
76
      }
77
    void prev()
78
      {
79
      curPattern--;
80
      if( curPattern<0 )
81
        curPattern=numpatterns-1;
82
      }
83
    String getPatternName()
84
      {
85
      Pattern p = patterns.elementAt(curPattern);
86
      return  p!=null ? p.getName(curPattern+1,numpatterns):null;
87
      }
88
    int getPatternCurMove()
89
      {
90
      Pattern p = patterns.elementAt(curPattern);
91
      return  p!=null ? p.getCurMove():-1;
92
      }
93
    int getPatternNumMove()
94
      {
95
      Pattern p = patterns.elementAt(curPattern);
96
      return  p!=null ? p.getNumMove():-1;
97
      }
98
    void initializeCube()
99
      {
100
      Pattern p = patterns.elementAt(curPattern);
101
      if( p!=null ) p.initializeCube();
102
      }
103
    void makeNextMove()
104
      {
105
      Pattern p = patterns.elementAt(curPattern);
106
      if( p!=null ) p.makeNextMove();
107
      }
108
    void makePrevMove()
109
      {
110
      Pattern p = patterns.elementAt(curPattern);
111
      if( p!=null ) p.makePrevMove();
112
      }
113
    }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116

    
117
  private class Pattern
118
    {
119
    private String name;
120
    private String shortname=null;
121
    private String moves;
122
    private int curmove;
123
    private int nummove;
124

    
125
    Pattern(String n, String m)
126
      {
127
      name=n;
128
      moves=m;
129
      nummove = moves.length()/4;
130
      curmove=nummove;
131
      }
132
    String getName(int cur,int num)
133
      {
134
      if( shortname==null ) shortname=cutPatternName(name);
135
      return shortname;
136
      }
137
    int getNumMove()
138
    {
139
    return nummove;
140
    }
141
    int getCurMove()
142
    {
143
    return curmove;
144
    }
145
    void makeNextMove()
146
      {
147
      curmove++;
148
      if( curmove>nummove)
149
        {
150
        curmove= 0;
151
        initializeCube();
152
        }
153
      else
154
        {
155
// TODO
156
//        RubikCube cube = world.getCube();
157
//        if( cube!=null && !cube.makeMove(moves.substring(4*curmove-4,4*curmove)) )
158
          curmove--;
159
        }
160
      }
161
    void makePrevMove()
162
      {
163
      curmove--;
164
      if( curmove<0)
165
        {
166
        curmove=nummove;
167
        initializeCube();
168
        }
169
      else
170
        {
171
// TODO
172
//        RubikCube cube = world.getCube();
173
//        if( cube!=null && !cube.backMove(moves.substring(4*curmove,4*curmove+4)) )
174
          curmove++;
175
        }
176
      }
177
    void initializeCube()
178
      {
179
// TODO
180
//	    RubikCube cube = world.getCube();
181
//      if( cube!=null ) cube.setupPosition(moves.substring(0,4*curmove));
182
      }
183
    }
184

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

    
187
  private RubikPattern()
188
    {
189
    mPaint.setTextSize(24);
190
    mWidth = 200;
191
    mCategories = new Vector[NUM_CUBES];
192

    
193
    initializeCategories(0, RubikPatternData2.patterns);
194
    initializeCategories(1, RubikPatternData3.patterns);
195
    initializeCategories(2, RubikPatternData4.patterns);
196
    initializeCategories(3, RubikPatternData5.patterns);
197
    }
198

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

    
201
  private void initializeCategories(int num, String[] pat)
202
    {
203
    int colon;
204
    mCategories[num] = new Vector<>();
205
    Category cat=null;
206
    String name, pattern;
207
    Pattern patt;
208

    
209
    numCategories[num]=0;
210

    
211
    for(String p: pat)
212
      {
213
      colon = p.indexOf(':');
214

    
215
      if( colon==-1 )
216
        {
217
        cat = new Category(p);
218
        mCategories[num].addElement(cat);
219
        numCategories[num]++;
220
        }
221
      else
222
        {
223
        pattern = p.substring(colon+1);
224
        name    = p.substring(0,colon);
225
        patt = new Pattern(name,pattern);
226
        cat.addPattern(patt);
227
        }
228
      }
229
    }
230

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

    
233
  private static String cutPatternName(String n)
234
    {
235
    int len1 = (int)mPaint.measureText(n);
236

    
237
    if( len1>mWidth )
238
      {
239
      int l = n.length();
240

    
241
      while( l>=2 && len1>mWidth )
242
        {
243
        l--;
244
        buffer = n.substring(0,l);
245
        len1 = (int)mPaint.measureText(buffer);
246
        }
247

    
248
      return buffer+"...";
249
      }
250

    
251
    return n;
252
    }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255
// PUBLIC API
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257

    
258
  public static RubikPattern getInstance()
259
    {
260
    if( mThis==null )
261
      {
262
      mThis = new RubikPattern();
263
      }
264

    
265
    return mThis;
266
    }
267

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

    
270
  public String getCategoryName(int size,int num)
271
    {
272
    if( size>=0 && size<NUM_CUBES && num>=0 && num< numCategories[size] )
273
      {
274
      Category c = mCategories[size].elementAt(num);
275
      return c!=null ? c.getName() : null;
276
      }
277

    
278
    return null;
279
    }
280

    
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282

    
283
  public String getPatternName(int size,int num)
284
    {
285
    if( size>=0 && size<NUM_CUBES && num>=0 && num< numCategories[size] )
286
      {
287
      Category c = mCategories[size].elementAt(num);
288
      return c!=null ? c.getPatternName() : null;
289
      }
290

    
291
    return null;
292
    }
293

    
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295

    
296
  public int getNumPatterns(int size, int num )
297
    {
298
    if( size>=0 && size<NUM_CUBES && num>=0 && num< numCategories[size] )
299
      {
300
      Category c = mCategories[size].elementAt(num);
301
      return c!=null ? c.getNumPatterns() : 0;
302
      }
303

    
304
    return 0;
305
    }
306

    
307
///////////////////////////////////////////////////////////////////////////////////////////////////
308

    
309
  public void nextPattern(int size, int num )
310
    {
311
    if( size>=0 && size<NUM_CUBES && num>=0 && num< numCategories[size] )
312
      {
313
      Category c = mCategories[size].elementAt(num);
314

    
315
      if( c!=null )
316
        {
317
// TODO
318
//        RubikMenu.texDirty[0] = true;
319
        c.next();
320
        c.initializeCube();
321
        }
322
      }
323
    }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

    
327
  public void prevPattern(int size, int num )
328
    {
329
    if( size>=0 && size<NUM_CUBES && num>=0 && num< numCategories[size] )
330
      {
331
      Category c = mCategories[size].elementAt(num);
332

    
333
      if( c!=null )
334
        {
335
// TODO
336
//        RubikMenu.texDirty[0] = true;
337
        c.prev();
338
        c.initializeCube();
339
        }
340
      }
341
    }
342

    
343
///////////////////////////////////////////////////////////////////////////////////////////////////
344

    
345
  public int getCurrPattern(int size, int num )
346
    {
347
    if( size>=0 && size<NUM_CUBES && num>=0 && num< numCategories[size] )
348
      {
349
      Category c = mCategories[size].elementAt(num);
350
      return c!=null ? c.curPattern:0;
351
      }
352

    
353
    return 0;
354
    }
355

    
356
///////////////////////////////////////////////////////////////////////////////////////////////////
357

    
358
  public int getCurMove(int size, int num )
359
    {
360
    if( size>=0 && size<NUM_CUBES && num>=0 && num< numCategories[size] )
361
      {
362
      Category c = mCategories[size].elementAt(num);
363
      return c!=null ? c.getPatternCurMove():0;
364
      }
365

    
366
    return 0;
367
    }
368

    
369
///////////////////////////////////////////////////////////////////////////////////////////////////
370

    
371
  public int getNumMove(int size, int num )
372
    {
373
    if( size>=0 && size<NUM_CUBES && num>=0 && num< numCategories[size] )
374
      {
375
      Category c = mCategories[size].elementAt(num);
376
      return c!=null ? c.getPatternNumMove():0;
377
      }
378

    
379
    return 0;
380
    }
381

    
382
///////////////////////////////////////////////////////////////////////////////////////////////////
383

    
384
  public void makeNextMove(int size, int num)
385
    {
386
    if( size>=0 && size<NUM_CUBES && num>=0 && num< numCategories[size] )
387
      {
388
      Category c = mCategories[size].elementAt(num);
389
      if( c!=null ) c.makeNextMove();
390
      }
391
    }
392

    
393
///////////////////////////////////////////////////////////////////////////////////////////////////
394

    
395
  public void makePrevMove(int size, int num)
396
    {
397
    if( size>=0 && size<NUM_CUBES && num>=0 && num< numCategories[size] )
398
      {
399
      Category c = mCategories[size].elementAt(num);
400
      if( c!=null ) c.makePrevMove();
401
      }
402
    }
403

    
404
///////////////////////////////////////////////////////////////////////////////////////////////////
405

    
406
  public void initializeCube(int size,int num)
407
    {
408
    if( size>=0 && size<NUM_CUBES && num>=0 && num< numCategories[size] )
409
      {
410
      Category c = mCategories[size].elementAt(num);
411
      if( c!=null ) c.initializeCube();
412
      }
413
    }
414

    
415
///////////////////////////////////////////////////////////////////////////////////////////////////
416

    
417
  public int getNumCategories(int size)
418
    {
419
    return size>=0 && size<NUM_CUBES ? numCategories[size] : 0;
420
    }
421
}
(1-1/5)