Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 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.scrambling;
11

    
12
import java.util.Random;
13

    
14
///////////////////////////////////////////////////////////////////////////////////////////////////
15

    
16
public class ScrambleState
17
{
18
  private final int mTotal, mNumAxis, mNumNonZero;
19
  private final int[] mNum;
20
  private final int[] mInfo;
21
  private final int[] mTmp;
22
  private final int[][] mOrigAxis;
23
  private final int LEN = 4;
24

    
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26

    
27
  public ScrambleState(int[][] axis)
28
    {
29
    mOrigAxis = axis;
30

    
31
    mTmp = new int[LEN];
32

    
33
    mNumAxis = axis.length;
34
    mNum = new int[mNumAxis];
35
    int nonzero=0, total =0;
36

    
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
    for(int i=0; i<mNumAxis; i++) if( mNum[i]>0 ) nonzero++;
44

    
45
    mNumNonZero = nonzero;
46
    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
    if( mNumNonZero<2 ) indexExcluded=-1;
84

    
85
    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
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
148

    
149
  public int getTotal(int indexExcluded)
150
    {
151
    return ( indexExcluded>=0 && indexExcluded<mNumAxis ) ? mTotal-mNum[indexExcluded] : mTotal;
152
    }
153
}
(3-3/6)