-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCube.java
More file actions
444 lines (377 loc) · 10.6 KB
/
Cube.java
File metadata and controls
444 lines (377 loc) · 10.6 KB
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
public class Cube {
public static enum Color {
WHITE(0), RED(1), GREEN(2), BLUE(3), ORANGE(4), YELLOW(5);
public int val;
Color(int val) { this.val = val; }
public String toString() {
switch(val) {
case 0: return "W";
case 1: return "R";
case 2: return "G";
case 3: return "B";
case 4: return "O";
case 5: return "Y";
default: return "N";
}
}
}
public static final Color WHITE = Color.WHITE;
public static final Color YELLOW = Color.YELLOW;
public static final Color RED = Color.RED;
public static final Color GREEN = Color.GREEN;
public static final Color BLUE = Color.BLUE;
public static final Color ORANGE = Color.ORANGE;
public static enum Type {
EDGE, CORNER
}
public static final int DBL_CW = 2;
public static final int CW = 1;
public static final int CCW = -1;
public static final int DBL_CCW = -2;
public static class Tile {
public final Color color;
public Face face;
public Tile(Color color) {
this.color = color;
}
public Tile(Color color, Face face) {
this.color = color;
this.face = face;
}
public boolean facing(Face f) {
return this.face == f;
}
public boolean facing(Color c) {
return this.face.color == c;
}
public String toString() {
return "T(" + color.toString() + ")";
}
}
public static class Spot {
public List<Color> colors;
public Spot(Color... colors) {
this.colors = new ArrayList<Color>();
for(Color c : colors) {
this.colors.add(c);
}
}
@Override
public int hashCode() {
int hc = 0;
for(Color c : colors) {
if(c != null) hc |= (1 << c.val);
}
return hc;
}
@Override
public boolean equals(Object other) {
if(!(other instanceof Spot)) return false;
return (((Spot) other) != null) && (((Spot) other).hashCode() == this.hashCode());
}
public String toString() {
return colors.toString();
}
}
public class Piece {
private Type type;
private Tile[] tiles;
public Piece(Type type) {
this.type = type;
tiles = new Tile[type == Type.EDGE ? 2 : 3];
}
public Piece(Spot spot, boolean initialize) {
this.type = spot.colors.size() == 2 ? Type.EDGE : Type.CORNER;
tiles = new Tile[type == Type.EDGE ? 2 : 3];
if(initialize) {
for(int i = 0; i < tiles.length; i++) {
Color c = spot.colors.get(i);
tiles[i] = new Tile(c, faces.get(c));
}
pieces.put(spot, this);
}
}
public void addTile(Color color, Face face) {
for(int i = 0; i < tiles.length; i++) {
if(tiles[i] == null) {
tiles[i] = new Tile(color, face);
return;
}
}
}
public Tile[] getTiles() { return tiles; }
public Spot spot() {
if(type == Type.EDGE) return new Spot(tiles[0].face.color, tiles[1].face.color);
else return new Spot(tiles[0].face.color, tiles[1].face.color, tiles[2].face.color);
}
public Spot colors() {
if(type == Type.EDGE) return new Spot(tiles[0].color, tiles[1].color);
else return new Spot(tiles[0].color, tiles[1].color, tiles[2].color);
}
public boolean facing(Face f) {
for(Tile t : tiles) {
if(t.facing(f)) return true;
}
return false;
}
public Face otherFace(Color... colors) {
if(this.type == Type.EDGE) {
Color not = colors[0];
for(Tile t : tiles) {
if(!t.facing(not)) return t.face;
}
} else {
Color not1 = colors[0];
Color not2 = colors[1];
for(Tile t : tiles) {
if(!t.facing(not1) && !t.facing(not2)) return t.face;
}
}
return null;
}
public Color otherColor(Color not) {
for(Tile t : tiles) {
if(t.color != not) return t.color;
}
return null;
}
public Color[] otherColors(Color not) {
Color[] others = new Color[2];
for(Tile t : tiles) {
if(t.color != not && others[0] == null) others[0] = t.color;
else if(t.color != not) others[1] = t.color;
}
return others;
}
public Tile getTile(Color c) {
for(Tile t : tiles) {
if(t.color == c) return t;
}
return null;
}
public Tile getTile(Face f) {
for(Tile t : tiles) {
if(t.facing(f)) return t;
}
return null;
}
public int layer() {
if(facing(faces.get(WHITE))) return 1;
if(facing(faces.get(YELLOW))) return 3;
return 2;
}
public String toString() {
String out = "P(" + tiles[0].toString() + "," + tiles[1].toString();
if(type == Type.CORNER) out += "," + tiles[2].toString();
return out + ") at " + spot().toString();
}
@Override
public int hashCode() {
int hc = 0;
for(Tile t : tiles) {
if(t != null && t.color != null) hc |= (1 << t.color.val);
}
return hc;
}
@Override
public boolean equals(Object other) {
return (((Piece) other) != null) && (((Piece) other).hashCode() == this.hashCode());
}
}
public class Face {
public Color color;
public Color[] adjs;
public Face(Color color, Color[] adjs) {
this.color = color;
this.adjs = adjs;
}
public Piece[] pieces() {
Piece[] pcs = new Piece[8];
for(int i = 0; i < 4; i++) {
Color curC = adjs[i];
Color nextC = adjs[(i+1) % 4];
pcs[2*i] = pieceAt.get(spot(this.color, curC));
pcs[(2*i)+1] = pieceAt.get(spot(this.color, curC, nextC));
}
return pcs;
}
public void rotate(int dir) {
Piece[] pcs = pieces();
for(Piece p : pcs) {
for(Tile t : p.getTiles()) {
if(!t.facing(this)) t.face = faces.get(nextColor(t.face.color, dir));
}
pieceAt.put(p.spot(), p);
}
}
public Color nextColor(Color c, int dir) {
int idx;
for(idx = 0; idx < 4; idx++) {
if(adjs[idx] == c) break;
}
return adjs[(idx + dir + 4) % 4];
}
public int relativeDir(Color c1, Color c2) {
int idx1 = 0, idx2 = 0;
for(int i = 0; i < 4; i++) {
if(adjs[i] == c1) idx1 = i;
if(adjs[i] == c2) idx2 = i;
}
int diff = Math.abs(idx1 - idx2);
if(diff == 2) { return DBL_CW; }
else if((diff == 1 && idx1 < idx2) || (diff == 3 && idx1 > idx2)) {
return CW;
} else {
return CCW;
}
}
public String toString() {
Piece[] pcs = pieces();
return "" +
"|" + pcs[7].getTile(this).color + "|" + pcs[0].getTile(this).color + "|" + pcs[1].getTile(this).color + "|\n" +
"|" + pcs[6].getTile(this).color + "|" + this.color + "|" + pcs[2].getTile(this).color + "|\n" +
"|" + pcs[5].getTile(this).color + "|" + pcs[4].getTile(this).color + "|" + pcs[3].getTile(this).color + "|\n";
}
}
public HashMap<Color, Face> faces;
public HashMap<Spot, Piece> pieces;
public HashMap<Spot, Piece> pieceAt;
public ArrayList<String> moveLog;
public Spot spot(Color... colors) {
return new Spot(colors);
}
public Cube(boolean initialize) {
pieces = new HashMap<Spot, Piece>();
pieceAt = new HashMap<Spot, Piece>();
faces = new HashMap<Color, Face>();
moveLog = new ArrayList<String>();
faces.put(WHITE, new Face(WHITE, new Color[]{ RED, GREEN, ORANGE, BLUE }));
faces.put(RED, new Face(RED, new Color[]{ WHITE, BLUE, YELLOW, GREEN }));
faces.put(GREEN, new Face(GREEN, new Color[]{ WHITE, RED, YELLOW, ORANGE }));
faces.put(BLUE, new Face(BLUE, new Color[]{ WHITE, ORANGE, YELLOW, RED }));
faces.put(ORANGE, new Face(ORANGE, new Color[]{ WHITE, GREEN, YELLOW, BLUE }));
faces.put(YELLOW, new Face(YELLOW, new Color[]{ RED, BLUE, ORANGE, GREEN }));
Face wFace = faces.get(WHITE);
Face yFace = faces.get(YELLOW);
Color[] sides = wFace.adjs;
for(int i = 0; i < 4; i++) {
Color curC = sides[i];
Color nextC = sides[(i+1) % 4];
Spot wEdge = spot(WHITE, curC);
Piece wePiece = new Piece(wEdge, initialize);
pieceAt.put(wEdge, wePiece);
Spot yEdge = spot(YELLOW, curC);
Piece yePiece = new Piece(yEdge, initialize);
pieceAt.put(yEdge, yePiece);
Spot sEdge = spot(curC, nextC);
Piece sePiece = new Piece(sEdge, initialize);
pieceAt.put(sEdge, sePiece);
Spot wCorner = spot(WHITE, curC, nextC);
Piece wcPiece = new Piece(wCorner, initialize);
pieceAt.put(wCorner, wcPiece);
Spot yCorner = spot(YELLOW, curC, nextC);
Piece ycPiece = new Piece(yCorner, initialize);
pieceAt.put(yCorner, ycPiece);
}
}
public Piece at(Color... colors) {
return pieceAt.get(spot(colors));
}
public Piece at(Spot spot) {
return pieceAt.get(spot);
}
public void rotate(Color c, int dir) {
faces.get(c).rotate(dir);
String toLog = "";
switch(dir) {
case CW: toLog = c.toString(); break;
case CCW: toLog = c.toString() + "'"; break;
case DBL_CW:
case DBL_CCW: toLog = c.toString() + c.toString(); break;
}
log(toLog);
}
public void mix(int num) {
Color[] colors = { WHITE, GREEN, RED, BLUE, ORANGE, YELLOW };
int[] moves = { DBL_CCW, CCW, CW, DBL_CW };
Random rand = new Random();
for(int i = 0; i < num; i++) {
Color c = colors[rand.nextInt(6)];
int move = moves[rand.nextInt(4)];
rotate(c, move);
}
}
public boolean isMove(String s) {
if(s == null || s.equals("")) return false;
char first = s.charAt(0);
if(!Character.isLetter(first)) return false;
if(s.length() == 1) return true;
if(s.length() == 2 && s.charAt(1) == first) return true;
if(s.length() == 2 && s.charAt(1) == '\'') return true;
return false;
}
public void execute(String[] moves, Color front) {
HashMap<String, Color> relativeFace = new HashMap<String, Color>();
relativeFace.put("U", YELLOW);
relativeFace.put("D", WHITE);
Color[] sides = faces.get(WHITE).adjs;
for(int i = 0; i < 4; i++) {
if(sides[i] == front) {
relativeFace.put("F", sides[i]);
relativeFace.put("R", sides[(i+1)%4]);
relativeFace.put("B", sides[(i+2)%4]);
relativeFace.put("L", sides[(i+3)%4]);
break;
}
}
for(int i = 0; i < moves.length; i++) {
if(isMove(moves[i])) {
String let = moves[i].substring(0, 1);
int dir;
if(moves[i].length() == 1) {
dir = CW;
} else if(moves[i].charAt(1) == '\'') {
dir = CCW;
} else {
dir = DBL_CW;
}
rotate(relativeFace.get(let), dir);
}
}
}
public void log(String s) {
moveLog.add(s);
}
public void compressLog() {
int i = 0;
String cur, next, next2;
while(i < log.size() - 2) {
cur = log.get(i);
next = log.get(i+1);
next2 = log.get(i+2);
if(isMove(cur) && isMove(next)) {
if(cur.equals(next + "'") || next.equals(cur + "'")) {
log.remove(i); log.remove(i+1); i--;
} else if(((cur.length() == 1) || (cur.length() == 2 && cur.charAt(1) == '\'')) && cur.equals(next)) {
log.set(i, cur.substring(0, 1) + cur.substring(0, 1));
log.remove(i+1); i--;
}
}
}
}
public ArrayList<String> getLog() {
return moveLog;
}
public String toString() {
String out = "";
Color[] colors = { WHITE, GREEN, RED, BLUE, ORANGE, YELLOW };
for(Color c : colors) {
out += faces.get(c).toString() + "\n";
}
return out;
}
}