Revision 4f470e00
Added by Leszek Koltunski about 4 years ago
src/main/java/org/distorted/patterns/RubikPattern.java | ||
---|---|---|
30 | 30 |
private static final int MAX_CUBE = 5; |
31 | 31 |
public static final int NUM_CUBES = MAX_CUBE-MIN_CUBE+1; |
32 | 32 |
|
33 |
public int[] numCategories = new int[NUM_CUBES];
|
|
33 |
private int[] numCategories = new int[NUM_CUBES];
|
|
34 | 34 |
private static String buffer=""; |
35 | 35 |
private static Paint mPaint = new Paint(); |
36 |
private static int width;
|
|
36 |
private static int mWidth;
|
|
37 | 37 |
|
38 | 38 |
private Vector<Category>[] mCategories; |
39 | 39 |
|
40 |
private static RubikPattern mThis; |
|
41 |
|
|
40 | 42 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
41 | 43 |
|
42 | 44 |
private class Category |
... | ... | |
46 | 48 |
private Vector<Pattern> patterns; |
47 | 49 |
private int curPattern; |
48 | 50 |
|
49 |
public Category(String n)
|
|
51 |
Category(String n) |
|
50 | 52 |
{ |
51 | 53 |
name=n; |
52 | 54 |
curPattern=0; |
53 | 55 |
numpatterns=0; |
54 | 56 |
patterns = new Vector<>(); |
55 | 57 |
} |
56 |
public void addPattern(Pattern p)
|
|
58 |
void addPattern(Pattern p) |
|
57 | 59 |
{ |
58 | 60 |
patterns.addElement(p); |
59 | 61 |
numpatterns++; |
60 | 62 |
} |
61 |
public int getNumPatterns()
|
|
63 |
int getNumPatterns() |
|
62 | 64 |
{ |
63 | 65 |
return numpatterns; |
64 |
};
|
|
65 |
public String getName()
|
|
66 |
} |
|
67 |
String getName() |
|
66 | 68 |
{ |
67 | 69 |
return name; |
68 | 70 |
} |
69 |
public void next()
|
|
71 |
void next() |
|
70 | 72 |
{ |
71 | 73 |
curPattern++; |
72 | 74 |
if( curPattern>=numpatterns ) |
73 | 75 |
curPattern=0; |
74 | 76 |
} |
75 |
public void prev()
|
|
77 |
void prev() |
|
76 | 78 |
{ |
77 | 79 |
curPattern--; |
78 | 80 |
if( curPattern<0 ) |
79 | 81 |
curPattern=numpatterns-1; |
80 | 82 |
} |
81 |
public String getPatternName()
|
|
83 |
String getPatternName() |
|
82 | 84 |
{ |
83 | 85 |
Pattern p = patterns.elementAt(curPattern); |
84 | 86 |
return p!=null ? p.getName(curPattern+1,numpatterns):null; |
85 | 87 |
} |
86 |
public int getPatternCurMove()
|
|
88 |
int getPatternCurMove() |
|
87 | 89 |
{ |
88 | 90 |
Pattern p = patterns.elementAt(curPattern); |
89 | 91 |
return p!=null ? p.getCurMove():-1; |
90 | 92 |
} |
91 |
public int getPatternNumMove()
|
|
93 |
int getPatternNumMove() |
|
92 | 94 |
{ |
93 | 95 |
Pattern p = patterns.elementAt(curPattern); |
94 | 96 |
return p!=null ? p.getNumMove():-1; |
95 | 97 |
} |
96 |
public void initializeCube()
|
|
98 |
void initializeCube() |
|
97 | 99 |
{ |
98 | 100 |
Pattern p = patterns.elementAt(curPattern); |
99 | 101 |
if( p!=null ) p.initializeCube(); |
100 | 102 |
} |
101 |
public void makeNextMove()
|
|
103 |
void makeNextMove() |
|
102 | 104 |
{ |
103 | 105 |
Pattern p = patterns.elementAt(curPattern); |
104 | 106 |
if( p!=null ) p.makeNextMove(); |
105 | 107 |
} |
106 |
public void makePrevMove()
|
|
108 |
void makePrevMove() |
|
107 | 109 |
{ |
108 | 110 |
Pattern p = patterns.elementAt(curPattern); |
109 | 111 |
if( p!=null ) p.makePrevMove(); |
... | ... | |
120 | 122 |
private int curmove; |
121 | 123 |
private int nummove; |
122 | 124 |
|
123 |
public Pattern(String n, String m)
|
|
125 |
Pattern(String n, String m) |
|
124 | 126 |
{ |
125 | 127 |
name=n; |
126 | 128 |
moves=m; |
127 | 129 |
nummove = moves.length()/4; |
128 | 130 |
curmove=nummove; |
129 | 131 |
} |
130 |
public String getName(int cur,int num)
|
|
132 |
String getName(int cur,int num) |
|
131 | 133 |
{ |
132 | 134 |
if( shortname==null ) shortname=cutPatternName(name); |
133 | 135 |
return shortname; |
134 | 136 |
} |
135 |
public int getNumMove()
|
|
137 |
int getNumMove() |
|
136 | 138 |
{ |
137 | 139 |
return nummove; |
138 | 140 |
} |
139 |
public int getCurMove()
|
|
141 |
int getCurMove() |
|
140 | 142 |
{ |
141 | 143 |
return curmove; |
142 | 144 |
} |
143 |
public void makeNextMove()
|
|
145 |
void makeNextMove() |
|
144 | 146 |
{ |
145 | 147 |
curmove++; |
146 | 148 |
if( curmove>nummove) |
... | ... | |
150 | 152 |
} |
151 | 153 |
else |
152 | 154 |
{ |
153 |
|
|
154 | 155 |
// TODO |
155 | 156 |
// RubikCube cube = world.getCube(); |
156 | 157 |
// if( cube!=null && !cube.makeMove(moves.substring(4*curmove-4,4*curmove)) ) |
157 | 158 |
curmove--; |
158 | 159 |
} |
159 | 160 |
} |
160 |
public void makePrevMove()
|
|
161 |
void makePrevMove() |
|
161 | 162 |
{ |
162 | 163 |
curmove--; |
163 | 164 |
if( curmove<0) |
... | ... | |
173 | 174 |
curmove++; |
174 | 175 |
} |
175 | 176 |
} |
176 |
public void initializeCube()
|
|
177 |
void initializeCube() |
|
177 | 178 |
{ |
178 | 179 |
// TODO |
179 | 180 |
// RubikCube cube = world.getCube(); |
... | ... | |
183 | 184 |
|
184 | 185 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
185 | 186 |
|
186 |
public RubikPattern(int wi, int h)
|
|
187 |
private RubikPattern()
|
|
187 | 188 |
{ |
188 |
width = wi; |
|
189 |
mPaint.setTextSize(24); |
|
190 |
mWidth = 200; |
|
189 | 191 |
mCategories = new Vector[NUM_CUBES]; |
190 |
mPaint.setTextSize(h); |
|
191 |
|
|
192 |
|
|
192 | 193 |
initializeCategories(0, RubikPatternData2.patterns); |
193 | 194 |
initializeCategories(1, RubikPatternData3.patterns); |
194 | 195 |
initializeCategories(2, RubikPatternData4.patterns); |
... | ... | |
232 | 233 |
private static String cutPatternName(String n) |
233 | 234 |
{ |
234 | 235 |
int len1 = (int)mPaint.measureText(n); |
235 |
|
|
236 |
if( len1>width )
|
|
236 |
|
|
237 |
if( len1>mWidth )
|
|
237 | 238 |
{ |
238 | 239 |
int l = n.length(); |
239 | 240 |
|
240 |
while( l>=2 && len1>width )
|
|
241 |
while( l>=2 && len1>mWidth )
|
|
241 | 242 |
{ |
242 | 243 |
l--; |
243 | 244 |
buffer = n.substring(0,l); |
... | ... | |
250 | 251 |
return n; |
251 | 252 |
} |
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 |
|
|
253 | 268 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
254 | 269 |
|
255 | 270 |
public String getCategoryName(int size,int num) |
256 | 271 |
{ |
257 |
if( size>=MIN_CUBE && size<=MAX_CUBE && num>=0 && num< numCategories[size-MIN_CUBE] )
|
|
272 |
if( size>=0 && size<NUM_CUBES && num>=0 && num< numCategories[size] )
|
|
258 | 273 |
{ |
259 |
Category c = mCategories[size-MIN_CUBE].elementAt(num);
|
|
274 |
Category c = mCategories[size].elementAt(num); |
|
260 | 275 |
return c!=null ? c.getName() : null; |
261 | 276 |
} |
262 | 277 |
|
... | ... | |
267 | 282 |
|
268 | 283 |
public String getPatternName(int size,int num) |
269 | 284 |
{ |
270 |
if( size>=MIN_CUBE && size<=MAX_CUBE && num>=0 && num< numCategories[size-MIN_CUBE] )
|
|
285 |
if( size>=0 && size<NUM_CUBES && num>=0 && num< numCategories[size] )
|
|
271 | 286 |
{ |
272 |
Category c = mCategories[size-MIN_CUBE].elementAt(num);
|
|
287 |
Category c = mCategories[size].elementAt(num); |
|
273 | 288 |
return c!=null ? c.getPatternName() : null; |
274 | 289 |
} |
275 | 290 |
|
... | ... | |
280 | 295 |
|
281 | 296 |
public int getNumPatterns(int size, int num ) |
282 | 297 |
{ |
283 |
if( size>=MIN_CUBE && size<=MAX_CUBE && num>=0 && num< numCategories[size-MIN_CUBE] )
|
|
298 |
if( size>=0 && size<NUM_CUBES && num>=0 && num< numCategories[size] )
|
|
284 | 299 |
{ |
285 |
Category c = mCategories[size-MIN_CUBE].elementAt(num);
|
|
300 |
Category c = mCategories[size].elementAt(num); |
|
286 | 301 |
return c!=null ? c.getNumPatterns() : 0; |
287 | 302 |
} |
288 | 303 |
|
... | ... | |
293 | 308 |
|
294 | 309 |
public void nextPattern(int size, int num ) |
295 | 310 |
{ |
296 |
if( size>=MIN_CUBE && size<=MAX_CUBE && num>=0 && num< numCategories[size-MIN_CUBE] )
|
|
311 |
if( size>=0 && size<NUM_CUBES && num>=0 && num< numCategories[size] )
|
|
297 | 312 |
{ |
298 |
Category c = mCategories[size-MIN_CUBE].elementAt(num);
|
|
313 |
Category c = mCategories[size].elementAt(num); |
|
299 | 314 |
|
300 | 315 |
if( c!=null ) |
301 | 316 |
{ |
... | ... | |
311 | 326 |
|
312 | 327 |
public void prevPattern(int size, int num ) |
313 | 328 |
{ |
314 |
if( size>=MIN_CUBE && size<=MAX_CUBE && num>=0 && num< numCategories[size-MIN_CUBE] )
|
|
329 |
if( size>=0 && size<NUM_CUBES && num>=0 && num< numCategories[size] )
|
|
315 | 330 |
{ |
316 |
Category c = mCategories[size-MIN_CUBE].elementAt(num);
|
|
331 |
Category c = mCategories[size].elementAt(num); |
|
317 | 332 |
|
318 | 333 |
if( c!=null ) |
319 | 334 |
{ |
... | ... | |
329 | 344 |
|
330 | 345 |
public int getCurrPattern(int size, int num ) |
331 | 346 |
{ |
332 |
if( size>=MIN_CUBE && size<=MAX_CUBE && num>=0 && num< numCategories[size-MIN_CUBE] )
|
|
347 |
if( size>=0 && size<NUM_CUBES && num>=0 && num< numCategories[size] )
|
|
333 | 348 |
{ |
334 |
Category c = mCategories[size-MIN_CUBE].elementAt(num);
|
|
349 |
Category c = mCategories[size].elementAt(num); |
|
335 | 350 |
return c!=null ? c.curPattern:0; |
336 | 351 |
} |
337 | 352 |
|
... | ... | |
342 | 357 |
|
343 | 358 |
public int getCurMove(int size, int num ) |
344 | 359 |
{ |
345 |
if( size>=MIN_CUBE && size<=MAX_CUBE && num>=0 && num< numCategories[size-MIN_CUBE] )
|
|
360 |
if( size>=0 && size<NUM_CUBES && num>=0 && num< numCategories[size] )
|
|
346 | 361 |
{ |
347 |
Category c = mCategories[size-MIN_CUBE].elementAt(num);
|
|
362 |
Category c = mCategories[size].elementAt(num); |
|
348 | 363 |
return c!=null ? c.getPatternCurMove():0; |
349 | 364 |
} |
350 | 365 |
|
... | ... | |
355 | 370 |
|
356 | 371 |
public int getNumMove(int size, int num ) |
357 | 372 |
{ |
358 |
if( size>=MIN_CUBE && size<=MAX_CUBE && num>=0 && num< numCategories[size-MIN_CUBE] )
|
|
373 |
if( size>=0 && size<NUM_CUBES && num>=0 && num< numCategories[size] )
|
|
359 | 374 |
{ |
360 |
Category c = mCategories[size-MIN_CUBE].elementAt(num);
|
|
375 |
Category c = mCategories[size].elementAt(num); |
|
361 | 376 |
return c!=null ? c.getPatternNumMove():0; |
362 | 377 |
} |
363 | 378 |
|
... | ... | |
368 | 383 |
|
369 | 384 |
public void makeNextMove(int size, int num) |
370 | 385 |
{ |
371 |
if( size>=MIN_CUBE && size<=MAX_CUBE && num>=0 && num< numCategories[size-MIN_CUBE] )
|
|
386 |
if( size>=0 && size<NUM_CUBES && num>=0 && num< numCategories[size] )
|
|
372 | 387 |
{ |
373 |
Category c = mCategories[size-MIN_CUBE].elementAt(num);
|
|
388 |
Category c = mCategories[size].elementAt(num); |
|
374 | 389 |
if( c!=null ) c.makeNextMove(); |
375 | 390 |
} |
376 | 391 |
} |
... | ... | |
379 | 394 |
|
380 | 395 |
public void makePrevMove(int size, int num) |
381 | 396 |
{ |
382 |
if( size>=MIN_CUBE && size<=MAX_CUBE && num>=0 && num< numCategories[size-MIN_CUBE] )
|
|
397 |
if( size>=0 && size<NUM_CUBES && num>=0 && num< numCategories[size] )
|
|
383 | 398 |
{ |
384 |
Category c = mCategories[size-MIN_CUBE].elementAt(num);
|
|
399 |
Category c = mCategories[size].elementAt(num); |
|
385 | 400 |
if( c!=null ) c.makePrevMove(); |
386 | 401 |
} |
387 | 402 |
} |
... | ... | |
390 | 405 |
|
391 | 406 |
public void initializeCube(int size,int num) |
392 | 407 |
{ |
393 |
if( size>=MIN_CUBE && size<=MAX_CUBE && num>=0 && num< numCategories[size-MIN_CUBE] )
|
|
408 |
if( size>=0 && size<NUM_CUBES && num>=0 && num< numCategories[size] )
|
|
394 | 409 |
{ |
395 |
Category c = mCategories[size-MIN_CUBE].elementAt(num);
|
|
410 |
Category c = mCategories[size].elementAt(num); |
|
396 | 411 |
if( c!=null ) c.initializeCube(); |
397 | 412 |
} |
398 | 413 |
} |
414 |
|
|
415 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
416 |
|
|
417 |
public int getNumCategories(int size) |
|
418 |
{ |
|
419 |
return size>=0 && size<NUM_CUBES ? numCategories[size] : 0; |
|
420 |
} |
|
399 | 421 |
} |
Also available in: Unified diff
Progress with Pretty Patterns.