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
|
|
24
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
25
|
|
26
|
public class RubikPattern
|
27
|
{
|
28
|
public static final int MIN_CUBE = 2;
|
29
|
public static final int MAX_CUBE = 5;
|
30
|
public static final int NUM_CUBES = MAX_CUBE-MIN_CUBE+1;
|
31
|
|
32
|
private int[] numCategories = new int[NUM_CUBES];
|
33
|
private int[] currentCategory = new int[NUM_CUBES];
|
34
|
private Vector<Category>[] mCategories;
|
35
|
private static RubikPattern mThis;
|
36
|
|
37
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
38
|
|
39
|
private static class Category
|
40
|
{
|
41
|
private String name;
|
42
|
private int numPatterns;
|
43
|
private Vector<Pattern> patterns;
|
44
|
|
45
|
/////////////////////////////////////////////////////////////
|
46
|
|
47
|
Category(String n)
|
48
|
{
|
49
|
name=n;
|
50
|
numPatterns=0;
|
51
|
patterns = new Vector<>();
|
52
|
}
|
53
|
|
54
|
/////////////////////////////////////////////////////////////
|
55
|
|
56
|
void addPattern(Pattern p)
|
57
|
{
|
58
|
patterns.addElement(p);
|
59
|
numPatterns++;
|
60
|
}
|
61
|
|
62
|
/////////////////////////////////////////////////////////////
|
63
|
|
64
|
int getNumPatterns()
|
65
|
{
|
66
|
return numPatterns;
|
67
|
}
|
68
|
|
69
|
/////////////////////////////////////////////////////////////
|
70
|
|
71
|
String getName()
|
72
|
{
|
73
|
return name;
|
74
|
}
|
75
|
|
76
|
/////////////////////////////////////////////////////////////
|
77
|
|
78
|
String getPatternName(int pattern)
|
79
|
{
|
80
|
if( pattern>=0 && pattern<numPatterns )
|
81
|
{
|
82
|
Pattern p = patterns.elementAt(pattern);
|
83
|
return p!=null ? p.getName():"";
|
84
|
}
|
85
|
return "";
|
86
|
}
|
87
|
|
88
|
/////////////////////////////////////////////////////////////
|
89
|
|
90
|
int getPatternCurMove(int pattern)
|
91
|
{
|
92
|
if( pattern>=0 && pattern<numPatterns )
|
93
|
{
|
94
|
Pattern p = patterns.elementAt(pattern);
|
95
|
return p!=null ? p.getCurMove():-1;
|
96
|
}
|
97
|
return -1;
|
98
|
}
|
99
|
|
100
|
/////////////////////////////////////////////////////////////
|
101
|
|
102
|
int getPatternNumMove(int pattern)
|
103
|
{
|
104
|
if( pattern>=0 && pattern<numPatterns )
|
105
|
{
|
106
|
Pattern p = patterns.elementAt(pattern);
|
107
|
return p!=null ? p.getNumMove():-1;
|
108
|
}
|
109
|
return -1;
|
110
|
}
|
111
|
|
112
|
/////////////////////////////////////////////////////////////
|
113
|
|
114
|
String retInitializationString(int pattern)
|
115
|
{
|
116
|
if( pattern>=0 && pattern<numPatterns )
|
117
|
{
|
118
|
Pattern p = patterns.elementAt(pattern);
|
119
|
if( p!=null ) return p.retInitializationString();
|
120
|
}
|
121
|
|
122
|
return "";
|
123
|
}
|
124
|
|
125
|
/////////////////////////////////////////////////////////////
|
126
|
|
127
|
String retNextMove(int pattern)
|
128
|
{
|
129
|
if( pattern>=0 && pattern<numPatterns )
|
130
|
{
|
131
|
Pattern p = patterns.elementAt(pattern);
|
132
|
if( p!=null ) return p.retNextMove();
|
133
|
}
|
134
|
|
135
|
return "";
|
136
|
}
|
137
|
|
138
|
/////////////////////////////////////////////////////////////
|
139
|
|
140
|
String retPrevMove(int pattern)
|
141
|
{
|
142
|
if( pattern>=0 && pattern<numPatterns )
|
143
|
{
|
144
|
Pattern p = patterns.elementAt(pattern);
|
145
|
if( p!=null ) return p.retPrevMove();
|
146
|
}
|
147
|
|
148
|
return "";
|
149
|
}
|
150
|
}
|
151
|
|
152
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
153
|
|
154
|
private static class Pattern
|
155
|
{
|
156
|
private String name;
|
157
|
private String moves;
|
158
|
private int curMove;
|
159
|
private int numMove;
|
160
|
|
161
|
/////////////////////////////////////////////////////////////
|
162
|
|
163
|
Pattern(String n, String m)
|
164
|
{
|
165
|
name=n;
|
166
|
moves=m;
|
167
|
numMove = moves.length()/4;
|
168
|
curMove=numMove;
|
169
|
}
|
170
|
|
171
|
/////////////////////////////////////////////////////////////
|
172
|
|
173
|
String getName()
|
174
|
{
|
175
|
return name;
|
176
|
}
|
177
|
|
178
|
/////////////////////////////////////////////////////////////
|
179
|
|
180
|
int getNumMove()
|
181
|
{
|
182
|
return numMove;
|
183
|
}
|
184
|
|
185
|
/////////////////////////////////////////////////////////////
|
186
|
|
187
|
int getCurMove()
|
188
|
{
|
189
|
return curMove;
|
190
|
}
|
191
|
|
192
|
/////////////////////////////////////////////////////////////
|
193
|
|
194
|
String retNextMove()
|
195
|
{
|
196
|
curMove++;
|
197
|
|
198
|
if( curMove>numMove)
|
199
|
{
|
200
|
curMove= 0;
|
201
|
return retInitializationString();
|
202
|
}
|
203
|
else
|
204
|
{
|
205
|
curMove--;
|
206
|
return moves.substring(4*curMove-4,4*curMove);
|
207
|
}
|
208
|
}
|
209
|
|
210
|
/////////////////////////////////////////////////////////////
|
211
|
|
212
|
String retPrevMove()
|
213
|
{
|
214
|
curMove--;
|
215
|
|
216
|
if( curMove<0)
|
217
|
{
|
218
|
curMove=numMove;
|
219
|
return retInitializationString();
|
220
|
}
|
221
|
else
|
222
|
{
|
223
|
curMove++;
|
224
|
return moves.substring(4*curMove,4*curMove+4);
|
225
|
}
|
226
|
}
|
227
|
|
228
|
/////////////////////////////////////////////////////////////
|
229
|
|
230
|
String retInitializationString()
|
231
|
{
|
232
|
return moves.substring(0,4*curMove);
|
233
|
}
|
234
|
}
|
235
|
|
236
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
237
|
|
238
|
private RubikPattern()
|
239
|
{
|
240
|
mCategories = new Vector[NUM_CUBES];
|
241
|
|
242
|
initializeCategories(0, RubikPatternData2.patterns);
|
243
|
initializeCategories(1, RubikPatternData3.patterns);
|
244
|
initializeCategories(2, RubikPatternData4.patterns);
|
245
|
initializeCategories(3, RubikPatternData5.patterns);
|
246
|
}
|
247
|
|
248
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
249
|
|
250
|
private void initializeCategories(int num, String[] pat)
|
251
|
{
|
252
|
int colon;
|
253
|
mCategories[num] = new Vector<>();
|
254
|
Category cat=null;
|
255
|
String name, pattern;
|
256
|
Pattern patt;
|
257
|
|
258
|
numCategories[num]=0;
|
259
|
|
260
|
for(String p: pat)
|
261
|
{
|
262
|
colon = p.indexOf(':');
|
263
|
|
264
|
if( colon==-1 )
|
265
|
{
|
266
|
cat = new Category(p);
|
267
|
mCategories[num].addElement(cat);
|
268
|
numCategories[num]++;
|
269
|
}
|
270
|
else
|
271
|
{
|
272
|
pattern = p.substring(colon+1);
|
273
|
name = p.substring(0,colon);
|
274
|
patt = new Pattern(name,pattern);
|
275
|
cat.addPattern(patt);
|
276
|
}
|
277
|
}
|
278
|
}
|
279
|
|
280
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
281
|
// PUBLIC API
|
282
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
283
|
|
284
|
public static RubikPattern getInstance()
|
285
|
{
|
286
|
if( mThis==null )
|
287
|
{
|
288
|
mThis = new RubikPattern();
|
289
|
}
|
290
|
|
291
|
return mThis;
|
292
|
}
|
293
|
|
294
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
295
|
|
296
|
public int getNumCategories(int size)
|
297
|
{
|
298
|
return size>=0 && size<NUM_CUBES ? numCategories[size] : 0;
|
299
|
}
|
300
|
|
301
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
302
|
|
303
|
public String getCategoryName(int size,int num)
|
304
|
{
|
305
|
if( size>=0 && size<NUM_CUBES && num>=0 && num< numCategories[size] )
|
306
|
{
|
307
|
Category c = mCategories[size].elementAt(num);
|
308
|
return c!=null ? c.getName() : null;
|
309
|
}
|
310
|
|
311
|
return null;
|
312
|
}
|
313
|
|
314
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
315
|
|
316
|
public void rememberCategory(int size,int num)
|
317
|
{
|
318
|
if( size>=0 && size<NUM_CUBES )
|
319
|
{
|
320
|
currentCategory[size] = num;
|
321
|
}
|
322
|
}
|
323
|
|
324
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
325
|
|
326
|
public int recallCategory(int size)
|
327
|
{
|
328
|
return size>=0 && size<NUM_CUBES ? currentCategory[size] : 0;
|
329
|
}
|
330
|
|
331
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
332
|
|
333
|
public String getPatternName(int size, int cat, int pat)
|
334
|
{
|
335
|
if( size>=0 && size<NUM_CUBES && cat>=0 && cat< numCategories[size] )
|
336
|
{
|
337
|
Category c = mCategories[size].elementAt(cat);
|
338
|
return c!=null ? c.getPatternName(pat) : null;
|
339
|
}
|
340
|
|
341
|
return null;
|
342
|
}
|
343
|
|
344
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
345
|
|
346
|
public int getNumPatterns(int size, int num )
|
347
|
{
|
348
|
if( size>=0 && size<NUM_CUBES && num>=0 && num< numCategories[size] )
|
349
|
{
|
350
|
Category c = mCategories[size].elementAt(num);
|
351
|
return c!=null ? c.getNumPatterns() : 0;
|
352
|
}
|
353
|
|
354
|
return 0;
|
355
|
}
|
356
|
|
357
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
358
|
|
359
|
public int getCurMove(int size, int cat, int pat)
|
360
|
{
|
361
|
if( size>=0 && size<NUM_CUBES && cat>=0 && cat< numCategories[size] )
|
362
|
{
|
363
|
Category c = mCategories[size].elementAt(cat);
|
364
|
return c!=null ? c.getPatternCurMove(pat):0;
|
365
|
}
|
366
|
|
367
|
return 0;
|
368
|
}
|
369
|
|
370
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
371
|
|
372
|
public int getNumMoves(int size, int cat, int pat)
|
373
|
{
|
374
|
if( size>=0 && size<NUM_CUBES && cat>=0 && cat< numCategories[size] )
|
375
|
{
|
376
|
Category c = mCategories[size].elementAt(cat);
|
377
|
return c!=null ? c.getPatternNumMove(pat):0;
|
378
|
}
|
379
|
|
380
|
return 0;
|
381
|
}
|
382
|
|
383
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
384
|
|
385
|
public String retNextMove(int size, int cat, int pat)
|
386
|
{
|
387
|
if( size>=0 && size<NUM_CUBES && cat>=0 && cat< numCategories[size] )
|
388
|
{
|
389
|
Category c = mCategories[size].elementAt(cat);
|
390
|
if( c!=null ) return c.retNextMove(pat);
|
391
|
}
|
392
|
|
393
|
return "";
|
394
|
}
|
395
|
|
396
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
397
|
|
398
|
public String retPrevMove(int size, int cat, int pat)
|
399
|
{
|
400
|
if( size>=0 && size<NUM_CUBES && cat>=0 && cat< numCategories[size] )
|
401
|
{
|
402
|
Category c = mCategories[size].elementAt(cat);
|
403
|
if( c!=null ) return c.retPrevMove(pat);
|
404
|
}
|
405
|
|
406
|
return "";
|
407
|
}
|
408
|
|
409
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
410
|
|
411
|
public String retInitializationString(int size,int cat, int pat)
|
412
|
{
|
413
|
if( size>=0 && size<NUM_CUBES && cat>=0 && cat< numCategories[size] )
|
414
|
{
|
415
|
Category c = mCategories[size].elementAt(cat);
|
416
|
if( c!=null ) return c.retInitializationString(pat);
|
417
|
}
|
418
|
|
419
|
return "";
|
420
|
}
|
421
|
}
|