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.helpers;
|
21
|
|
22
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
23
|
|
24
|
import java.util.Random;
|
25
|
|
26
|
public class ScrambleState
|
27
|
{
|
28
|
private final int mTotal, mNumAxis;
|
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 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
|
mTotal = total;
|
51
|
|
52
|
mInfo = new int[LEN*total];
|
53
|
int start = 0;
|
54
|
|
55
|
for(int i=0; i<mNumAxis; i++)
|
56
|
{
|
57
|
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
|
}
|
67
|
}
|
68
|
|
69
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
70
|
|
71
|
private int getIndex(int num, int indexExcluded)
|
72
|
{
|
73
|
int current= -1;
|
74
|
|
75
|
for(int i=0; i<mTotal; i++)
|
76
|
if( mInfo[LEN*i]!=indexExcluded && ++current==num ) return i;
|
77
|
|
78
|
return -1;
|
79
|
}
|
80
|
|
81
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
136
|
|
137
|
public int getTotal(int indexExcluded)
|
138
|
{
|
139
|
return ( indexExcluded>=0 && indexExcluded<mNumAxis ) ? mTotal-mNum[indexExcluded] : mTotal;
|
140
|
}
|
141
|
}
|