-
Notifications
You must be signed in to change notification settings - Fork 0
/
cube.h
342 lines (293 loc) · 10.2 KB
/
cube.h
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#ifndef CUBE_H
#define CUBE_H
#include <iostream>
#include <set>
#include <cstdint>
#include <string>
#include <vector>
// Cube state in the given orientation. When you rotate the faces it is done relative to this orientation.
// When you rotate the entire cube the face contents are preserved but the order is shuffled.
enum Color {
WHITE = 1,
YELLOW = 2,
RED = 4,
ORANGE = 8,
BLUE = 16,
GREEN = 32,
BLACK = 0
};
class CubeState {
public:
Color FRONT[3][3];
Color LEFT[3][3];
Color RIGHT[3][3];
Color UP[3][3];
Color DOWN[3][3];
Color BACK[3][3];
int ex[4] = {00, 01, 02, 01};
int ey[4] = {01, 02, 01, 00};
int cx[4] = {00, 00, 02, 02};
int cy[4] = {00, 02, 02, 00};
std::vector<bool> edges(Color face[3][3], Color c) {
std::vector<bool> res = {false, false, false, false};
for (int i = 0; i < 4; i++) {
if (face[ex[i]][ey[i]] == c) res[i] = true;
}
return res;
}
std::vector<bool> corners(Color face[3][3], Color c) {
std::vector<bool> res = {false, false, false, false};
for (int i = 0; i < 4; i++) {
if (face[cx[i]][cy[i]] == c) res[i] = true;
}
return res;
}
char colchar(Color c) {
Color cs[7] = {WHITE, YELLOW, RED, ORANGE, BLUE, GREEN, BLACK};
char ch[7] = {'W', 'Y', 'R', 'O', 'B', 'G', '.'};
for (int i = 0; i < 7; i++) {
if (c == cs[i]) return ch[i];
}
return '?';
}
CubeState() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
UP[i][j] = WHITE;
FRONT[i][j] = GREEN;
RIGHT[i][j] = RED;
DOWN[i][j] = YELLOW;
BACK[i][j] = BLUE;
LEFT[i][j] = ORANGE;
}
}
}
CubeState(const CubeState& other) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
UP[i][j] = other.UP[i][j];
FRONT[i][j] = other.FRONT[i][j];
RIGHT[i][j] = other.RIGHT[i][j];
DOWN[i][j] = other.DOWN[i][j];
BACK[i][j] = other.BACK[i][j];
LEFT[i][j] = other.LEFT[i][j];
}
}
}
// COORDINATE SYSTEM - The X axis is the first index and the Y axis the second.
// For the front and back faces, (0, 0) is the bottom left. The X axis goes to the right and the Y axis goes up.
// For the left and right faces, (0, 0) is bottom front. The X axis goes behind and the Y axis up.
// For the top and bottom faces, (0, 0) is the left front. The X axis goes right and the Y axis back.
// Essentially we are aligning axes to the 3-dimensional axes of the cube itself.
// Cube Rotations. The axes are a bit different - right handed Y up. Think Minecraft.
// All rotations are clockwise relative to the positive side/face.
// Rotates an array clockwise, assuming the X axis goes right and the Y axis goes up.
void clockwise(Color (&x)[3][3]) {
Color temp = x[0][0];
x[0][0] = x[2][0];
x[2][0] = x[2][2];
x[2][2] = x[0][2];
x[0][2] = temp;
temp = x[0][1];
x[0][1] = x[1][0];
x[1][0] = x[2][1];
x[2][1] = x[1][2];
x[1][2] = temp;
}
void counterclockwise(Color (&x)[3][3]) {
clockwise(x);
clockwise(x);
clockwise(x);
}
// The two faces normal to the rotation axis are rotated directly.
// However since the axes are reversed for one of them both faces technically rotate in the same direction.
void X_() {
clockwise(RIGHT);
clockwise(LEFT);
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
Color temp = FRONT[x][y];
FRONT[x][y] = DOWN[x][2 - y];
DOWN[x][2 - y] = BACK[x][2 - y];
BACK[x][2 - y] = UP[x][y];
UP[x][y] = temp;
}
}
}
void Y_() {
clockwise(UP);
clockwise(DOWN);
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
Color temp = FRONT[x][y];
FRONT[x][y] = RIGHT[x][y];
RIGHT[x][y] = BACK[2 - x][y];
BACK[2 - x][y] = LEFT[2 - x][y];
LEFT[2 - x][y] = temp;
}
}
}
void Z_() {
clockwise(FRONT);
clockwise(BACK);
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
Color temp = UP[x][y];
UP[x][y] = LEFT[y][x];
LEFT[y][x] = DOWN[2 - x][y];
DOWN[2 - x][y] = RIGHT[y][2 - x];
RIGHT[y][2 - x] = temp;
}
}
}
// Face rotations. Each face is rotated clockwise relative to looking at the face from the outside. (This means faces such as back are rotated inversely.)
// Pieces on adjacent sides are also rotated
void F_() {
clockwise(FRONT);
for (int x = 0; x < 3; x++) {
int y = 0;
Color temp = UP[x][y];
UP[x][y] = LEFT[y][x];
LEFT[y][x] = DOWN[2 - x][y];
DOWN[2 - x][y] = RIGHT[y][2 - x];
RIGHT[y][2 - x] = temp;
}
}
void B_() {
counterclockwise(BACK);
for (int x = 0; x < 3; x++) {
int y = 2;
Color temp = UP[x][y];
UP[x][y] = RIGHT[y][2 - x];
RIGHT[y][2 - x] = DOWN[2 - x][y];
DOWN[2 - x][y] = LEFT[y][x];
LEFT[y][x] = temp;
}
}
void U_() {
clockwise(UP);
for (int x = 0; x < 3; x++) {
int y = 2;
Color temp = FRONT[x][y];
FRONT[x][y] = RIGHT[x][y];
RIGHT[x][y] = BACK[2 - x][y];
BACK[2 - x][y] = LEFT[2 - x][y];
LEFT[2 - x][y] = temp;
}
}
void D_() {
counterclockwise(DOWN);
for (int x = 0; x < 3; x++) {
int y = 0;
Color temp = FRONT[x][y];
FRONT[x][y] = LEFT[2 - x][y];
LEFT[2 - x][y] = BACK[2 - x][y];
BACK[2 - x][y] = RIGHT[x][y];
RIGHT[x][y] = temp;
}
}
void R_() {
clockwise(RIGHT);
for (int y = 0; y < 3; y++) {
int x = 2;
Color temp = FRONT[x][y];
FRONT[x][y] = DOWN[x][2 - y];
DOWN[x][2 - y] = BACK[x][2 - y];
BACK[x][2 - y] = UP[x][y];
UP[x][y] = temp;
}
}
void L_() {
counterclockwise(LEFT);
for (int y = 0; y < 3; y++) {
int x = 0;
Color temp = FRONT[x][y];
FRONT[x][y] = UP[x][y];
UP[x][y] = BACK[x][2 - y];
BACK[x][2 - y] = DOWN[x][2 - y];
DOWN[x][2 - y] = temp;
}
}
std::string toString() {
std::string res = "";
for (int y = 0; y < 3; y++) {
res = res + " ";
for (int x = 0; x < 3; x++) res = res + colchar(UP[x][2 - y]);
res = res + "\n";
}
res = res + "\n";
for (int y = 0; y < 3; y++) {
for (int x = 0; x < 3; x++) res = res + colchar(LEFT[2 - x][2 - y]);
res = res + " ";
for (int x = 0; x < 3; x++) res = res + colchar(FRONT[x][2 - y]);
res = res + " ";
for (int x = 0; x < 3; x++) res = res + colchar(RIGHT[x][2 - y]);
res = res + " ";
for (int x = 0; x < 3; x++) res = res + colchar(BACK[2 - x][2 - y]);
res = res + "\n";
}
res = res + "\n";
for (int y = 0; y < 3; y++) {
res = res + " ";
for (int x = 0; x < 3; x++) res = res + colchar(DOWN[x][y]);
res = res + "\n";
}
return res;
}
void disp() {
std::cout << toString() << "\n";
}
bool isSolved() {
std::set<Color> faces;
faces.clear();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) faces.insert(FRONT[i][j]);
}
if (faces.size() != 1) return false;
faces.clear();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) faces.insert(LEFT[i][j]);
}
if (faces.size() != 1) return false;
faces.clear();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) faces.insert(UP[i][j]);
}
if (faces.size() != 1) return false;
faces.clear();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) faces.insert(BACK[i][j]);
}
if (faces.size() != 1) return false;
faces.clear();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) faces.insert(RIGHT[i][j]);
}
if (faces.size() != 1) return false;
faces.clear();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) faces.insert(DOWN[i][j]);
}
if (faces.size() != 1) return false;
return true;
}
int randr(int x) {
double bleep = (double)(rand()) / (double)(RAND_MAX);
return (int)(bleep * x);
}
void scramble(int moves = 256) {
for (int i = 0; i < moves; i++) {
int beep = randr(9);
if (beep == 0) F_();
if (beep == 1) R_();
if (beep == 2) U_();
if (beep == 3) B_();
if (beep == 4) L_();
if (beep == 5) D_();
if (beep == 6) X_();
if (beep == 7) Y_();
if (beep == 8) Z_();
}
}
};
#endif