Project

General

Profile

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

magiccube / src / main / java / org / distorted / patterns / RubikPattern.java @ d18993ac

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
  private static final int MIN_CUBE  = 2;
30
  private static final int MAX_CUBE  = 5;
31
  public  static final int NUM_CUBES = MAX_CUBE-MIN_CUBE+1;
32

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

    
38
  private Vector<Category>[] mCategories;
39

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

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

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

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114

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

    
123
    public Pattern(String n, String m)
124
      {
125
      name=n;
126
      moves=m;
127
      nummove = moves.length()/4;
128
      curmove=nummove;
129
      }
130
    public String getName(int cur,int num)
131
      {
132
      if( shortname==null ) shortname=cutPatternName(name);
133
      return shortname;
134
      }
135
    public int getNumMove()
136
    {
137
    return nummove;
138
    }
139
    public int getCurMove()
140
    {
141
    return curmove;
142
    }
143
    public void makeNextMove()
144
      {
145
      curmove++;
146
      if( curmove>nummove)
147
        {
148
        curmove= 0;
149
        initializeCube();
150
        }
151
      else
152
        {
153

    
154
// TODO
155
//        RubikCube cube = world.getCube();
156
//        if( cube!=null && !cube.makeMove(moves.substring(4*curmove-4,4*curmove)) )
157
          curmove--;
158
        }
159
      }
160
    public void makePrevMove()
161
      {
162
      curmove--;
163
      if( curmove<0)
164
        {
165
        curmove=nummove;
166
        initializeCube();
167
        }
168
      else
169
        {
170
// TODO
171
//        RubikCube cube = world.getCube();
172
//        if( cube!=null && !cube.backMove(moves.substring(4*curmove,4*curmove+4)) )
173
          curmove++;
174
        }
175
      }
176
    public void initializeCube()
177
      {
178
// TODO
179
//	    RubikCube cube = world.getCube();
180
//      if( cube!=null ) cube.setupPosition(moves.substring(0,4*curmove));
181
      }
182
    }
183

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

    
186
  public RubikPattern(int wi, int h)
187
    {
188
    width = wi;
189
    mCategories = new Vector[NUM_CUBES];
190
    mPaint.setTextSize(h);
191
  
192
    initializeCategories(0, RubikPatternData2.patterns);
193
    initializeCategories(1, RubikPatternData3.patterns);
194
    initializeCategories(2, RubikPatternData4.patterns);
195
    initializeCategories(3, RubikPatternData5.patterns);
196
    }
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

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

    
208
    numCategories[num]=0;
209

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

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

    
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231

    
232
  private static String cutPatternName(String n)
233
    {
234
    int len1 = (int)mPaint.measureText(n);
235
  
236
    if( len1>width )
237
      {
238
      int l = n.length();
239

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

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

    
250
    return n;
251
    }
252

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

    
255
  public String getCategoryName(int size,int num)
256
    {
257
    if( size>=MIN_CUBE && size<=MAX_CUBE && num>=0 && num< numCategories[size-MIN_CUBE] )
258
      {
259
      Category c = mCategories[size-MIN_CUBE].elementAt(num);
260
      return c!=null ? c.getName() : null;
261
      }
262

    
263
    return null;
264
    }
265

    
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267

    
268
  public String getPatternName(int size,int num)
269
    {
270
    if( size>=MIN_CUBE && size<=MAX_CUBE && num>=0 && num< numCategories[size-MIN_CUBE] )
271
      {
272
      Category c = mCategories[size-MIN_CUBE].elementAt(num);
273
      return c!=null ? c.getPatternName() : null;
274
      }
275

    
276
    return null;
277
    }
278

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

    
281
  public int getNumPatterns(int size, int num )
282
    {
283
    if( size>=MIN_CUBE && size<=MAX_CUBE && num>=0 && num< numCategories[size-MIN_CUBE] )
284
      {
285
      Category c = mCategories[size-MIN_CUBE].elementAt(num);
286
      return c!=null ? c.getNumPatterns() : 0;
287
      }
288

    
289
    return 0;
290
    }
291

    
292
///////////////////////////////////////////////////////////////////////////////////////////////////
293

    
294
  public void nextPattern(int size, int num )
295
    {
296
    if( size>=MIN_CUBE && size<=MAX_CUBE && num>=0 && num< numCategories[size-MIN_CUBE] )
297
      {
298
      Category c = mCategories[size-MIN_CUBE].elementAt(num);
299

    
300
      if( c!=null )
301
        {
302
// TODO
303
//        RubikMenu.texDirty[0] = true;
304
        c.next();
305
        c.initializeCube();
306
        }
307
      }
308
    }
309

    
310
///////////////////////////////////////////////////////////////////////////////////////////////////
311

    
312
  public void prevPattern(int size, int num )
313
    {
314
    if( size>=MIN_CUBE && size<=MAX_CUBE && num>=0 && num< numCategories[size-MIN_CUBE] )
315
      {
316
      Category c = mCategories[size-MIN_CUBE].elementAt(num);
317

    
318
      if( c!=null )
319
        {
320
// TODO
321
//        RubikMenu.texDirty[0] = true;
322
        c.prev();
323
        c.initializeCube();
324
        }
325
      }
326
    }
327

    
328
///////////////////////////////////////////////////////////////////////////////////////////////////
329

    
330
  public int getCurrPattern(int size, int num )
331
    {
332
    if( size>=MIN_CUBE && size<=MAX_CUBE && num>=0 && num< numCategories[size-MIN_CUBE] )
333
      {
334
      Category c = mCategories[size-MIN_CUBE].elementAt(num);
335
      return c!=null ? c.curPattern:0;
336
      }
337

    
338
    return 0;
339
    }
340

    
341
///////////////////////////////////////////////////////////////////////////////////////////////////
342

    
343
  public int getCurMove(int size, int num )
344
    {
345
    if( size>=MIN_CUBE && size<=MAX_CUBE && num>=0 && num< numCategories[size-MIN_CUBE] )
346
      {
347
      Category c = mCategories[size-MIN_CUBE].elementAt(num);
348
      return c!=null ? c.getPatternCurMove():0;
349
      }
350

    
351
    return 0;
352
    }
353

    
354
///////////////////////////////////////////////////////////////////////////////////////////////////
355

    
356
  public int getNumMove(int size, int num )
357
    {
358
    if( size>=MIN_CUBE && size<=MAX_CUBE && num>=0 && num< numCategories[size-MIN_CUBE] )
359
      {
360
      Category c = mCategories[size-MIN_CUBE].elementAt(num);
361
      return c!=null ? c.getPatternNumMove():0;
362
      }
363

    
364
    return 0;
365
    }
366

    
367
///////////////////////////////////////////////////////////////////////////////////////////////////
368

    
369
  public void makeNextMove(int size, int num)
370
    {
371
    if( size>=MIN_CUBE && size<=MAX_CUBE && num>=0 && num< numCategories[size-MIN_CUBE] )
372
      {
373
      Category c = mCategories[size-MIN_CUBE].elementAt(num);
374
      if( c!=null ) c.makeNextMove();
375
      }
376
    }
377

    
378
///////////////////////////////////////////////////////////////////////////////////////////////////
379

    
380
  public void makePrevMove(int size, int num)
381
    {
382
    if( size>=MIN_CUBE && size<=MAX_CUBE && num>=0 && num< numCategories[size-MIN_CUBE] )
383
      {
384
      Category c = mCategories[size-MIN_CUBE].elementAt(num);
385
      if( c!=null ) c.makePrevMove();
386
      }
387
    }
388

    
389
///////////////////////////////////////////////////////////////////////////////////////////////////
390

    
391
  public void initializeCube(int size,int num)
392
    {
393
    if( size>=MIN_CUBE && size<=MAX_CUBE && num>=0 && num< numCategories[size-MIN_CUBE] )
394
      {
395
      Category c = mCategories[size-MIN_CUBE].elementAt(num);
396
      if( c!=null ) c.initializeCube();
397
      }
398
    }
399
}
(1-1/5)