1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2023 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Magic Cube. //
|
5
|
// //
|
6
|
// Magic Cube is proprietary software licensed under an EULA which you should have received //
|
7
|
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html //
|
8
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
9
|
|
10
|
package org.distorted.objectlib.tablebases;
|
11
|
|
12
|
import static org.distorted.objectlib.main.TwistyObject.SQ2;
|
13
|
import static org.distorted.objectlib.main.TwistyObject.SQ3;
|
14
|
import static org.distorted.objectlib.main.TwistyObject.SQ6;
|
15
|
|
16
|
import android.content.res.Resources;
|
17
|
|
18
|
import org.distorted.library.type.Static3D;
|
19
|
import org.distorted.objectlib.R;
|
20
|
|
21
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
22
|
|
23
|
public class TablebasesPyraminx extends TablebasesAbstract
|
24
|
{
|
25
|
public static final int[][] EDGE_QUATS = new int[][]
|
26
|
{
|
27
|
{ 0,10}, // even-odd quat (i.e. even-odd twist of the relevant edge piece)
|
28
|
{ 1, 7},
|
29
|
{ 4, 8},
|
30
|
{ 2, 6},
|
31
|
{ 3, 5},
|
32
|
{11, 9}
|
33
|
};
|
34
|
|
35
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
36
|
|
37
|
public TablebasesPyraminx()
|
38
|
{
|
39
|
super();
|
40
|
}
|
41
|
|
42
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
43
|
|
44
|
public TablebasesPyraminx(Resources res)
|
45
|
{
|
46
|
super(res, R.raw.pyra_3_tablebase);
|
47
|
}
|
48
|
|
49
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
50
|
|
51
|
int[][] getBasicAngles()
|
52
|
{
|
53
|
int[] tmp = {3,3};
|
54
|
return new int[][] { tmp,tmp,tmp,tmp };
|
55
|
}
|
56
|
|
57
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
58
|
|
59
|
Static3D[] getRotationAxis()
|
60
|
{
|
61
|
return new Static3D[]
|
62
|
{
|
63
|
new Static3D( 0,-SQ3/3,-SQ6/3),
|
64
|
new Static3D( 0,-SQ3/3, SQ6/3),
|
65
|
new Static3D( SQ6/3, SQ3/3, 0),
|
66
|
new Static3D(-SQ6/3, SQ3/3, 0),
|
67
|
};
|
68
|
}
|
69
|
|
70
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
71
|
|
72
|
float[][] getPosition()
|
73
|
{
|
74
|
return new float[][]
|
75
|
{
|
76
|
{ 0.0f,-SQ2/4, 0.5f},
|
77
|
{ 0.0f,-SQ2/4,-0.5f},
|
78
|
{-0.5f, SQ2/4, 0.0f},
|
79
|
{ 0.5f, SQ2/4, 0.0f},
|
80
|
|
81
|
{ 0.0f,-SQ2/2, 0.0f}, // index=0 has the quats already set up so that
|
82
|
{ 0.0f,-SQ2/2, 0.0f}, // all the edges fall into right spots, so here
|
83
|
{ 0.0f,-SQ2/2, 0.0f}, // they need to be all in one place
|
84
|
{ 0.0f,-SQ2/2, 0.0f},
|
85
|
{ 0.0f,-SQ2/2, 0.0f},
|
86
|
{ 0.0f,-SQ2/2, 0.0f},
|
87
|
};
|
88
|
}
|
89
|
|
90
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
91
|
|
92
|
float[][] getCuts()
|
93
|
{
|
94
|
float[] cut = { 0.25f*(SQ6/3) };
|
95
|
return new float[][] { cut,cut,cut,cut };
|
96
|
}
|
97
|
|
98
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
99
|
|
100
|
boolean[][] getRotatable()
|
101
|
{
|
102
|
boolean[] tmp = new boolean[] {false,true};
|
103
|
return new boolean[][] { tmp,tmp,tmp,tmp };
|
104
|
}
|
105
|
|
106
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
107
|
// specifically for the tablebase
|
108
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
109
|
|
110
|
private void addMove(int[] move, int axis, int twist)
|
111
|
{
|
112
|
move[0] = axis;
|
113
|
move[1] = 4;
|
114
|
move[2] = (twist==2 ? -1:twist);
|
115
|
}
|
116
|
|
117
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
118
|
|
119
|
private void includeTip(int[] move, int[] extra, int index)
|
120
|
{
|
121
|
move[1] = 6;
|
122
|
extra[index] += (move[2]==1 ? 2:1);
|
123
|
}
|
124
|
|
125
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
126
|
|
127
|
private boolean failedInsertingTipMove(int[][] moves, int[] extra, int extraIndex, int axis)
|
128
|
{
|
129
|
int numAxis = 0;
|
130
|
boolean ret = true;
|
131
|
extra[extraIndex]+=3; // make sure extra is positive
|
132
|
|
133
|
for( int[] move : moves )
|
134
|
{
|
135
|
if( move[0]==axis )
|
136
|
{
|
137
|
numAxis++;
|
138
|
if( (numAxis==1 && (move[2]-extra[extraIndex])%3==0 ) || numAxis>=2 ) ret=false;
|
139
|
}
|
140
|
}
|
141
|
|
142
|
int currAx = 0;
|
143
|
int numMoves = moves.length;
|
144
|
int foreIndex=-1;
|
145
|
int lastIndex = -1;
|
146
|
|
147
|
for( int index=0; index<numMoves; index++ )
|
148
|
{
|
149
|
int[] move = moves[index];
|
150
|
|
151
|
if( move[0]==axis )
|
152
|
{
|
153
|
if( currAx < numAxis-2 ) includeTip(move,extra,extraIndex);
|
154
|
else if( currAx == numAxis-2 ) foreIndex = index;
|
155
|
else if( currAx == numAxis-1 ) lastIndex = index;
|
156
|
currAx++;
|
157
|
}
|
158
|
}
|
159
|
|
160
|
extra[extraIndex] = (extra[extraIndex]%3);
|
161
|
|
162
|
if( foreIndex>=0 )
|
163
|
{
|
164
|
int foreAngle = moves[foreIndex][2];
|
165
|
int lastAngle = moves[lastIndex][2];
|
166
|
|
167
|
if( extra[extraIndex]==1 )
|
168
|
{
|
169
|
if( foreAngle==1 ) includeTip(moves[foreIndex],extra,extraIndex);
|
170
|
else
|
171
|
{
|
172
|
includeTip(moves[lastIndex],extra,extraIndex);
|
173
|
if( lastAngle==-1 ) includeTip(moves[foreIndex],extra,extraIndex);
|
174
|
}
|
175
|
}
|
176
|
else if( extra[extraIndex]==2 )
|
177
|
{
|
178
|
if( foreAngle==-1 ) includeTip(moves[foreIndex],extra,extraIndex);
|
179
|
else
|
180
|
{
|
181
|
includeTip(moves[lastIndex],extra,extraIndex);
|
182
|
if( lastAngle==1 ) includeTip(moves[foreIndex],extra,extraIndex);
|
183
|
}
|
184
|
}
|
185
|
}
|
186
|
else if( lastIndex>=0 )
|
187
|
{
|
188
|
int lastAngle = moves[lastIndex][2];
|
189
|
|
190
|
if( (extra[extraIndex]-lastAngle)%3 ==0 )
|
191
|
includeTip(moves[lastIndex],extra,extraIndex);
|
192
|
}
|
193
|
|
194
|
extra[extraIndex] = (extra[extraIndex]%3);
|
195
|
|
196
|
return ret;
|
197
|
}
|
198
|
|
199
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
200
|
// extra = cornerTips
|
201
|
|
202
|
@Override
|
203
|
int[][] extraInfo(int[][] moves, int[] extra)
|
204
|
{
|
205
|
int lenExtra = 0;
|
206
|
|
207
|
if( failedInsertingTipMove(moves,extra,0,1) && extra[0]!=0 ) lenExtra++;
|
208
|
if( failedInsertingTipMove(moves,extra,1,0) && extra[1]!=0 ) lenExtra++;
|
209
|
if( failedInsertingTipMove(moves,extra,2,3) && extra[2]!=0 ) lenExtra++;
|
210
|
if( failedInsertingTipMove(moves,extra,3,2) && extra[3]!=0 ) lenExtra++;
|
211
|
|
212
|
if( lenExtra>0 )
|
213
|
{
|
214
|
int[][] extraMoves = new int[lenExtra][3];
|
215
|
|
216
|
int index=0;
|
217
|
if( extra[0]!=0 ) { addMove(extraMoves[index],1,extra[0]); index++; }
|
218
|
if( extra[1]!=0 ) { addMove(extraMoves[index],0,extra[1]); index++; }
|
219
|
if( extra[2]!=0 ) { addMove(extraMoves[index],3,extra[2]); index++; }
|
220
|
if( extra[3]!=0 ) { addMove(extraMoves[index],2,extra[3]); }
|
221
|
|
222
|
int len = moves.length;
|
223
|
int totalLen = len+lenExtra;
|
224
|
int[][] totalMoves = new int[totalLen][];
|
225
|
|
226
|
if( len>0 ) System.arraycopy(moves, 0, totalMoves, 0, len);
|
227
|
System.arraycopy(extraMoves, 0, totalMoves, len, lenExtra);
|
228
|
|
229
|
return totalMoves;
|
230
|
}
|
231
|
|
232
|
return moves;
|
233
|
}
|
234
|
|
235
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
236
|
|
237
|
int getSize()
|
238
|
{
|
239
|
return 933120; // see https://www.jaapsch.net/puzzles/pyraminx.htm
|
240
|
}
|
241
|
|
242
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
243
|
|
244
|
int getMinScramble()
|
245
|
{
|
246
|
return 9;
|
247
|
}
|
248
|
|
249
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
250
|
|
251
|
public static void getEdgePermutation(int[] output, int[] quats, int index)
|
252
|
{
|
253
|
for(int i=0; i<6; i++ )
|
254
|
{
|
255
|
int quat = quats[i+index];
|
256
|
|
257
|
for(int j=0; j<6; j++)
|
258
|
{
|
259
|
int[] edge = EDGE_QUATS[j];
|
260
|
if( quat==edge[0] || quat==edge[1] ) { output[i]=j; break; }
|
261
|
}
|
262
|
}
|
263
|
}
|
264
|
|
265
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
266
|
|
267
|
public static void getEdgeTwist(int[] output, int[] quats, int index)
|
268
|
{
|
269
|
for(int i=0; i<6; i++)
|
270
|
{
|
271
|
int quat = quats[i+index];
|
272
|
|
273
|
for(int j=0; j<6; j++)
|
274
|
{
|
275
|
int[] edge = EDGE_QUATS[j];
|
276
|
if( quat==edge[0] ) { output[i]=0; break; }
|
277
|
if( quat==edge[1] ) { output[i]=1; break; }
|
278
|
}
|
279
|
}
|
280
|
}
|
281
|
|
282
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
283
|
|
284
|
private void getEdgeQuats(int twist, int[] perm, int[] output)
|
285
|
{
|
286
|
int totalTwist = 0;
|
287
|
|
288
|
for(int i=0; i<5; i++)
|
289
|
{
|
290
|
int p = perm[i];
|
291
|
int t = (twist%2);
|
292
|
twist/=2;
|
293
|
output[i+4] = EDGE_QUATS[p][t];
|
294
|
|
295
|
totalTwist += t;
|
296
|
}
|
297
|
|
298
|
int p = perm[5];
|
299
|
int t = (totalTwist%2)==0 ? 0:1;
|
300
|
output[9] = EDGE_QUATS[p][t];
|
301
|
}
|
302
|
|
303
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
304
|
|
305
|
private int getVertexTwist(int[] quats)
|
306
|
{
|
307
|
int twist = 0;
|
308
|
|
309
|
if( quats[3]==5 ) twist += 1;
|
310
|
if( quats[3]==6 ) twist += 2;
|
311
|
twist *= 3;
|
312
|
if( quats[2]==7 ) twist += 1;
|
313
|
if( quats[2]==8 ) twist += 2;
|
314
|
twist *= 3;
|
315
|
if( quats[1]==1 ) twist += 1;
|
316
|
if( quats[1]==2 ) twist += 2;
|
317
|
twist *= 3;
|
318
|
if( quats[0]==3 ) twist += 1;
|
319
|
if( quats[0]==4 ) twist += 2;
|
320
|
|
321
|
return twist;
|
322
|
}
|
323
|
|
324
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
325
|
|
326
|
private void getVertexQuats(int twist, int[] output)
|
327
|
{
|
328
|
switch(twist%3)
|
329
|
{
|
330
|
case 0: output[0]=0; break;
|
331
|
case 1: output[0]=3; break;
|
332
|
case 2: output[0]=4; break;
|
333
|
}
|
334
|
twist /= 3;
|
335
|
|
336
|
switch(twist%3)
|
337
|
{
|
338
|
case 0: output[1]=0; break;
|
339
|
case 1: output[1]=1; break;
|
340
|
case 2: output[1]=2; break;
|
341
|
}
|
342
|
twist /= 3;
|
343
|
|
344
|
switch(twist%3)
|
345
|
{
|
346
|
case 0: output[2]=0; break;
|
347
|
case 1: output[2]=7; break;
|
348
|
case 2: output[2]=8; break;
|
349
|
}
|
350
|
twist /= 3;
|
351
|
|
352
|
switch(twist%3)
|
353
|
{
|
354
|
case 0: output[3]=0; break;
|
355
|
case 1: output[3]=5; break;
|
356
|
case 2: output[3]=6; break;
|
357
|
}
|
358
|
}
|
359
|
|
360
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
361
|
|
362
|
public int[] getQuats(int index)
|
363
|
{
|
364
|
int vertexTwist = (index%81);
|
365
|
index /= 81;
|
366
|
int edgeTwist = (index%32);
|
367
|
int perm_num = index/32;
|
368
|
|
369
|
int[] quats = new int[10];
|
370
|
|
371
|
getVertexQuats(vertexTwist,quats);
|
372
|
int[] permutation = new int[6];
|
373
|
TablebaseHelpers.getEvenPermutationFromNum(permutation,perm_num);
|
374
|
getEdgeQuats(edgeTwist,permutation,quats);
|
375
|
|
376
|
return quats;
|
377
|
}
|
378
|
|
379
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
380
|
|
381
|
public int getIndex(int[] quats)
|
382
|
{
|
383
|
int vertexTwist = getVertexTwist(quats);
|
384
|
int[] permutation = new int[6];
|
385
|
getEdgePermutation(permutation,quats,4);
|
386
|
int[] twist = new int[6];
|
387
|
getEdgeTwist(twist,quats,4);
|
388
|
|
389
|
int edgeTwist = twist[0]+ 2*(twist[1]+ 2*(twist[2]+ 2*(twist[3]+ 2*twist[4])));
|
390
|
int perm_num = TablebaseHelpers.computeEvenPermutationNum(permutation);
|
391
|
|
392
|
return vertexTwist + 81*(edgeTwist + 32*perm_num);
|
393
|
}
|
394
|
}
|
395
|
|