Revision 48145a5b
Added by Leszek Koltunski about 4 years ago
src/main/java/org/distorted/patterns/RubikPattern.java | ||
---|---|---|
22 | 22 |
import org.distorted.magic.RubikPostRender; |
23 | 23 |
import org.distorted.object.RubikObject; |
24 | 24 |
|
25 |
import java.util.Vector; |
|
25 |
import java.util.ArrayList; |
|
26 |
import java.util.List; |
|
26 | 27 |
|
27 | 28 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
28 | 29 |
|
... | ... | |
38 | 39 |
private int[] currentCategory = new int[NUM_CUBES]; |
39 | 40 |
private int[] currentScrollPos= new int[NUM_CUBES]; |
40 | 41 |
|
41 |
private Vector<Category>[] mCategories;
|
|
42 |
private List<List<Category>> mCategories;
|
|
42 | 43 |
private static RubikPattern mThis; |
43 | 44 |
|
44 | 45 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
45 | 46 |
|
46 | 47 |
private static class Category |
47 | 48 |
{ |
48 |
private String name;
|
|
49 |
private String[] mLines;
|
|
49 | 50 |
private int numPatterns; |
50 |
private Vector<Pattern> patterns; |
|
51 |
private ArrayList<Pattern> mPatterns; |
|
52 |
private boolean mInitialized; |
|
51 | 53 |
|
52 | 54 |
///////////////////////////////////////////////////////////// |
53 | 55 |
|
54 |
Category(String n)
|
|
56 |
Category(String[] lines)
|
|
55 | 57 |
{ |
56 |
name=n; |
|
57 |
numPatterns=0; |
|
58 |
patterns = new Vector<>(); |
|
58 |
mLines=lines; |
|
59 |
numPatterns=lines.length-1; |
|
60 |
mPatterns = new ArrayList<>(); |
|
61 |
mInitialized = false; |
|
59 | 62 |
} |
60 | 63 |
|
61 | 64 |
///////////////////////////////////////////////////////////// |
62 | 65 |
|
63 |
void addPattern(Pattern p)
|
|
66 |
void initialize()
|
|
64 | 67 |
{ |
65 |
patterns.addElement(p); |
|
66 |
numPatterns++; |
|
68 |
int colon; |
|
69 |
String moves, name; |
|
70 |
Pattern pattern; |
|
71 |
|
|
72 |
for(int i=0; i<numPatterns; i++) |
|
73 |
{ |
|
74 |
colon = mLines[i+1].indexOf(":"); |
|
75 |
|
|
76 |
if( colon!=-1 ) |
|
77 |
{ |
|
78 |
moves = mLines[i+1].substring(colon+1); |
|
79 |
name = mLines[i+1].substring(0,colon); |
|
80 |
pattern = new Pattern(name,moves); |
|
81 |
|
|
82 |
mPatterns.add(pattern); |
|
83 |
} |
|
84 |
else |
|
85 |
{ |
|
86 |
numPatterns--; |
|
87 |
} |
|
88 |
} |
|
89 |
|
|
90 |
mInitialized = true; |
|
67 | 91 |
} |
68 | 92 |
|
69 | 93 |
///////////////////////////////////////////////////////////// |
70 | 94 |
|
71 | 95 |
int getNumPatterns() |
72 |
{ |
|
73 |
return numPatterns; |
|
74 |
} |
|
96 |
{
|
|
97 |
return numPatterns;
|
|
98 |
}
|
|
75 | 99 |
|
76 | 100 |
///////////////////////////////////////////////////////////// |
77 | 101 |
|
78 | 102 |
String getName() |
79 |
{ |
|
80 |
return name;
|
|
81 |
} |
|
103 |
{
|
|
104 |
return mLines[0];
|
|
105 |
}
|
|
82 | 106 |
|
83 | 107 |
///////////////////////////////////////////////////////////// |
84 | 108 |
|
85 | 109 |
String getPatternName(int pattern) |
86 | 110 |
{ |
111 |
if( !mInitialized ) initialize(); |
|
112 |
|
|
87 | 113 |
if( pattern>=0 && pattern<numPatterns ) |
88 | 114 |
{ |
89 |
Pattern p = patterns.elementAt(pattern);
|
|
115 |
Pattern p = mPatterns.get(pattern);
|
|
90 | 116 |
return p!=null ? p.getName():""; |
91 | 117 |
} |
118 |
|
|
92 | 119 |
return ""; |
93 | 120 |
} |
94 | 121 |
|
... | ... | |
96 | 123 |
|
97 | 124 |
int getPatternCurMove(int pattern) |
98 | 125 |
{ |
126 |
if( !mInitialized ) initialize(); |
|
127 |
|
|
99 | 128 |
if( pattern>=0 && pattern<numPatterns ) |
100 | 129 |
{ |
101 |
Pattern p = patterns.elementAt(pattern);
|
|
130 |
Pattern p = mPatterns.get(pattern);
|
|
102 | 131 |
return p!=null ? p.getCurMove():-1; |
103 | 132 |
} |
133 |
|
|
104 | 134 |
return -1; |
105 | 135 |
} |
106 | 136 |
|
... | ... | |
108 | 138 |
|
109 | 139 |
int getPatternNumMove(int pattern) |
110 | 140 |
{ |
141 |
if( !mInitialized ) initialize(); |
|
142 |
|
|
111 | 143 |
if( pattern>=0 && pattern<numPatterns ) |
112 | 144 |
{ |
113 |
Pattern p = patterns.elementAt(pattern);
|
|
145 |
Pattern p = mPatterns.get(pattern);
|
|
114 | 146 |
return p!=null ? p.getNumMove():-1; |
115 | 147 |
} |
148 |
|
|
116 | 149 |
return -1; |
117 | 150 |
} |
118 | 151 |
|
... | ... | |
120 | 153 |
|
121 | 154 |
void makeMove(RubikPostRender post, int pattern) |
122 | 155 |
{ |
156 |
if( !mInitialized ) initialize(); |
|
157 |
|
|
123 | 158 |
if( pattern>=0 && pattern<numPatterns ) |
124 | 159 |
{ |
125 |
Pattern p = patterns.elementAt(pattern);
|
|
160 |
Pattern p = mPatterns.get(pattern);
|
|
126 | 161 |
if( p!=null ) p.makeMove(post); |
127 | 162 |
} |
128 | 163 |
} |
... | ... | |
131 | 166 |
|
132 | 167 |
void backMove(RubikPostRender post, int pattern) |
133 | 168 |
{ |
169 |
if( !mInitialized ) initialize(); |
|
170 |
|
|
134 | 171 |
if( pattern>=0 && pattern<numPatterns ) |
135 | 172 |
{ |
136 |
Pattern p = patterns.elementAt(pattern);
|
|
173 |
Pattern p = mPatterns.get(pattern);
|
|
137 | 174 |
if( p!=null ) p.backMove(post); |
138 | 175 |
} |
139 | 176 |
} |
... | ... | |
142 | 179 |
|
143 | 180 |
int[][] reInitialize(int pattern) |
144 | 181 |
{ |
182 |
if( !mInitialized ) initialize(); |
|
183 |
|
|
145 | 184 |
if( pattern>=0 && pattern<numPatterns ) |
146 | 185 |
{ |
147 |
Pattern p = patterns.elementAt(pattern);
|
|
186 |
Pattern p = mPatterns.get(pattern);
|
|
148 | 187 |
if( p!=null ) return p.reInitialize(); |
149 | 188 |
} |
150 | 189 |
|
... | ... | |
175 | 214 |
|
176 | 215 |
///////////////////////////////////////////////////////////// |
177 | 216 |
|
178 |
private int[][] movesParser(String moves)
|
|
217 |
private void initialize()
|
|
179 | 218 |
{ |
180 |
numMove = moves.length()/4; |
|
181 |
curMove=numMove; |
|
219 |
numMove = moveStr.length()/4; |
|
220 |
moves = new int[numMove][3]; |
|
221 |
curMove = numMove; |
|
182 | 222 |
|
183 | 223 |
int digit0, digit1, digit2; |
184 |
int[][] result = new int[numMove][3]; |
|
185 | 224 |
|
186 | 225 |
for(int i=0; i<numMove; i++) |
187 | 226 |
{ |
188 |
digit0 = moves.charAt(4*i+1)-'0';
|
|
189 |
digit1 = moves.charAt(4*i+2)-'0';
|
|
190 |
digit2 = moves.charAt(4*i+3)-'0';
|
|
227 |
digit0 = moveStr.charAt(4*i+1)-'0';
|
|
228 |
digit1 = moveStr.charAt(4*i+2)-'0';
|
|
229 |
digit2 = moveStr.charAt(4*i+3)-'0';
|
|
191 | 230 |
|
192 |
result[i][0] = (10*digit0+digit1)/32;
|
|
193 |
result[i][1] = (10*digit0+digit1)%32;
|
|
194 |
result[i][2] = 2-digit2;
|
|
231 |
moves[i][0] = (10*digit0+digit1)/32;
|
|
232 |
moves[i][1] = (10*digit0+digit1)%32;
|
|
233 |
moves[i][2] = 2-digit2;
|
|
195 | 234 |
} |
196 | 235 |
|
197 |
return result; |
|
236 |
moveStr = null; |
|
237 |
mInitialized = true; |
|
198 | 238 |
} |
199 | 239 |
|
200 | 240 |
///////////////////////////////////////////////////////////// |
... | ... | |
208 | 248 |
|
209 | 249 |
int getNumMove() |
210 | 250 |
{ |
211 |
if( !mInitialized ) |
|
212 |
{ |
|
213 |
mInitialized = true; |
|
214 |
moves = movesParser(moveStr); |
|
215 |
moveStr = null; |
|
216 |
} |
|
251 |
if( !mInitialized ) initialize(); |
|
217 | 252 |
|
218 | 253 |
return numMove; |
219 | 254 |
} |
... | ... | |
222 | 257 |
|
223 | 258 |
int getCurMove() |
224 | 259 |
{ |
225 |
if( !mInitialized ) |
|
226 |
{ |
|
227 |
mInitialized = true; |
|
228 |
moves = movesParser(moveStr); |
|
229 |
moveStr = null; |
|
230 |
} |
|
260 |
if( !mInitialized ) initialize(); |
|
231 | 261 |
|
232 | 262 |
return curMove; |
233 | 263 |
} |
... | ... | |
236 | 266 |
|
237 | 267 |
void makeMove(RubikPostRender post) |
238 | 268 |
{ |
239 |
if( !mInitialized ) |
|
240 |
{ |
|
241 |
mInitialized = true; |
|
242 |
moves = movesParser(moveStr); |
|
243 |
moveStr = null; |
|
244 |
} |
|
269 |
if( !mInitialized ) initialize(); |
|
245 | 270 |
|
246 | 271 |
curMove++; |
247 | 272 |
RubikObject object = post.getObject(); |
... | ... | |
283 | 308 |
|
284 | 309 |
void backMove(RubikPostRender post) |
285 | 310 |
{ |
286 |
if( !mInitialized ) |
|
287 |
{ |
|
288 |
mInitialized = true; |
|
289 |
moves = movesParser(moveStr); |
|
290 |
moveStr = null; |
|
291 |
} |
|
311 |
if( !mInitialized ) initialize(); |
|
292 | 312 |
|
293 | 313 |
curMove--; |
294 | 314 |
RubikObject object = post.getObject(); |
... | ... | |
330 | 350 |
|
331 | 351 |
int[][] reInitialize() |
332 | 352 |
{ |
333 |
if( !mInitialized ) |
|
334 |
{ |
|
335 |
mInitialized = true; |
|
336 |
moves = movesParser(moveStr); |
|
337 |
moveStr = null; |
|
338 |
} |
|
353 |
if( !mInitialized ) initialize(); |
|
339 | 354 |
|
340 | 355 |
mCanRotate = true; |
341 | 356 |
curMove = numMove; |
... | ... | |
354 | 369 |
|
355 | 370 |
private RubikPattern() |
356 | 371 |
{ |
357 |
mCategories = new Vector[NUM_CUBES];
|
|
372 |
mCategories = new ArrayList<>();
|
|
358 | 373 |
|
359 |
initializeCategories(0, RubikPatternData2.patterns);
|
|
360 |
initializeCategories(1, RubikPatternData3.patterns);
|
|
361 |
initializeCategories(2, RubikPatternData4.patterns);
|
|
362 |
initializeCategories(3, RubikPatternData5.patterns);
|
|
374 |
initializeCategories(0, RubikPatternCube2.patterns);
|
|
375 |
initializeCategories(1, RubikPatternCube3.patterns);
|
|
376 |
initializeCategories(2, RubikPatternCube4.patterns);
|
|
377 |
initializeCategories(3, RubikPatternCube5.patterns);
|
|
363 | 378 |
} |
364 | 379 |
|
365 | 380 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
366 | 381 |
|
367 |
private void initializeCategories(int num, String[] pat) |
|
382 |
private void initializeCategories(int num, String[][] pat)
|
|
368 | 383 |
{ |
369 |
int colon; |
|
370 |
mCategories[num] = new Vector<>(); |
|
371 |
Category cat=null; |
|
372 |
String name, pattern; |
|
373 |
Pattern patt; |
|
374 |
|
|
375 |
numCategories[num]=0; |
|
384 |
List<Category> list = new ArrayList<>(); |
|
385 |
Category cat; |
|
376 | 386 |
|
377 |
for(String p: pat)
|
|
387 |
for(String[] lines: pat)
|
|
378 | 388 |
{ |
379 |
colon = p.indexOf(':'); |
|
380 |
|
|
381 |
if( colon==-1 ) |
|
382 |
{ |
|
383 |
cat = new Category(p); |
|
384 |
mCategories[num].addElement(cat); |
|
385 |
numCategories[num]++; |
|
386 |
} |
|
387 |
else |
|
388 |
{ |
|
389 |
pattern = p.substring(colon+1); |
|
390 |
name = p.substring(0,colon); |
|
391 |
patt = new Pattern(name,pattern); |
|
392 |
cat.addPattern(patt); |
|
393 |
} |
|
389 |
cat = new Category(lines); |
|
390 |
list.add(cat); |
|
394 | 391 |
} |
392 |
|
|
393 |
mCategories.add(num,list); |
|
394 |
numCategories[num]=pat.length; |
|
395 | 395 |
} |
396 | 396 |
|
397 | 397 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
410 | 410 |
|
411 | 411 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
412 | 412 |
|
413 |
public int getNumCategories(int size)
|
|
413 |
public int getNumCategories(int tab)
|
|
414 | 414 |
{ |
415 |
return size>=0 && size<NUM_CUBES ? numCategories[size] : 0;
|
|
415 |
return tab>=0 && tab<NUM_CUBES ? numCategories[tab] : 0;
|
|
416 | 416 |
} |
417 | 417 |
|
418 | 418 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
419 | 419 |
|
420 |
public String getCategoryName(int size,int num)
|
|
420 |
public String getCategoryName(int tab, int cat)
|
|
421 | 421 |
{ |
422 |
if( size>=0 && size<NUM_CUBES && num>=0 && num< numCategories[size] )
|
|
422 |
if( tab>=0 && tab<NUM_CUBES && cat>=0 && cat< numCategories[tab] )
|
|
423 | 423 |
{ |
424 |
Category c = mCategories[size].elementAt(num);
|
|
424 |
Category c = mCategories.get(tab).get(cat);
|
|
425 | 425 |
return c!=null ? c.getName() : null; |
426 | 426 |
} |
427 | 427 |
|
... | ... | |
430 | 430 |
|
431 | 431 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
432 | 432 |
|
433 |
public void rememberState(int size,int num, int scrollPos)
|
|
433 |
public void rememberState(int tab, int cat, int scrollPos)
|
|
434 | 434 |
{ |
435 |
if( size>=0 && size<NUM_CUBES )
|
|
435 |
if( tab>=0 && tab<NUM_CUBES )
|
|
436 | 436 |
{ |
437 |
currentCategory[size] = num;
|
|
438 |
currentScrollPos[size]= scrollPos;
|
|
437 |
currentCategory[tab] = cat;
|
|
438 |
currentScrollPos[tab]= scrollPos;
|
|
439 | 439 |
} |
440 | 440 |
} |
441 | 441 |
|
442 | 442 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
443 | 443 |
|
444 |
public int recallCategory(int size)
|
|
444 |
public int recallCategory(int tab)
|
|
445 | 445 |
{ |
446 |
return size>=0 && size<NUM_CUBES ? currentCategory[size] : 0;
|
|
446 |
return tab>=0 && tab<NUM_CUBES ? currentCategory[tab] : 0;
|
|
447 | 447 |
} |
448 | 448 |
|
449 | 449 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
450 | 450 |
|
451 |
public int recallScrollPos(int size)
|
|
451 |
public int recallScrollPos(int tab)
|
|
452 | 452 |
{ |
453 |
return size>=0 && size<NUM_CUBES ? currentScrollPos[size] : 0;
|
|
453 |
return tab>=0 && tab<NUM_CUBES ? currentScrollPos[tab] : 0;
|
|
454 | 454 |
} |
455 | 455 |
|
456 | 456 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
457 | 457 |
|
458 |
public String getPatternName(int size, int cat, int pat)
|
|
458 |
public String getPatternName(int tab, int cat, int pat)
|
|
459 | 459 |
{ |
460 |
if( size>=0 && size<NUM_CUBES && cat>=0 && cat< numCategories[size] )
|
|
460 |
if( tab>=0 && tab<NUM_CUBES && cat>=0 && cat< numCategories[tab] )
|
|
461 | 461 |
{ |
462 |
Category c = mCategories[size].elementAt(cat);
|
|
462 |
Category c = mCategories.get(tab).get(cat);
|
|
463 | 463 |
return c!=null ? c.getPatternName(pat) : null; |
464 | 464 |
} |
465 | 465 |
|
... | ... | |
468 | 468 |
|
469 | 469 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
470 | 470 |
|
471 |
public int getNumPatterns(int size, int num )
|
|
471 |
public int getNumPatterns(int tab, int cat)
|
|
472 | 472 |
{ |
473 |
if( size>=0 && size<NUM_CUBES && num>=0 && num< numCategories[size] )
|
|
473 |
if( tab>=0 && tab<NUM_CUBES && cat>=0 && cat< numCategories[tab] )
|
|
474 | 474 |
{ |
475 |
Category c = mCategories[size].elementAt(num);
|
|
475 |
Category c = mCategories.get(tab).get(cat);
|
|
476 | 476 |
return c!=null ? c.getNumPatterns() : 0; |
477 | 477 |
} |
478 | 478 |
|
... | ... | |
481 | 481 |
|
482 | 482 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
483 | 483 |
|
484 |
public int getCurMove(int size, int cat, int pat)
|
|
484 |
public int getCurMove(int tab, int cat, int pat)
|
|
485 | 485 |
{ |
486 |
if( size>=0 && size<NUM_CUBES && cat>=0 && cat< numCategories[size] )
|
|
486 |
if( tab>=0 && tab<NUM_CUBES && cat>=0 && cat< numCategories[tab] )
|
|
487 | 487 |
{ |
488 |
Category c = mCategories[size].elementAt(cat);
|
|
488 |
Category c = mCategories.get(tab).get(cat);
|
|
489 | 489 |
return c!=null ? c.getPatternCurMove(pat):0; |
490 | 490 |
} |
491 | 491 |
|
... | ... | |
494 | 494 |
|
495 | 495 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
496 | 496 |
|
497 |
public int getNumMoves(int size, int cat, int pat)
|
|
497 |
public int getNumMoves(int tab, int cat, int pat)
|
|
498 | 498 |
{ |
499 |
if( size>=0 && size<NUM_CUBES && cat>=0 && cat< numCategories[size] )
|
|
499 |
if( tab>=0 && tab<NUM_CUBES && cat>=0 && cat< numCategories[tab] )
|
|
500 | 500 |
{ |
501 |
Category c = mCategories[size].elementAt(cat);
|
|
501 |
Category c = mCategories.get(tab).get(cat);
|
|
502 | 502 |
return c!=null ? c.getPatternNumMove(pat):0; |
503 | 503 |
} |
504 | 504 |
|
... | ... | |
507 | 507 |
|
508 | 508 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
509 | 509 |
|
510 |
public void makeMove(RubikPostRender post, int size, int cat, int pat)
|
|
510 |
public void makeMove(RubikPostRender post, int tab, int cat, int pat)
|
|
511 | 511 |
{ |
512 |
if( size>=0 && size<NUM_CUBES && cat>=0 && cat< numCategories[size] )
|
|
512 |
if( tab>=0 && tab<NUM_CUBES && cat>=0 && cat< numCategories[tab] )
|
|
513 | 513 |
{ |
514 |
Category c = mCategories[size].elementAt(cat);
|
|
514 |
Category c = mCategories.get(tab).get(cat);
|
|
515 | 515 |
if( c!=null ) c.makeMove(post,pat); |
516 | 516 |
} |
517 | 517 |
} |
518 | 518 |
|
519 | 519 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
520 | 520 |
|
521 |
public void backMove(RubikPostRender post, int size, int cat, int pat)
|
|
521 |
public void backMove(RubikPostRender post, int tab, int cat, int pat)
|
|
522 | 522 |
{ |
523 |
if( size>=0 && size<NUM_CUBES && cat>=0 && cat< numCategories[size] )
|
|
523 |
if( tab>=0 && tab<NUM_CUBES && cat>=0 && cat< numCategories[tab] )
|
|
524 | 524 |
{ |
525 |
Category c = mCategories[size].elementAt(cat);
|
|
525 |
Category c = mCategories.get(tab).get(cat);
|
|
526 | 526 |
if( c!=null ) c.backMove(post,pat); |
527 | 527 |
} |
528 | 528 |
} |
529 | 529 |
|
530 | 530 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
531 | 531 |
|
532 |
public int[][] reInitialize(int size, int cat, int pat)
|
|
532 |
public int[][] reInitialize(int tab, int cat, int pat)
|
|
533 | 533 |
{ |
534 |
if( size>=0 && size<NUM_CUBES && cat>=0 && cat< numCategories[size] )
|
|
534 |
if( tab>=0 && tab<NUM_CUBES && cat>=0 && cat< numCategories[tab] )
|
|
535 | 535 |
{ |
536 |
Category c = mCategories[size].elementAt(cat);
|
|
536 |
Category c = mCategories.get(tab).get(cat);
|
|
537 | 537 |
if( c!=null ) return c.reInitialize(pat); |
538 | 538 |
} |
539 | 539 |
|
Also available in: Unified diff
Speed up displaying the Patterns Dialog - do not read the whole thing, but only one Category at a time as required.