Project

General

Profile

Download (11.9 KB) Statistics
| Branch: | Revision:

distorted-objectlib / src / main / java / org / distorted / objectlib / tablebases / TablebasesPyraminx.java @ 20df890a

1 b8519939 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 ccd8a6f2 Leszek Koltunski
import static org.distorted.objectlib.main.TwistyObject.SQ3;
14 b8519939 Leszek Koltunski
import static org.distorted.objectlib.main.TwistyObject.SQ6;
15
16
import android.content.res.Resources;
17
18
import org.distorted.library.type.Static3D;
19 74cc695a Leszek Koltunski
import org.distorted.objectlib.R;
20 b8519939 Leszek Koltunski
21
///////////////////////////////////////////////////////////////////////////////////////////////////
22
23
public class TablebasesPyraminx extends TablebasesAbstract
24
{
25 d8003f3a Leszek Koltunski
  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 f32c546d Leszek Koltunski
          {11, 9}
33 d8003f3a Leszek Koltunski
      };
34
35 b8519939 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
36
37
  public TablebasesPyraminx()
38
    {
39
    super();
40
    }
41
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43
44
  public TablebasesPyraminx(Resources res)
45
    {
46 74cc695a Leszek Koltunski
    super(res, R.raw.pyra_3_tablebase);
47 b8519939 Leszek Koltunski
    }
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 ccd8a6f2 Leszek Koltunski
    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 b8519939 Leszek Koltunski
    }
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 74cc695a Leszek Koltunski
             { 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 b8519939 Leszek Koltunski
             { 0.0f,-SQ2/2, 0.0f},
85 bf890718 Leszek Koltunski
             { 0.0f,-SQ2/2, 0.0f},
86
             { 0.0f,-SQ2/2, 0.0f},
87 b8519939 Leszek Koltunski
         };
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 6d637f71 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
109
110
  private void addMove(int[] move, int axis, int twist)
111
    {
112
    move[0] = axis;
113 20df890a Leszek Koltunski
    move[1] = 4;
114 6d637f71 Leszek Koltunski
    move[2] = (twist==2 ? -1:twist);
115
    }
116
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118
119 20df890a Leszek Koltunski
  private boolean failedInsertingTipMove(int[][] moves, int[] extra, int extraIndex, int axis)
120 6d637f71 Leszek Koltunski
    {
121 20df890a Leszek Koltunski
    int numAxis = 0;
122
    boolean ret = true;
123 6d637f71 Leszek Koltunski
124 20df890a Leszek Koltunski
    for( int[] move : moves )
125
      {
126
      if( move[0]==axis )
127
        {
128
        numAxis++;
129
130
android.util.Log.e("D", "sameAxis, numAxis="+numAxis+" "+move[2]+" "+extra[extraIndex]);
131
132
        if( (numAxis==1 && move[2]==extra[extraIndex]) || numAxis>=2 ) ret=false;
133
        }
134
      }
135
136
android.util.Log.e("D", "failedInsertingTipMove "+axis+" : "+ret);
137
138
    int currAx = 0;
139
    int numMoves = moves.length;
140
    int foreIndex=-1;
141
    int lastIndex = -1;
142 6d637f71 Leszek Koltunski
143 20df890a Leszek Koltunski
    for( int index=0; index<numMoves; index++ )
144 6d637f71 Leszek Koltunski
      {
145 20df890a Leszek Koltunski
      int[] move = moves[index];
146 6d637f71 Leszek Koltunski
147 20df890a Leszek Koltunski
      if( move[0]==axis )
148
        {
149
        if (currAx < numAxis-2 )
150
          {
151
          move[1] = 6;
152
          extra[extraIndex] += (move[2]==1 ? 1:2);
153
          }
154
        if( currAx == numAxis-2 ) foreIndex = index;
155
        if( currAx == numAxis-1 ) lastIndex = index;
156
157
        currAx++;
158
        }
159
      }
160
161
    extra[extraIndex] = (extra[extraIndex]%3);
162
163
    if( foreIndex>=0 )
164
      {
165
      int foreAngle = moves[foreIndex][2];
166
      int lastAngle = moves[lastIndex][2];
167 6d637f71 Leszek Koltunski
168 20df890a Leszek Koltunski
      if( extra[extraIndex]==1 )
169
        {
170
        if( foreAngle==1 )
171
          {
172
          moves[foreIndex][1] = 6;
173
          extra[extraIndex]++;
174
          }
175
        else
176
          {
177
          if( lastAngle==1 )
178
            {
179
            moves[lastIndex][1] = 6;
180
            extra[extraIndex]++;
181
            }
182
          else
183
            {
184
            moves[foreIndex][1] = 6;
185
            moves[lastIndex][1] = 6;
186
            extra[extraIndex]+=2;
187
            }
188
          }
189
        }
190
      else if( extra[extraIndex]==2 )
191
        {
192
        if( foreAngle==-1 )
193
          {
194
          moves[foreIndex][1] = 6;
195
          extra[extraIndex]+=2;
196
          }
197
        else
198
          {
199
          if( lastAngle==-1 )
200
            {
201
            moves[lastIndex][1] = 6;
202
            extra[extraIndex]+=2;
203
            }
204
          else
205
            {
206
            moves[foreIndex][1] = 6;
207
            moves[lastIndex][1] = 6;
208
            extra[extraIndex]++;
209
            }
210
          }
211
        }
212 6d637f71 Leszek Koltunski
      }
213 20df890a Leszek Koltunski
    else if( lastIndex>=0 )
214
      {
215
      int lastAngle = moves[lastIndex][2];
216 6d637f71 Leszek Koltunski
217 20df890a Leszek Koltunski
      if( extra[extraIndex]==1 )
218
        {
219
        if( lastAngle==1 )
220
          {
221
          moves[lastIndex][1] = 6;
222
          extra[extraIndex]++;
223
          }
224
        }
225
      else if( extra[extraIndex]==2 )
226
        {
227
        if( lastAngle==-1 )
228
          {
229
          moves[lastIndex][1] = 6;
230
          extra[extraIndex]+=2;
231
          }
232
        }
233
      }
234
235
    extra[extraIndex] = (extra[extraIndex]%3);
236
237
    return ret;
238 6d637f71 Leszek Koltunski
    }
239
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241
// extra = cornerTips
242
243
  @Override
244
  int[][] extraInfo(int[][] moves, int[] extra)
245
    {
246 20df890a Leszek Koltunski
    int lenExtra = 0;
247 6d637f71 Leszek Koltunski
248 20df890a Leszek Koltunski
    if( failedInsertingTipMove(moves,extra,0,1) && extra[0]!=0 ) lenExtra++;
249
    if( failedInsertingTipMove(moves,extra,1,0) && extra[1]!=0 ) lenExtra++;
250
    if( failedInsertingTipMove(moves,extra,2,3) && extra[2]!=0 ) lenExtra++;
251
    if( failedInsertingTipMove(moves,extra,3,2) && extra[3]!=0 ) lenExtra++;
252 6d637f71 Leszek Koltunski
253 20df890a Leszek Koltunski
android.util.Log.e("D", "lenExtra="+lenExtra);
254 6d637f71 Leszek Koltunski
255 20df890a Leszek Koltunski
    if( lenExtra>0 )
256
      {
257
      int[][] extraMoves = new int[lenExtra][3];
258 6d637f71 Leszek Koltunski
259 20df890a Leszek Koltunski
      int index=0;
260
      if( extra[0]!=0 ) { addMove(extraMoves[index],1,extra[0]); index++; }
261
      if( extra[1]!=0 ) { addMove(extraMoves[index],0,extra[1]); index++; }
262
      if( extra[2]!=0 ) { addMove(extraMoves[index],3,extra[2]); index++; }
263
      if( extra[3]!=0 ) { addMove(extraMoves[index],2,extra[3]);          }
264
265
      int len = moves.length;
266
      int totalLen = len+lenExtra;
267
      int[][] totalMoves = new int[totalLen][];
268
269
      if( len>0 ) System.arraycopy(moves, 0, totalMoves, 0, len);
270
      System.arraycopy(extraMoves, 0, totalMoves, len, lenExtra);
271
272
for(int i=0; i<totalLen; i++)
273
  android.util.Log.e("D", totalMoves[i][0]+" "+totalMoves[i][1]+" "+totalMoves[i][2]);
274
275
      return totalMoves;
276
      }
277
278
    return moves;
279 6d637f71 Leszek Koltunski
    }
280
281 b8519939 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
282
283
  int getSize()
284
    {
285
    return 933120;  // see https://www.jaapsch.net/puzzles/pyraminx.htm
286
    }
287
288 c0266cb1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
289
290 6d637f71 Leszek Koltunski
  int getMinScramble()
291 c0266cb1 Leszek Koltunski
    {
292
    return 9;
293
    }
294
295 b9f8919d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
296
297 d8003f3a Leszek Koltunski
  public static void getEdgePermutation(int[] output, int[] quats, int index)
298
    {
299
    for(int i=0; i<6; i++ )
300
      {
301
      int quat = quats[i+index];
302
303
      for(int j=0; j<6; j++)
304
        {
305
        int[] edge = EDGE_QUATS[j];
306
        if( quat==edge[0] || quat==edge[1] ) { output[i]=j; break; }
307
        }
308
      }
309
    }
310
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312
313
  public static void getEdgeTwist(int[] output, int[] quats, int index)
314 b9f8919d Leszek Koltunski
    {
315 d8003f3a Leszek Koltunski
    for(int i=0; i<6; i++)
316
      {
317
      int quat = quats[i+index];
318
319
      for(int j=0; j<6; j++)
320
        {
321
        int[] edge = EDGE_QUATS[j];
322
        if( quat==edge[0] ) { output[i]=0; break; }
323
        if( quat==edge[1] ) { output[i]=1; break; }
324
        }
325
      }
326
    }
327
328
///////////////////////////////////////////////////////////////////////////////////////////////////
329 b9f8919d Leszek Koltunski
330 d8003f3a Leszek Koltunski
  private void getEdgeQuats(int twist, int[] perm, int[] output)
331
    {
332 f32c546d Leszek Koltunski
    int totalTwist = 0;
333
334
    for(int i=0; i<5; i++)
335 d8003f3a Leszek Koltunski
      {
336
      int p = perm[i];
337 f32c546d Leszek Koltunski
      int t = (twist%2);
338 d8003f3a Leszek Koltunski
      twist/=2;
339 bf890718 Leszek Koltunski
      output[i+4] = EDGE_QUATS[p][t];
340 f32c546d Leszek Koltunski
341
      totalTwist += t;
342 d8003f3a Leszek Koltunski
      }
343 f32c546d Leszek Koltunski
344
    int p = perm[5];
345
    int t = (totalTwist%2)==0 ? 0:1;
346
    output[9] = EDGE_QUATS[p][t];
347 d8003f3a Leszek Koltunski
    }
348
349
///////////////////////////////////////////////////////////////////////////////////////////////////
350
351
  private int getVertexTwist(int[] quats)
352
    {
353
    int twist = 0;
354
355
    if( quats[3]==5 ) twist += 1;
356
    if( quats[3]==6 ) twist += 2;
357
    twist *= 3;
358
    if( quats[2]==7 ) twist += 1;
359
    if( quats[2]==8 ) twist += 2;
360
    twist *= 3;
361
    if( quats[1]==1 ) twist += 1;
362
    if( quats[1]==2 ) twist += 2;
363
    twist *= 3;
364
    if( quats[0]==3 ) twist += 1;
365
    if( quats[0]==4 ) twist += 2;
366
367
    return twist;
368
    }
369
370
///////////////////////////////////////////////////////////////////////////////////////////////////
371
372
  private void getVertexQuats(int twist, int[] output)
373
    {
374
    switch(twist%3)
375
      {
376
      case 0: output[0]=0; break;
377
      case 1: output[0]=3; break;
378
      case 2: output[0]=4; break;
379
      }
380
    twist /= 3;
381
382
    switch(twist%3)
383
      {
384
      case 0: output[1]=0; break;
385
      case 1: output[1]=1; break;
386
      case 2: output[1]=2; break;
387
      }
388
    twist /= 3;
389
390
    switch(twist%3)
391
      {
392
      case 0: output[2]=0; break;
393
      case 1: output[2]=7; break;
394
      case 2: output[2]=8; break;
395
      }
396
    twist /= 3;
397
398
    switch(twist%3)
399
      {
400
      case 0: output[3]=0; break;
401
      case 1: output[3]=5; break;
402
      case 2: output[3]=6; break;
403
      }
404 b9f8919d Leszek Koltunski
    }
405
406 b8519939 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
407
408 f32c546d Leszek Koltunski
  public int[] getQuats(int index)
409 b8519939 Leszek Koltunski
    {
410 d8003f3a Leszek Koltunski
    int vertexTwist = (index%81);
411
    index /= 81;
412
    int edgeTwist = (index%32);
413
    int perm_num = index/32;
414
415
    int[] quats = new int[10];
416
417
    getVertexQuats(vertexTwist,quats);
418
    int[] permutation = new int[6];
419 522d858e Leszek Koltunski
    TablebaseHelpers.getEvenPermutationFromNum(permutation,perm_num);
420 d8003f3a Leszek Koltunski
    getEdgeQuats(edgeTwist,permutation,quats);
421
422
    return quats;
423 b8519939 Leszek Koltunski
    }
424
425
///////////////////////////////////////////////////////////////////////////////////////////////////
426
427 f32c546d Leszek Koltunski
  public int getIndex(int[] quats)
428 b8519939 Leszek Koltunski
    {
429 d8003f3a Leszek Koltunski
    int vertexTwist = getVertexTwist(quats);
430
    int[] permutation = new int[6];
431
    getEdgePermutation(permutation,quats,4);
432
    int[] twist = new int[6];
433
    getEdgeTwist(twist,quats,4);
434
435
    int edgeTwist = twist[0]+ 2*(twist[1]+ 2*(twist[2]+ 2*(twist[3]+ 2*twist[4])));
436
    int perm_num = TablebaseHelpers.computeEvenPermutationNum(permutation);
437
438
    return vertexTwist + 81*(edgeTwist + 32*perm_num);
439 b8519939 Leszek Koltunski
    }
440
}