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