Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / helpers / ScrambleState.java @ f9a81f52

1 29b82486 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 198c5bf0 Leszek Koltunski
package org.distorted.objectlib.helpers;
21 29b82486 Leszek Koltunski
22
import java.util.Random;
23
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25
26
public class ScrambleState
27
{
28 4a5157a1 Leszek Koltunski
  private final int mTotal, mNumAxis, mNumNonZero;
29 29b82486 Leszek Koltunski
  private final int[] mNum;
30
  private final int[] mInfo;
31
  private final int[] mTmp;
32 f9a81f52 Leszek Koltunski
  private final int[][] mOrigAxis;
33 29b82486 Leszek Koltunski
  private final int LEN = 4;
34
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36
37
  public ScrambleState(int[][] axis)
38
    {
39 f9a81f52 Leszek Koltunski
    mOrigAxis = axis;
40
41 29b82486 Leszek Koltunski
    mTmp = new int[LEN];
42
43
    mNumAxis = axis.length;
44
    mNum = new int[mNumAxis];
45 4a5157a1 Leszek Koltunski
    int nonzero=0, total =0;
46 29b82486 Leszek Koltunski
47
    for(int i=0; i<mNumAxis; i++)
48
      {
49
      mNum[i] = axis[i]==null ? 0 : axis[i].length/(LEN-1);
50
      total += mNum[i];
51
      }
52
53 4a5157a1 Leszek Koltunski
    for(int i=0; i<mNumAxis; i++) if( mNum[i]>0 ) nonzero++;
54
55
    mNumNonZero = nonzero;
56 29b82486 Leszek Koltunski
    mTotal = total;
57
58
    mInfo = new int[LEN*total];
59
    int start = 0;
60
61
    for(int i=0; i<mNumAxis; i++)
62
      {
63
      for(int j=0; j<mNum[i]; j++)
64
        {
65
        mInfo[LEN*j   + start] = i;
66
        mInfo[LEN*j+1 + start] = axis[i][(LEN-1)*j  ];
67
        mInfo[LEN*j+2 + start] = axis[i][(LEN-1)*j+1];
68
        mInfo[LEN*j+3 + start] = axis[i][(LEN-1)*j+2];
69
        }
70
71
      start += LEN*mNum[i];
72
      }
73
    }
74
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76
77
  private int getIndex(int num, int indexExcluded)
78
    {
79
    int current= -1;
80
81
    for(int i=0; i<mTotal; i++)
82
      if( mInfo[LEN*i]!=indexExcluded && ++current==num ) return i;
83
84
    return -1;
85
    }
86
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
89
  public int[] getRandom(Random rnd, int indexExcluded, int[][] scrambleTable, int[] numOccurences)
90
    {
91
    int num=0, total=0, max=0, ax, layer;
92
93 4a5157a1 Leszek Koltunski
    if( mNumNonZero<2 ) indexExcluded=-1;
94
95 29b82486 Leszek Koltunski
    for(int i=0; i<mTotal; i++)
96
      {
97
      ax = mInfo[LEN*i];
98
99
      if( ax!=indexExcluded )
100
        {
101
        layer = mInfo[LEN*i+1];
102
        int value = scrambleTable[ax][layer];
103
        if( value>max ) max=value;
104
        }
105
      }
106
107
    for(int i=0; i<mTotal; i++)
108
      {
109
      ax = mInfo[LEN*i];
110
111
      if( ax!=indexExcluded )
112
        {
113
        layer = mInfo[LEN*i+1];
114
        int value = scrambleTable[ax][layer];
115
        numOccurences[total] = 1 + max - value + (total==0 ? 0 : numOccurences[total-1]);
116
        total++;
117
        }
118
      }
119
120
    float random= rnd.nextFloat()*numOccurences[total-1];
121
122
    for(int i=0; i<total; i++)
123
      {
124
      if( random <= numOccurences[i] )
125
        {
126
        num=i;
127
        break;
128
        }
129
      }
130
131
    int index = getIndex(num,indexExcluded);
132
133
    mTmp[0] = mInfo[LEN*index  ];   // axis
134
    mTmp[1] = mInfo[LEN*index+1];   // row
135
    mTmp[2] = mInfo[LEN*index+2];   // angle
136
    mTmp[3] = mInfo[LEN*index+3];   // next state
137
138
    scrambleTable[mTmp[0]][mTmp[1]]++;
139
140
    return mTmp;
141
    }
142
143 f9a81f52 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
144
145
  public int getNumAxis()
146
    {
147
    return mNumAxis;
148
    }
149
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
152
  public int[] getAx(int numAx)
153
    {
154
    return (numAx>=0 && numAx<mNumAxis) ? mOrigAxis[numAx] : null;
155
    }
156
157 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
158
159
  public int getTotal(int indexExcluded)
160
    {
161
    return ( indexExcluded>=0 && indexExcluded<mNumAxis ) ? mTotal-mNum[indexExcluded] : mTotal;
162
    }
163
}