rubikscube
college code for finding optimal rubiks cube solutions in java
git clone https://9o.is/git/rubikscube.git
RubiksState.java
(5130B)
1 import java.util.LinkedList;
2 import java.util.List;
3
4 /**
5 * A Rubik's Cube State.
6 */
7 public class RubiksState {
8
9 /* State of the corners. */
10 private RubiksCornerState corners;
11
12 /* State of half of the edges. */
13 private RubiksHalfEdgeState halfEdges;
14
15 /* State of half of the other edges. */
16 private RubiksHalfEdgeState half2Edges;
17
18 /* Heuristic of the state. */
19 private byte heuristic = -1;
20
21 /**
22 * Initiates this empty state.
23 */
24 public RubiksState(
25 String corners,
26 String halfEdges,
27 String half2Edges) throws Exception {
28 if(!isEdgeValid(halfEdges+half2Edges))
29 throw new Exception();
30
31 this.corners = new RubiksCornerState(corners);
32 this.halfEdges = new RubiksHalfEdgeState(halfEdges);
33 this.half2Edges = new RubiksHalfEdgeState(half2Edges);
34 }
35
36 public RubiksState(
37 String corners,
38 String halfEdges,
39 String half2Edges,
40 int heuristic) throws Exception {
41 this(corners,halfEdges,half2Edges);
42 this.heuristic = (byte) heuristic;
43 }
44
45 public RubiksState(RubiksCube cube) throws Exception {
46 if(!isEdgeValid(
47 cube.getHalfEdgeState()+cube.getHalf2EdgeState()))
48 throw new Exception();
49
50 this.corners = new RubiksCornerState(cube.getCornerState());
51 this.halfEdges = new RubiksHalfEdgeState(cube.getHalfEdgeState());
52 this.half2Edges = new RubiksHalfEdgeState(cube.getHalf2EdgeState());
53 }
54
55 public RubiksState(RubiksCube cube, int heuristic) throws Exception {
56 this(cube);
57 this.heuristic = (byte) heuristic;
58 }
59
60 public RubiksState(RubiksState state) throws Exception {
61 if(!isEdgeValid(
62 state.getHalfEdges().toString() +
63 state.getHalf2Edges().toString()))
64 throw new Exception();
65
66 this.corners = state.getCorners();
67 this.halfEdges = state.getHalfEdges();
68 this.half2Edges = state.getHalf2Edges();
69 }
70
71 public RubiksState(RubiksState state, int heuristic) throws Exception {
72 this(state);
73 this.heuristic = (byte) heuristic;
74 }
75
76 /**
77 * Set the state for this corners' state.
78 * @param corners New state for corners.
79 */
80 public void setCorners(String corners) throws Exception {
81 this.corners = new RubiksCornerState(corners);
82 }
83
84 /**
85 * Set the state for this half of the edges' state.
86 * @param halfEdges New state for half of the edges.
87 */
88 public void setHalfEdges(String halfEdges) throws Exception {
89 this.halfEdges = new RubiksHalfEdgeState(halfEdges);
90 }
91
92 /**
93 * Set the state for this second half of the edges' state.
94 * @param half2Edges New state for second half of the edges.
95 */
96 public void setHalf2Edges(String half2Edges) throws Exception {
97 this.half2Edges = new RubiksHalfEdgeState(half2Edges);
98 }
99
100 /**
101 * Retrieves the state of the corners.
102 * @return State of the corners.
103 */
104 public RubiksCornerState getCorners() {
105 return corners;
106 }
107
108 /**
109 * Retrieves the state of first half of the edges.
110 * @return State of first half of the edges.
111 */
112 public RubiksHalfEdgeState getHalfEdges() {
113 return halfEdges;
114 }
115
116 /**
117 * Retrieves the state of second half of the edges.
118 * @return State of second half of the edges.
119 */
120 public RubiksHalfEdgeState getHalf2Edges() {
121 return half2Edges;
122 }
123
124 /**
125 * Heuristic.
126 * @return heuristic
127 */
128 public byte getHeuristic() {
129 return heuristic;
130 }
131
132 /**
133 * All part-states combined to make a final Rubik's cube state..
134 * @return The entire state of a cube.
135 */
136 public String getState() {
137 return
138 getCorners().toString() +
139 getHalfEdges().toString() +
140 getHalf2Edges().toString();
141 }
142
143 /**
144 * Since RubiksState combines two halves of one rubik's edge
145 * state, it must make sure they both match to be in one cube.
146 * @param str The string sequence of all edge state.
147 * @return True if it passes the test.
148 */
149 protected boolean isEdgeValid(String str) {
150 if(str.length() == 24) {
151 List<Integer> edgesChecked = new LinkedList<Integer>();
152 for(int i=0; i<str.length(); i+=2) {
153 String edge =
154 str.substring(i,i+2).toUpperCase();
155 int check = RubiksHalfEdgeState.isValidEdge(edge);
156 if(check == -1) return false;
157 else {
158 int count=0; //can only be counted up to two
159 for(int j=0; j<edgesChecked.size(); j++) {
160 int edgeUsed = edgesChecked.get(j);
161 if(edgeUsed == check)
162 if(++count > 1) return false;
163 }
164 edgesChecked.add(check);
165 }
166 }
167 return true;
168 }
169 return false;
170 }
171 }