forked from clementgallet/FiveStage444
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCubeState.java
112 lines (94 loc) · 2.47 KB
/
CubeState.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package cg.fivestage444;
import cg.fivestage444.Cubies.EdgeCubies;
import cg.fivestage444.Cubies.CornerCubies;
import cg.fivestage444.Cubies.CenterCubies;
import java.util.Arrays;
import java.util.Random;
//CubeState structure: a cubie-level representation of the cube.
public final class CubeState{
public final EdgeCubies edges = new EdgeCubies();
public final CornerCubies corners = new CornerCubies();
public final CenterCubies centers = new CenterCubies();
public void init (){
edges.init();
corners.init();
centers.init();
}
public int is_solved (){
CubeState cube = new CubeState();
for (int sym=0; sym<Symmetry.N_SYM; sym++){
copyTo( cube );
cube.leftMult( sym );
if( cube.edges.is_solved() && cube.corners.is_solved() && cube.centers.is_solved() )
return sym;
}
return -1;
}
/**
* Generates a random cube.
* @return A random cube in the string representation. Each cube of the cube space has the same probability.
*/
public void randomise(Random r) {
init ();
/* Randomise corners */
randomPerm(r, corners.cubies, 8);
int os = 0;
for (int i=0; i<7; i++){
int o = r.nextInt(3);
corners.cubies[i] += 8*o;
os += o;
}
corners.cubies[7] += 8*((15 - os) % 3);
/* Randomize centers */
randomPerm(r, centers.cubies, 24);
/* Randomize edges */
randomPerm(r, edges.cubies, 24);
}
/* Fisher-Yates shuffle */
private static void randomPerm(Random r, byte[] array, int n) {
int i, j;
byte t;
for (i = n-1; i > 0; i--){
j = r.nextInt(i+1);
t = array[i];
array[i] = array[j];
array[j] = t;
}
}
public final void move (int move_code){
edges.move (move_code);
corners.move (move_code);
centers.move (move_code);
}
public void scramble (int move_count, byte[] move_arr){
int i;
for (i = 0; i < move_count; ++i) {
move (move_arr[i]);
}
}
public void copyTo (CubeState cube){
edges.copyTo(cube.edges);
corners.copyTo(cube.corners);
centers.copyTo(cube.centers);
}
public void leftMult (int symIdx){
edges.leftMult (symIdx);
corners.leftMult (symIdx);
centers.leftMult (symIdx);
}
/*
public void rightMult (int symIdx){
CubeState c = new CubeState();
copyTo(c);
c.rightMultEdges (symIdx, this);
c.rightMultCenters (symIdx, this);
c.rightMultCorners (symIdx, this);
}*/
// --Commented out by Inspection START (28/09/12 19:19):
// public void print (){
// edges.print();
// corners.print();
// centers.print();
// }
// --Commented out by Inspection STOP (28/09/12 19:19)
}