rubikscube
college code for finding optimal rubiks cube solutions in java
git clone https://9o.is/git/rubikscube.git
RubiksSubState.java
(1754B)
1 /**
2 * Holds part of the state of the rubik's cube with a heuristic that
3 * measures the amount of moves away from the goal state.
4 */
5 public abstract class RubiksSubState {
6
7 /* The state is the key of this RubiksSubState. */
8 protected String state;
9
10 /* The heuristic of this RubiksSubState. */
11 protected byte heuristic = -1;
12
13 /**
14 * An RubiksSubState has a state as a key and a heuristic.
15 * @param state the state
16 * @param heuristic the heuristic (moves from the goal state).
17 */
18 protected RubiksSubState(String state, short heuristic) {
19 this.state = state;
20 this.heuristic = (byte) heuristic;
21 }
22
23 protected RubiksSubState(String state, int heuristic) {
24 this(state, (short) heuristic);
25 }
26
27 protected RubiksSubState(String state) {
28 this.state = state;
29 }
30
31 /**
32 * Retrieve state.
33 * @return The state of RubiksSubState.
34 */
35 public String getState() {
36 return state;
37 }
38
39 /**
40 * Retrieve heuristic.
41 * @return The heuristic of RubiksSubState.
42 */
43 public byte getHeuristic() {
44 return heuristic;
45 }
46
47 @Override
48 public String toString() {
49 return state;
50 }
51
52 /**
53 * Simply a factorial function.
54 * @param n Number
55 * @return Factorial of n (n!)
56 */
57 protected static int fac(int n) {
58 if(n<=1) return 1;
59 return n * fac(n-1);
60 }
61
62 /**
63 * Checks if the state is valid.
64 * @param str The string to check.
65 * @return If the state validates.
66 */
67 protected abstract boolean isValid(String str);
68
69 /**
70 * Hashes the state into a numerical value.
71 * @return The hashed state.
72 */
73 protected abstract int hashState();
74 }