rubikscube
college code for finding optimal rubiks cube solutions in java
git clone https://9o.is/git/rubikscube.git
RubiksColor.java
(2296B)
1 import java.util.Arrays;
2 import java.util.Collections;
3 import java.util.List;
4 import java.util.Random;
5
6 /**
7 * Different kinds of colors in a Rubik's cube.
8 */
9 public enum RubiksColor {
10 RED ("R",0),
11 GREEN ("G",1),
12 BLUE ("B",2),
13 YELLOW ("Y",3),
14 ORANGE ("O",4),
15 WHITE ("W",5);
16
17 /* The color in string format. */
18 private final String string;
19
20 /* The number of color. */
21 private final int num;
22
23 /* Every RubiksColor requires a string format. */
24 RubiksColor(String string, int num) {
25 this.string = string;
26 this.num = num;
27 }
28
29 /**
30 * Finds the RubiksColor type by searching with its string type.
31 * @param string The string of the RubiksColor.
32 * @return The correct RubiksColor of the string string.
33 */
34 public static RubiksColor find(String string) {
35 string = string.toUpperCase();
36 if(string.equals("R")) return RED;
37 else if(string.equals("G")) return GREEN;
38 else if(string.equals("B")) return BLUE;
39 else if(string.equals("Y")) return YELLOW;
40 else if(string.equals("O")) return ORANGE;
41 else if(string.equals("W")) return WHITE;
42 else return null;
43 }
44
45 /**
46 * Finds the RubiksColor type by searching with its int type.
47 * @param num The integer of the RubiksColor.
48 * @return The correct RubiksColor of the integer num.
49 */
50 public static RubiksColor find(int num) {
51 if(num == 0) return RED;
52 else if(num == 1) return GREEN;
53 else if(num == 2) return BLUE;
54 else if(num == 3) return YELLOW;
55 else if(num == 4) return ORANGE;
56 else if(num == 5) return WHITE;
57 else return null;
58 }
59
60 /**
61 * Picks a random rotation.
62 * @return The random rotation.
63 */
64 public static RubiksColor randomColor() {
65 return VALUES.get(new Random().nextInt(SIZE));
66 }
67
68 /* Cached list of all possible values. */
69 private static final List<RubiksColor> VALUES =
70 Collections.unmodifiableList(Arrays.asList(values()));
71
72 /* Cached size of all possible values. */
73 private static final int SIZE = VALUES.size();
74
75 public String toString() {
76 return this.string;
77 }
78
79 public int toInt() {
80 return this.num;
81 }
82 }