Project

General

Profile

Download (3.8 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / helpers / ScrambleStateGraph.java @ 8db55f55

1 c0254421 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 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 eaf87d1d Leszek Koltunski
package org.distorted.helpers;
21 c0254421 Leszek Koltunski
22
///////////////////////////////////////////////////////////////////////////////////////////////////
23
24 8db55f55 Leszek Koltunski
public class ScrambleStateGraph
25 c0254421 Leszek Koltunski
{
26 8db55f55 Leszek Koltunski
  private final int mTotal, mNumAxis;
27
  private final int[] mNum;
28 c0254421 Leszek Koltunski
  private final int[] mInfo;
29
  private final int[] mTmp;
30
  private final int LEN = 4;
31
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33
34 8db55f55 Leszek Koltunski
  public ScrambleStateGraph(int[][] axis)
35 c0254421 Leszek Koltunski
    {
36
    mTmp = new int[LEN];
37
38 8db55f55 Leszek Koltunski
    mNumAxis = axis.length;
39
    mNum = new int[mNumAxis];
40
    int total =0;
41 c0254421 Leszek Koltunski
42 8db55f55 Leszek Koltunski
    for(int i=0; i<mNumAxis; i++)
43 c0254421 Leszek Koltunski
      {
44 8db55f55 Leszek Koltunski
      mNum[i] = axis[i]==null ? 0 : axis[i].length/(LEN-1);
45
      total += mNum[i];
46 c0254421 Leszek Koltunski
      }
47
48 8db55f55 Leszek Koltunski
    mTotal = total;
49 c0254421 Leszek Koltunski
50 8db55f55 Leszek Koltunski
    mInfo = new int[LEN*total];
51
    int start = 0;
52 c0254421 Leszek Koltunski
53 8db55f55 Leszek Koltunski
    for(int i=0; i<mNumAxis; i++)
54 c0254421 Leszek Koltunski
      {
55 8db55f55 Leszek Koltunski
      for(int j=0; j<mNum[i]; j++)
56
        {
57
        mInfo[LEN*j   + start] = i;
58
        mInfo[LEN*j+1 + start] = axis[i][(LEN-1)*j  ];
59
        mInfo[LEN*j+2 + start] = axis[i][(LEN-1)*j+1];
60
        mInfo[LEN*j+3 + start] = axis[i][(LEN-1)*j+2];
61
        }
62
63
      start += LEN*mNum[i];
64 c0254421 Leszek Koltunski
      }
65
    }
66
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68
69 8db55f55 Leszek Koltunski
  private int getIndex(int num, int indexExcluded)
70 c0254421 Leszek Koltunski
    {
71 8db55f55 Leszek Koltunski
    int current= -1;
72 c0254421 Leszek Koltunski
73 8db55f55 Leszek Koltunski
    for(int i=0; i<mTotal; i++)
74
      if( mInfo[LEN*i]!=indexExcluded && ++current==num ) return i;
75 c0254421 Leszek Koltunski
76
    return -1;
77
    }
78
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80
81 8db55f55 Leszek Koltunski
  public int getTotal(int indexExcluded)
82 c0254421 Leszek Koltunski
    {
83 8db55f55 Leszek Koltunski
    return ( indexExcluded>=0 && indexExcluded<mNumAxis ) ? mTotal-mNum[indexExcluded] : mTotal;
84 c0254421 Leszek Koltunski
    }
85
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87
88 8db55f55 Leszek Koltunski
  public int[] getInfo(int num, int indexExcluded)
89 c0254421 Leszek Koltunski
    {
90 8db55f55 Leszek Koltunski
    int index = getIndex(num,indexExcluded);
91 c0254421 Leszek Koltunski
92
    mTmp[0] = mInfo[LEN*index  ];   // axis
93
    mTmp[1] = mInfo[LEN*index+1];   // row
94
    mTmp[2] = mInfo[LEN*index+2];   // angle
95
    mTmp[3] = mInfo[LEN*index+3];   // next state
96
97
    return mTmp;
98
    }
99
}