rubikscube

college code for finding optimal rubiks cube solutions in java

git clone https://9o.is/git/rubikscube.git

commit fc6c5a6e96d2e7e091714c540bf1ffb542fc50e8
parent 317e7af991cb78cd1108d24b3e99a5307ddf5f0f
Author: Jul <jul@9o.is>
Date:   Sun, 21 Oct 2012 02:23:28 -0400

Fixed getNeighborStates in RubiksCube class

Diffstat:
Msrc/RubiksCube.java | 53+++++++++++++++++++++++++++++------------------------
Msrc/RubiksState.java | 6++++++
Msrc/RubiksTest.java | 31++++++++++++++++++++++++++++++-
3 files changed, 65 insertions(+), 25 deletions(-)

diff --git a/src/RubiksCube.java b/src/RubiksCube.java @@ -15,9 +15,6 @@ public class RubiksCube { /* 3-Dimensional array that stores the state of the cube. */ private String cube[][][]; - /* Amount of moves */ - private int moves = 0; - /** * Initiates a rubik's cube at it's goal state. */ @@ -48,15 +45,16 @@ public class RubiksCube { /** * Sets the state to the given state. - * TODO test */ - /*public void initState(RubiksState state) { + public void setState(RubiksState state) { final int CORNERS = 4; //corners per side - RubiksColor[] cornerColors = new RubiksColor[CUBE_SIDES*CORNERS]; + String[] cornerColors = new String[CUBE_SIDES*CORNERS]; int i=0; for(char ch : state.getCorners().toCharArray()) { - cornerColors[i] = RubiksColor.find(Character.toString(ch)); i++; + System.out.println(state.getCorners().toCharArray().length); + //cornerColors[i] = RubiksColor.find(Character.toString(ch)); i++; + cornerColors[i] = Character.toString(ch); i++; } for(i=0; i<CUBE_SIDES; i++) for(int j=0; j<CORNERS; j++) { @@ -72,11 +70,12 @@ public class RubiksCube { } final int HALF_EDGES = 2; //half edges per side - RubiksColor[] halfEdgesColors = new RubiksColor[CUBE_SIDES*HALF_EDGES]; + String[] halfEdgesColors = new String[CUBE_SIDES*HALF_EDGES]; i=0; for(char ch : state.getHalfEdges().toCharArray()) { - halfEdgesColors[i] = RubiksColor.find(Character.toString(ch)); i++; + //halfEdgesColors[i] = RubiksColor.find(Character.toString(ch)); i++; + halfEdgesColors[i] = Character.toString(ch); i++; } for(i=0; i<CUBE_SIDES; i++) for(int j=0; j<HALF_EDGES; j++) { @@ -89,7 +88,8 @@ public class RubiksCube { i=0; for(char ch : state.getHalf2Edges().toCharArray()) { - halfEdgesColors[i] = RubiksColor.find(Character.toString(ch)); i++; + //halfEdgesColors[i] = RubiksColor.find(Character.toString(ch)); i++; + halfEdgesColors[i] = Character.toString(ch); i++; } for(i=0; i<CUBE_SIDES; i++) for(int j=0; j<HALF_EDGES; j++) { @@ -99,7 +99,7 @@ public class RubiksCube { cube[i][2][1] = halfEdgesColors[i*HALF_EDGES + j]; } } - } */ + } /** * Resets the rubik's cube at it's goal state. @@ -155,26 +155,31 @@ public class RubiksCube { */ public RubiksState[] getNeighborStates() { RubiksState[] states = new RubiksState[18]; // 18 total states - for(int i=0; i<CUBE_SIZE; i++) { + for(int i=0; i<CUBE_SIZE-1; i++) { //vertical rotate(i,RubiksRotation.VERTICAL_CLOCKWISE); states[0+i] = getState(); - rotate(i,RubiksRotation.VERTICAL_180); - states[3+i] = getState(); - rotate(i,RubiksRotation.VERTICAL_CLOCKWISE); //original - rotate(i,RubiksRotation.VERTICAL_180); + rotate(i,RubiksRotation.VERTICAL_CLOCKWISE); + states[2+i] = getState(); + rotate(i,RubiksRotation.VERTICAL_CLOCKWISE); + states[4+i] = getState(); + rotate(i,RubiksRotation.VERTICAL_CLOCKWISE); //original state + + rotate(i,RubiksRotation.SIDE_VERTICAL_CLOCKWISE); states[6+i] = getState(); - rotate(i,RubiksRotation.VERTICAL_180); //original + rotate(i,RubiksRotation.SIDE_VERTICAL_CLOCKWISE); + states[8+i] = getState(); + rotate(i,RubiksRotation.SIDE_VERTICAL_CLOCKWISE); + states[10+i] = getState(); + rotate(i,RubiksRotation.SIDE_VERTICAL_CLOCKWISE); //original state - //horizontal rotate(i,RubiksRotation.HORIZONTAL_CLOCKWISE); - states[9+i] = getState(); - rotate(i,RubiksRotation.HORIZONTAL_180); states[12+i] = getState(); - rotate(i,RubiksRotation.HORIZONTAL_CLOCKWISE); //original - rotate(i,RubiksRotation.HORIZONTAL_180); - states[15+i] = getState(); - rotate(i,RubiksRotation.HORIZONTAL_180); //original + rotate(i,RubiksRotation.HORIZONTAL_CLOCKWISE); + states[14+i] = getState(); + rotate(i,RubiksRotation.HORIZONTAL_CLOCKWISE); + states[16+i] = getState(); + rotate(i,RubiksRotation.HORIZONTAL_CLOCKWISE); //original state } return states; } diff --git a/src/RubiksState.java b/src/RubiksState.java @@ -11,6 +11,12 @@ public class RubiksState { /* State of half of the other edges. */ private String half2Edges; + RubiksState(){ + corners = ""; + halfEdges = ""; + half2Edges = ""; + } + public void setCorners(String corners) { this.corners = corners; } diff --git a/src/RubiksTest.java b/src/RubiksTest.java @@ -22,6 +22,21 @@ class RubiksTest //cornersTable.clearTable(); RubiksCube cube = new RubiksCube(); + + // test if initState works + RubiksCube randomCube = new RubiksCube(true); + System.out.println("Goal State Cube:"); + cube.printState(); + System.out.println("Random State Cube:"); + randomCube.printState(); + + randomCube.setState(cube.getState()); + System.out.println("Random Cube in Goal State:"); + + + + + // all possible rotations (18) //cube.rotate(0, RubiksRotation.VERTICAL_CLOCKWISE); //CORRECT 1 //cube.rotate(1, RubiksRotation.VERTICAL_CLOCKWISE); //CORRECT 2 //cube.rotate(0, RubiksRotation.VERTICAL_COUNTERCLOCKWISE); //CORRECT 3 @@ -40,6 +55,20 @@ class RubiksTest //cube.rotate(1, RubiksRotation.SIDE_VERTICAL_COUNTERCLOCKWISE); //CORRECT 16 //cube.rotate(0, RubiksRotation.SIDE_VERTICAL_180); //CORRECT 17 //cube.rotate(1, RubiksRotation.SIDE_VERTICAL_180); //CORRECT 18 - cube.printState(); + + // checks if all neighbor states are unique + /*RubiksState[] states = cube.getNeighborStates(); + for(int i=0; i<states.length;i++) { + RubiksState state = states[i]; + for(int j=0; j<states.length;j++) { + RubiksState checkme = states[j]; + if(checkme.equals(state) && i != j) System.out.println("FAILED"); + } + System.out.println(state.getState()); + } + System.out.println("Done");*/ + + + } } \ No newline at end of file