Project

General

Profile

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

magiccube / src / main / java / org / distorted / helpers / ScrambleState.java @ 6cf89a3e

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