Project

General

Profile

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

magiccube / src / main / java / org / distorted / patterns / RubikPattern.java @ 3a9d19ed

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 static class Category
45
    {
46
    private String name;
47
    private int numPatterns;
48
    private Vector<Pattern> patterns;
49

    
50
  /////////////////////////////////////////////////////////////
51

    
52
    Category(String n)
53
      {
54
      name=n;
55
      numPatterns=0;
56
      patterns = new Vector<>();
57
      }
58

    
59
  /////////////////////////////////////////////////////////////
60

    
61
    void addPattern(Pattern p)
62
      {
63
      patterns.addElement(p);
64
      numPatterns++;
65
      }
66

    
67
  /////////////////////////////////////////////////////////////
68

    
69
    int getNumPatterns()
70
    {
71
    return numPatterns;
72
    }
73

    
74
  /////////////////////////////////////////////////////////////
75

    
76
    String getName()
77
    {
78
    return name;
79
    }
80

    
81
  /////////////////////////////////////////////////////////////
82

    
83
    String getPatternName(int pattern)
84
      {
85
      if( pattern>=0 && pattern<numPatterns )
86
        {
87
        Pattern p = patterns.elementAt(pattern);
88
        return  p!=null ? p.getName():"";
89
        }
90
      return "";
91
      }
92

    
93
  /////////////////////////////////////////////////////////////
94

    
95
    int getPatternCurMove(int pattern)
96
      {
97
      if( pattern>=0 && pattern<numPatterns )
98
        {
99
        Pattern p = patterns.elementAt(pattern);
100
        return  p!=null ? p.getCurMove():-1;
101
        }
102
      return -1;
103
      }
104

    
105
  /////////////////////////////////////////////////////////////
106

    
107
    int getPatternNumMove(int pattern)
108
      {
109
      if( pattern>=0 && pattern<numPatterns )
110
        {
111
        Pattern p = patterns.elementAt(pattern);
112
        return  p!=null ? p.getNumMove():-1;
113
        }
114
      return -1;
115
      }
116

    
117
  /////////////////////////////////////////////////////////////
118

    
119
    String retInitializationString(int pattern)
120
      {
121
      if( pattern>=0 && pattern<numPatterns )
122
        {
123
        Pattern p = patterns.elementAt(pattern);
124
        if( p!=null ) return p.retInitializationString();
125
        }
126

    
127
      return "";
128
      }
129

    
130
  /////////////////////////////////////////////////////////////
131

    
132
    String retNextMove(int pattern)
133
      {
134
      if( pattern>=0 && pattern<numPatterns )
135
        {
136
        Pattern p = patterns.elementAt(pattern);
137
        if( p!=null ) return p.retNextMove();
138
        }
139

    
140
      return "";
141
      }
142

    
143
  /////////////////////////////////////////////////////////////
144

    
145
    String retPrevMove(int pattern)
146
      {
147
      if( pattern>=0 && pattern<numPatterns )
148
        {
149
        Pattern p = patterns.elementAt(pattern);
150
        if( p!=null ) return p.retPrevMove();
151
        }
152

    
153
      return "";
154
      }
155
    }
156

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

    
159
  private static class Pattern
160
    {
161
    private String name;
162
    private String shortname=null;
163
    private String moves;
164
    private int curmove;
165
    private int nummove;
166

    
167
  /////////////////////////////////////////////////////////////
168

    
169
    Pattern(String n, String m)
170
      {
171
      name=n;
172
      moves=m;
173
      nummove = moves.length()/4;
174
      curmove=nummove;
175
      }
176

    
177
  /////////////////////////////////////////////////////////////
178

    
179
    String getName()
180
      {
181
      if( shortname==null ) shortname=cutPatternName(name);
182
      return shortname;
183
      }
184

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

    
187
    int getNumMove()
188
    {
189
    return nummove;
190
    }
191

    
192
  /////////////////////////////////////////////////////////////
193

    
194
    int getCurMove()
195
    {
196
    return curmove;
197
    }
198

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

    
201
    String retNextMove()
202
      {
203
      curmove++;
204

    
205
      if( curmove>nummove)
206
        {
207
        curmove= 0;
208
        return retInitializationString();
209
        }
210
      else
211
        {
212
        curmove--;
213
        return moves.substring(4*curmove-4,4*curmove);
214
        }
215
      }
216

    
217
  /////////////////////////////////////////////////////////////
218

    
219
    String retPrevMove()
220
      {
221
      curmove--;
222

    
223
      if( curmove<0)
224
        {
225
        curmove=nummove;
226
        return retInitializationString();
227
        }
228
      else
229
        {
230
        curmove++;
231
        return moves.substring(4*curmove,4*curmove+4);
232
        }
233
      }
234

    
235
  /////////////////////////////////////////////////////////////
236

    
237
    String retInitializationString()
238
      {
239
      return moves.substring(0,4*curmove);
240
      }
241
    }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

    
245
  private RubikPattern()
246
    {
247
    mPaint.setTextSize(24);
248
    mWidth = 300;
249
    mCategories = new Vector[NUM_CUBES];
250

    
251
    initializeCategories(0, RubikPatternData2.patterns);
252
    initializeCategories(1, RubikPatternData3.patterns);
253
    initializeCategories(2, RubikPatternData4.patterns);
254
    initializeCategories(3, RubikPatternData5.patterns);
255
    }
256

    
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258

    
259
  private void initializeCategories(int num, String[] pat)
260
    {
261
    int colon;
262
    mCategories[num] = new Vector<>();
263
    Category cat=null;
264
    String name, pattern;
265
    Pattern patt;
266

    
267
    numCategories[num]=0;
268

    
269
    for(String p: pat)
270
      {
271
      colon = p.indexOf(':');
272

    
273
      if( colon==-1 )
274
        {
275
        cat = new Category(p);
276
        mCategories[num].addElement(cat);
277
        numCategories[num]++;
278
        }
279
      else
280
        {
281
        pattern = p.substring(colon+1);
282
        name    = p.substring(0,colon);
283
        patt = new Pattern(name,pattern);
284
        cat.addPattern(patt);
285
        }
286
      }
287
    }
288

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

    
291
  private static String cutPatternName(String n)
292
    {
293
    int len1 = (int)mPaint.measureText(n);
294

    
295
    if( len1>mWidth )
296
      {
297
      int l = n.length();
298

    
299
      while( l>=2 && len1>mWidth )
300
        {
301
        l--;
302
        buffer = n.substring(0,l);
303
        len1 = (int)mPaint.measureText(buffer);
304
        }
305

    
306
      return buffer+"...";
307
      }
308

    
309
    return n;
310
    }
311

    
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313
// PUBLIC API
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315

    
316
  public static RubikPattern getInstance()
317
    {
318
    if( mThis==null )
319
      {
320
      mThis = new RubikPattern();
321
      }
322

    
323
    return mThis;
324
    }
325

    
326
///////////////////////////////////////////////////////////////////////////////////////////////////
327

    
328
  public int getNumCategories(int size)
329
    {
330
    return size>=0 && size<NUM_CUBES ? numCategories[size] : 0;
331
    }
332

    
333
///////////////////////////////////////////////////////////////////////////////////////////////////
334

    
335
  public String getCategoryName(int size,int num)
336
    {
337
    if( size>=0 && size<NUM_CUBES && num>=0 && num< numCategories[size] )
338
      {
339
      Category c = mCategories[size].elementAt(num);
340
      return c!=null ? c.getName() : null;
341
      }
342

    
343
    return null;
344
    }
345

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

    
348
  public String getPatternName(int size, int cat, int pat)
349
    {
350
    if( size>=0 && size<NUM_CUBES && cat>=0 && cat< numCategories[size] )
351
      {
352
      Category c = mCategories[size].elementAt(cat);
353
      return c!=null ? c.getPatternName(pat) : null;
354
      }
355

    
356
    return null;
357
    }
358

    
359
///////////////////////////////////////////////////////////////////////////////////////////////////
360

    
361
  public int getNumPatterns(int size, int num )
362
    {
363
    if( size>=0 && size<NUM_CUBES && num>=0 && num< numCategories[size] )
364
      {
365
      Category c = mCategories[size].elementAt(num);
366
      return c!=null ? c.getNumPatterns() : 0;
367
      }
368

    
369
    return 0;
370
    }
371

    
372
///////////////////////////////////////////////////////////////////////////////////////////////////
373

    
374
  public int getCurMove(int size, int cat, int pat)
375
    {
376
    if( size>=0 && size<NUM_CUBES && cat>=0 && cat< numCategories[size] )
377
      {
378
      Category c = mCategories[size].elementAt(cat);
379
      return c!=null ? c.getPatternCurMove(pat):0;
380
      }
381

    
382
    return 0;
383
    }
384

    
385
///////////////////////////////////////////////////////////////////////////////////////////////////
386

    
387
  public int getNumMoves(int size, int cat, int pat)
388
    {
389
    if( size>=0 && size<NUM_CUBES && cat>=0 && cat< numCategories[size] )
390
      {
391
      Category c = mCategories[size].elementAt(cat);
392
      return c!=null ? c.getPatternNumMove(pat):0;
393
      }
394

    
395
    return 0;
396
    }
397

    
398
///////////////////////////////////////////////////////////////////////////////////////////////////
399

    
400
  public String retNextMove(int size, int cat, int pat)
401
    {
402
    if( size>=0 && size<NUM_CUBES && cat>=0 && cat< numCategories[size] )
403
      {
404
      Category c = mCategories[size].elementAt(cat);
405
      if( c!=null ) return c.retNextMove(pat);
406
      }
407

    
408
    return "";
409
    }
410

    
411
///////////////////////////////////////////////////////////////////////////////////////////////////
412

    
413
  public String retPrevMove(int size, int cat, int pat)
414
    {
415
    if( size>=0 && size<NUM_CUBES && cat>=0 && cat< numCategories[size] )
416
      {
417
      Category c = mCategories[size].elementAt(cat);
418
      if( c!=null ) return c.retPrevMove(pat);
419
      }
420

    
421
    return "";
422
    }
423

    
424
///////////////////////////////////////////////////////////////////////////////////////////////////
425

    
426
  public String retInitializationString(int size,int cat, int pat)
427
    {
428
    if( size>=0 && size<NUM_CUBES && cat>=0 && cat< numCategories[size] )
429
      {
430
      Category c = mCategories[size].elementAt(cat);
431
      if( c!=null ) return c.retInitializationString(pat);
432
      }
433

    
434
    return "";
435
    }
436
}
(1-1/5)