Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / scrambling / ScrambleState.java @ df3dcf97

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