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