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