-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCubeState.java
160 lines (146 loc) · 4.14 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class CubeState {
static final int R = 0;
static final int Rprim = 1;
static final int U = 2;
static final int Uprim = 3;
static final int F = 4;
static final int Fprim = 5;
int lastMove;
byte[] stickers;
CubeState(byte[] arr, int move) {
this.stickers = arr;
this.lastMove = move;
}
List<CubeState> neighbours() {
List<CubeState> neigh = new ArrayList<>();
for (int move = 0; move < 6; move++) {
byte[] stickersRot = rotate(move, stickers);
neigh.add(new CubeState(stickersRot, move));
}
return neigh;
}
static byte[] rotate(int move, byte[] stickers) {
byte[] stickersRot = Arrays.copyOf(stickers, stickers.length);
switch (move) {
case U:
rotateU(stickersRot, stickers);
break;
case Uprim:
rotateUprim(stickersRot, stickers);
break;
case R:
rotateR(stickersRot, stickers);
break;
case Rprim:
rotateRprim(stickersRot, stickers);
break;
case F:
rotateF(stickersRot, stickers);
break;
case Fprim:
rotateFprim(stickersRot, stickers);
break;
}
return stickersRot;
}
static void rotateU(byte[] stickersRot, byte[] stickers) {
stickersRot[5] = stickers[21];
stickersRot[6] = stickers[22];
stickersRot[1] = stickers[3];
stickersRot[2] = stickers[1];
stickersRot[3] = stickers[4];
stickersRot[21] = stickers[13];
stickersRot[22] = stickers[14];
stickersRot[13] = stickers[17];
stickersRot[14] = stickers[18];
stickersRot[17] = stickers[5];
stickersRot[18] = stickers[6];
stickersRot[4] = stickers[2];
}
static void rotateUprim(byte[] stickersRot, byte[] stickers) {
stickersRot[1] = stickers[2];
stickersRot[2] = stickers[4];
stickersRot[4] = stickers[3];
stickersRot[3] = stickers[1];
stickersRot[5] = stickers[17];
stickersRot[6] = stickers[18];
stickersRot[21] = stickers[5];
stickersRot[22] = stickers[6];
stickersRot[13] = stickers[21];
stickersRot[14] = stickers[22];
stickersRot[17] = stickers[13];
stickersRot[18] = stickers[14];
}
static void rotateR(byte[] stickersRot, byte[] stickers) {
stickersRot[2] = stickers[6];
stickersRot[4] = stickers[8];
stickersRot[21] = stickers[23];
stickersRot[22] = stickers[21];
stickersRot[23] = stickers[24];
stickersRot[24] = stickers[22];
stickersRot[6] = stickers[10];
stickersRot[8] = stickers[12];
stickersRot[10] = stickers[15];
stickersRot[12] = stickers[13];
stickersRot[13] = stickers[4];
stickersRot[15] = stickers[2];
}
static void rotateRprim(byte[] stickersRot, byte[] stickers) {
stickersRot[2] = stickers[15];
stickersRot[4] = stickers[13];
stickersRot[6] = stickers[2];
stickersRot[8] = stickers[4];
stickersRot[10] = stickers[6];
stickersRot[12] = stickers[8];
stickersRot[15] = stickers[10];
stickersRot[13] = stickers[12];
stickersRot[24] = stickers[23];
stickersRot[23] = stickers[21];
stickersRot[21] = stickers[22];
stickersRot[22] = stickers[24];
}
static void rotateF(byte[] stickersRot, byte[] stickers) {
stickersRot[3] = stickers[20];
stickersRot[4] = stickers[18];
stickersRot[21] = stickers[3];
stickersRot[23] = stickers[4];
stickersRot[18] = stickers[9];
stickersRot[20] = stickers[10];
stickersRot[10] = stickers[21];
stickersRot[9] = stickers[23];
stickersRot[6] = stickers[5];
stickersRot[5] = stickers[7];
stickersRot[7] = stickers[8];
stickersRot[8] = stickers[6];
}
static void rotateFprim(byte[] stickersRot, byte[] stickers) {
stickersRot[20] = stickers[3];
stickersRot[18] = stickers[4];
stickersRot[3] = stickers[21];
stickersRot[4] = stickers[23];
stickersRot[9] = stickers[18];
stickersRot[10] = stickers[20];
stickersRot[21] = stickers[10];
stickersRot[23] = stickers[9];
stickersRot[5] = stickers[6];
stickersRot[7] = stickers[5];
stickersRot[8] = stickers[7];
stickersRot[6] = stickers[8];
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
CubeState stickersState = (CubeState) o;
return Arrays.equals(stickers, stickersState.stickers);
}
@Override
public int hashCode() {
return Arrays.hashCode(stickers);
}
}