Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
package org.distorted.objectlib.helpers;
21

    
22
import java.util.Random;
23

    
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25

    
26
public class ScrambleState
27
{
28
  private final int mTotal, mNumAxis, mNumNonZero;
29
  private final int[] mNum;
30
  private final int[] mInfo;
31
  private final int[] mTmp;
32
  private final int LEN = 4;
33

    
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35

    
36
  public ScrambleState(int[][] axis)
37
    {
38
    mTmp = new int[LEN];
39

    
40
    mNumAxis = axis.length;
41
    mNum = new int[mNumAxis];
42
    int nonzero=0, total =0;
43

    
44
    for(int i=0; i<mNumAxis; i++)
45
      {
46
      mNum[i] = axis[i]==null ? 0 : axis[i].length/(LEN-1);
47
      total += mNum[i];
48
      }
49

    
50
    for(int i=0; i<mNumAxis; i++) if( mNum[i]>0 ) nonzero++;
51

    
52
    mNumNonZero = nonzero;
53
    mTotal = total;
54

    
55
    mInfo = new int[LEN*total];
56
    int start = 0;
57

    
58
    for(int i=0; i<mNumAxis; i++)
59
      {
60
      for(int j=0; j<mNum[i]; j++)
61
        {
62
        mInfo[LEN*j   + start] = i;
63
        mInfo[LEN*j+1 + start] = axis[i][(LEN-1)*j  ];
64
        mInfo[LEN*j+2 + start] = axis[i][(LEN-1)*j+1];
65
        mInfo[LEN*j+3 + start] = axis[i][(LEN-1)*j+2];
66
        }
67

    
68
      start += LEN*mNum[i];
69
      }
70
    }
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

    
74
  private int getIndex(int num, int indexExcluded)
75
    {
76
    int current= -1;
77

    
78
    for(int i=0; i<mTotal; i++)
79
      if( mInfo[LEN*i]!=indexExcluded && ++current==num ) return i;
80

    
81
    return -1;
82
    }
83

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

    
86
  public int[] getRandom(Random rnd, int indexExcluded, int[][] scrambleTable, int[] numOccurences)
87
    {
88
    int num=0, total=0, max=0, ax, layer;
89

    
90
    if( mNumNonZero<2 ) indexExcluded=-1;
91

    
92
    for(int i=0; i<mTotal; i++)
93
      {
94
      ax = mInfo[LEN*i];
95

    
96
      if( ax!=indexExcluded )
97
        {
98
        layer = mInfo[LEN*i+1];
99
        int value = scrambleTable[ax][layer];
100
        if( value>max ) max=value;
101
        }
102
      }
103

    
104
    for(int i=0; i<mTotal; i++)
105
      {
106
      ax = mInfo[LEN*i];
107

    
108
      if( ax!=indexExcluded )
109
        {
110
        layer = mInfo[LEN*i+1];
111
        int value = scrambleTable[ax][layer];
112
        numOccurences[total] = 1 + max - value + (total==0 ? 0 : numOccurences[total-1]);
113
        total++;
114
        }
115
      }
116

    
117
    float random= rnd.nextFloat()*numOccurences[total-1];
118

    
119
    for(int i=0; i<total; i++)
120
      {
121
      if( random <= numOccurences[i] )
122
        {
123
        num=i;
124
        break;
125
        }
126
      }
127

    
128
    int index = getIndex(num,indexExcluded);
129

    
130
    mTmp[0] = mInfo[LEN*index  ];   // axis
131
    mTmp[1] = mInfo[LEN*index+1];   // row
132
    mTmp[2] = mInfo[LEN*index+2];   // angle
133
    mTmp[3] = mInfo[LEN*index+3];   // next state
134

    
135
    scrambleTable[mTmp[0]][mTmp[1]]++;
136

    
137
    return mTmp;
138
    }
139

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

    
142
  public int getTotal(int indexExcluded)
143
    {
144
    return ( indexExcluded>=0 && indexExcluded<mNumAxis ) ? mTotal-mNum[indexExcluded] : mTotal;
145
    }
146
}
(8-8/10)