rubikscube

college code for finding optimal rubiks cube solutions in java

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

commit 317e7af991cb78cd1108d24b3e99a5307ddf5f0f
parent 42ad1f3e1e1da4fa87b26adffdff7f8027940a06
Author: Jul <jul@9o.is>
Date:   Sun, 21 Oct 2012 01:25:38 -0400

Fixed major flaws in rubik's cube rotation.

Diffstat:
Msrc/RubiksCube.java | 530++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------
Msrc/RubiksRotation.java | 15+++++++++------
Asrc/RubiksTest.java | 46++++++++++++++++++++++++++++++++++++++++++++++
Dsrc/ThreeDMatrix.java | 28----------------------------
4 files changed, 404 insertions(+), 215 deletions(-)

diff --git a/src/RubiksCube.java b/src/RubiksCube.java @@ -9,11 +9,11 @@ public class RubiksCube { /* Amount of sides or faces (6) in this cube. */ private final int CUBE_SIDES = 6; - /* Size of the cube. (3 by 3) */ - final int CUBE_SIZE = 3; + /* Size of the cube. (3 by 3). Might break if changed. */ + private final int CUBE_SIZE = 3; /* 3-Dimensional array that stores the state of the cube. */ - private RubiksColor cube[][][]; + private String cube[][][]; /* Amount of moves */ private int moves = 0; @@ -22,7 +22,7 @@ public class RubiksCube { * Initiates a rubik's cube at it's goal state. */ RubiksCube() { - cube = new RubiksColor[CUBE_SIDES][CUBE_SIZE][CUBE_SIZE]; + cube = new String[CUBE_SIDES][CUBE_SIZE][CUBE_SIZE]; initGoalState(); } @@ -31,7 +31,7 @@ public class RubiksCube { * false or it randomizes the state if parameter is true. */ RubiksCube(boolean random) { - cube = new RubiksColor[CUBE_SIDES][CUBE_SIZE][CUBE_SIZE]; + cube = new String[CUBE_SIDES][CUBE_SIZE][CUBE_SIZE]; if(random) { initGoalState(); initRandomState(); @@ -42,14 +42,15 @@ public class RubiksCube { * Initiates a Rubik's cube from an existing state. */ RubiksCube(RubiksState state) { - cube = new RubiksColor[CUBE_SIDES][CUBE_SIZE][CUBE_SIZE]; - initState(state); + cube = new String[CUBE_SIDES][CUBE_SIZE][CUBE_SIZE]; + //initState(state); } /** * Sets the state to the given state. + * TODO test */ - public void initState(RubiksState state) { + /*public void initState(RubiksState state) { final int CORNERS = 4; //corners per side RubiksColor[] cornerColors = new RubiksColor[CUBE_SIDES*CORNERS]; @@ -98,22 +99,23 @@ public class RubiksCube { cube[i][2][1] = halfEdgesColors[i*HALF_EDGES + j]; } } - } + } */ /** * Resets the rubik's cube at it's goal state. */ public void initGoalState() { - int side,row,column; + int side,row,column, count=0; for (side=0; side<CUBE_SIDES; side++) for(row=0; row<CUBE_SIZE; row++) for (column=0; column<CUBE_SIZE; column++) { - if(side==0) cube[side][row][column]= RubiksColor.RED; + cube[side][row][column] = ++count + " "; + /*if(side==0) cube[side][row][column]= RubiksColor.RED; else if(side==1) cube[side][row][column]= RubiksColor.GREEN; else if(side==2) cube[side][row][column]= RubiksColor.YELLOW; else if(side==3) cube[side][row][column]= RubiksColor.BLUE; else if(side==4) cube[side][row][column]= RubiksColor.ORANGE; - else cube[side][row][column]= RubiksColor.WHITE; + else cube[side][row][column]= RubiksColor.WHITE; */ } } @@ -123,38 +125,7 @@ public class RubiksCube { public void initRandomState() { Random random = new Random(); for(int i=0; i<100; i++) { - rotate(random.nextInt(CUBE_SIZE), RubiksRotation.randomRotation()); - } - } - - /** - * Rotates a piece of the cube to change its state. - * A maximum of 18 child states are available. - * @param piece Piece to rotate (0-2 if cube size is 3) - * @param rotation Type of rotation - */ - public void rotate(int piece, RubiksRotation rotation) { - switch (rotation) { - case VERTICAL_CLOCKWISE_90: - rotatePieceVerticalClockwise90(piece); - break; - case VERTICAL_COUNTERCLOCKWISE_90: - rotatePieceVerticalCounterClockwise90(piece); - break; - case VERTICAL_180_DEGREES: - rotatePieceVertical180(piece); - break; - case HORIZONTAL_CLOCKWISE_90: - rotatePieceHorizontalClockwise90(piece); - break; - case HORIZONTAL_COUNTERCLOCKWISE_90: - rotatePieceHorizontalCounterClockwise90(piece); - break; - case HORIZONTAL_180_DEGREES: - rotatePieceHorizontal180(piece); - break; - default: - break; + rotate(random.nextInt(1), RubiksRotation.randomRotation()); } } @@ -186,24 +157,24 @@ public class RubiksCube { RubiksState[] states = new RubiksState[18]; // 18 total states for(int i=0; i<CUBE_SIZE; i++) { //vertical - rotate(i,RubiksRotation.VERTICAL_CLOCKWISE_90); + rotate(i,RubiksRotation.VERTICAL_CLOCKWISE); states[0+i] = getState(); - rotate(i,RubiksRotation.VERTICAL_180_DEGREES); + rotate(i,RubiksRotation.VERTICAL_180); states[3+i] = getState(); - rotate(i,RubiksRotation.VERTICAL_CLOCKWISE_90); //original - rotate(i,RubiksRotation.VERTICAL_180_DEGREES); + rotate(i,RubiksRotation.VERTICAL_CLOCKWISE); //original + rotate(i,RubiksRotation.VERTICAL_180); states[6+i] = getState(); - rotate(i,RubiksRotation.VERTICAL_180_DEGREES); //original + rotate(i,RubiksRotation.VERTICAL_180); //original //horizontal - rotate(i,RubiksRotation.HORIZONTAL_CLOCKWISE_90); + rotate(i,RubiksRotation.HORIZONTAL_CLOCKWISE); states[9+i] = getState(); - rotate(i,RubiksRotation.HORIZONTAL_180_DEGREES); + rotate(i,RubiksRotation.HORIZONTAL_180); states[12+i] = getState(); - rotate(i,RubiksRotation.HORIZONTAL_CLOCKWISE_90); //original - rotate(i,RubiksRotation.HORIZONTAL_180_DEGREES); + rotate(i,RubiksRotation.HORIZONTAL_CLOCKWISE); //original + rotate(i,RubiksRotation.HORIZONTAL_180); states[15+i] = getState(); - rotate(i,RubiksRotation.HORIZONTAL_180_DEGREES); //original + rotate(i,RubiksRotation.HORIZONTAL_180); //original } return states; } @@ -247,211 +218,408 @@ public class RubiksCube { System.out.println(); } - private void rotatePieceVerticalClockwise90(int piece) { - if(!isValidPiece(piece)) return; + /** + * Rotates a piece of the cube to change its state. + * A maximum of 18 child states are available. + * @param piece Piece to rotate (0-1 if cube size is 3, ignores the middle piece) + * @param rotation Type of rotation + */ + public void rotate(int piece, RubiksRotation rotation) { + try { + checkValidPiece(piece); + piece = fixPieceValue(piece); + + switch (rotation) { + case VERTICAL_CLOCKWISE: + rotatePieceVerticalClockwise(piece); + break; + case VERTICAL_COUNTERCLOCKWISE: + rotatePieceVerticalCounterClockwise(piece); + break; + case VERTICAL_180: + rotatePieceVertical180(piece); + break; + case SIDE_VERTICAL_CLOCKWISE: + rotateSidePieceVerticalClockwise(piece); + break; + case SIDE_VERTICAL_COUNTERCLOCKWISE: + rotateSidePieceVerticalCounterClockwise(piece); + break; + case SIDE_VERTICAL_180: + rotateSidePieceVertical180(piece); + break; + case HORIZONTAL_CLOCKWISE: + rotatePieceHorizontalClockwise(piece); + break; + case HORIZONTAL_COUNTERCLOCKWISE: + rotatePieceHorizontalCounterClockwise(piece); + break; + case HORIZONTAL_180: + rotatePieceHorizontal180(piece); + break; + default: + break; + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + /* + * Rotates a piece vertically by 90 degrees counter clockwise. + * @param piece The piece to rotate. + */ + private void rotatePieceVerticalClockwise(int piece) throws Exception { // change side 0 - RubiksColor side0row0 = setValue(0,0,piece, cube[5][0][piece]); - RubiksColor side0row1 = setValue(0,1,piece, cube[5][1][piece]); - RubiksColor side0row2 = setValue(0,2,piece, cube[5][2][piece]); + String side0row0 = setColor(0, 0, piece, cube[5][0][piece]); + String side0row1 = setColor(0, 1, piece, cube[5][1][piece]); + String side0row2 = setColor(0, 2, piece, cube[5][2][piece]); // change side 2 - RubiksColor side2row0 = setValue(2,0,piece, side0row0); - RubiksColor side2row1 = setValue(2,1,piece, side0row1); - RubiksColor side2row2 = setValue(2,2,piece, side0row2); + String side2row0 = setColor(2, 0, piece, side0row0); + String side2row1 = setColor(2, 1, piece, side0row1); + String side2row2 = setColor(2, 2, piece, side0row2); // change side 4 - RubiksColor side4row0 = setValue(4,0,piece, side2row0); - RubiksColor side4row1 = setValue(4,1,piece, side2row1); - RubiksColor side4row2 = setValue(4,2,piece, side2row2); + String side4row0 = setColor(4, 0, piece, side2row0); + String side4row1 = setColor(4, 1, piece, side2row1); + String side4row2 = setColor(4, 2, piece, side2row2); // change side 5 - RubiksColor side5row0 = setValue(5,0,piece, side4row0); - RubiksColor side5row1 = setValue(5,1,piece, side4row1); - RubiksColor side5row2 = setValue(5,2,piece, side4row2); + String side5row0 = setColor(5, 0, piece, side4row0); + String side5row1 = setColor(5, 1, piece, side4row1); + String side5row2 = setColor(5, 2, piece, side4row2); - if(piece==0) rotateSide90Clockwise(1); - else if(piece==2) rotateSide90CounterClockwise(3); + if(piece==0) rotateSideClockwise(1); + else if(piece==2) rotateSideCounterClockwise(3); } - private void rotatePieceVerticalCounterClockwise90(int piece) { - if(!isValidPiece(piece)) return; + /* + * Rotates a piece vertically by 90 degrees counter clockwise. + * @param piece The piece to rotate. + */ + private void rotatePieceVerticalCounterClockwise(int piece) throws Exception { // change side 0 - RubiksColor side0row0 = setValue(0,0,piece, cube[2][0][piece]); - RubiksColor side0row1 = setValue(0,1,piece, cube[2][1][piece]); - RubiksColor side0row2 = setValue(0,2,piece, cube[2][2][piece]); + String side0row0 = setColor(0, 0, piece, cube[2][0][piece]); + String side0row1 = setColor(0, 1, piece, cube[2][1][piece]); + String side0row2 = setColor(0, 2, piece, cube[2][2][piece]); // change side 5 - RubiksColor side5row0 = setValue(5,0,piece, side0row0); - RubiksColor side5row1 = setValue(5,1,piece, side0row1); - RubiksColor side5row2 = setValue(5,2,piece, side0row2); + String side5row0 = setColor(5, 0, piece, side0row0); + String side5row1 = setColor(5, 1, piece, side0row1); + String side5row2 = setColor(5, 2, piece, side0row2); // change side 4 - RubiksColor side4row0 = setValue(4,0,piece, side5row0); - RubiksColor side4row1 = setValue(4,1,piece, side5row1); - RubiksColor side4row2 = setValue(4,2,piece, side5row2); + String side4row0 = setColor(4, 0, piece, side5row0); + String side4row1 = setColor(4, 1, piece, side5row1); + String side4row2 = setColor(4, 2, piece, side5row2); // change side 2 - RubiksColor side2row0 = setValue(2,0,piece, side4row0); - RubiksColor side2row1 = setValue(2,1,piece, side4row1); - RubiksColor side2row2 = setValue(2,2,piece, side4row2); + String side2row0 = setColor(2, 0, piece, side4row0); + String side2row1 = setColor(2, 1, piece, side4row1); + String side2row2 = setColor(2, 2, piece, side4row2); - if(piece==0) rotateSide90CounterClockwise(1); - else if(piece==2) rotateSide90Clockwise(3); + if(piece==0) rotateSideCounterClockwise(1); + else if(piece==2) rotateSideClockwise(3); } - private void rotatePieceVertical180(int piece) { - if(!isValidPiece(piece)) return; + /* + * Rotates a piece vertically by 180 degrees. + * @param piece The piece to rotate. + */ + private void rotatePieceVertical180(int piece) throws Exception { // switch side 0 with side 4 - switchValues(0, 0, piece, 4, 0, piece); - switchValues(0, 1, piece, 4, 1, piece); - switchValues(0, 2, piece, 4, 2, piece); + switchColors(0, 0, piece, 4, 0, piece); + switchColors(0, 1, piece, 4, 1, piece); + switchColors(0, 2, piece, 4, 2, piece); // switch side 5 with side 2 - switchValues(5, 0, piece, 2, 0, piece); - switchValues(5, 1, piece, 2, 1, piece); - switchValues(5, 2, piece, 2, 2, piece); + switchColors(5, 0, piece, 2, 0, piece); + switchColors(5, 1, piece, 2, 1, piece); + switchColors(5, 2, piece, 2, 2, piece); if(piece==0) rotateSide180(1); else if(piece==2) rotateSide180(3); } - // rotate to left - private void rotatePieceHorizontalClockwise90(int piece) { - if(!isValidPiece(piece)) return; + /* + * Rotates a side piece vertically by 90 degrees counter clockwise. + * @param piece The piece to rotate. + */ + private void rotateSidePieceVerticalClockwise(int piece) throws Exception { + // change side 0 + String side0col0 = setColor(0, piece, 0, cube[1][2][piece]); + String side0col1 = setColor(0, piece, 1, cube[1][1][piece]); + String side0col2 = setColor(0, piece, 2, cube[1][0][piece]); + + // change side 3 + piece = switchPiece(piece); + String side3col0 = setColor(3, 0, piece, side0col0); + String side3col1 = setColor(3, 1, piece, side0col1); + String side3col2 = setColor(3, 2, piece, side0col2); + + // change side 4 + String side4col0 = setColor(4, piece, 2, side3col0); + String side4col1 = setColor(4, piece, 1, side3col1); + String side4col2 = setColor(4, piece, 0, side3col2); + piece = switchPiece(piece); + + // change side 1 + String side1col0 = setColor(1, 2, piece, side4col0); + String side1col1 = setColor(1, 1, piece, side4col1); + String side1col2 = setColor(1, 0, piece, side4col2); + + if(piece==0) rotateSideCounterClockwise(5); + else if(piece==2) rotateSideClockwise(2); + } + + /* + * Rotates a side piece vertically by 90 degrees counter clockwise. + * @param piece The piece to rotate. + */ + private void rotateSidePieceVerticalCounterClockwise(int piece) throws Exception { + // change side 0 + String side0col0 = setColor(0, piece, 0, cube[3][0][switchPiece(piece)]); + String side0col1 = setColor(0, piece, 1, cube[3][1][switchPiece(piece)]); + String side0col2 = setColor(0, piece, 2, cube[3][2][switchPiece(piece)]); + // change side 1 - RubiksColor side1col0 = setValue(1,piece,0, cube[2][piece][0]); - RubiksColor side1col1 = setValue(1,piece,1, cube[2][piece][1]); - RubiksColor side1col2 = setValue(1,piece,2, cube[2][piece][2]); + String side1col0 = setColor(1, 2, piece, side0col0); + String side1col1 = setColor(1, 1, piece, side0col1); + String side1col2 = setColor(1, 0, piece, side0col2); - // change side 5 - RubiksColor side5col0 = setValue(5,piece,0, side1col0); - RubiksColor side5col1 = setValue(5,piece,1, side1col1); - RubiksColor side5col2 = setValue(5,piece,2, side1col2); + // change side 4 + piece = switchPiece(piece); + String side4col0 = setColor(4, piece, 2, side1col0); + String side4col1 = setColor(4, piece, 1, side1col1); + String side4col2 = setColor(4, piece, 0, side1col2); // change side 3 - RubiksColor side3col0 = setValue(3,piece,0, side5col0); - RubiksColor side3col1 = setValue(3,piece,1, side5col1); - RubiksColor side3col2 = setValue(3,piece,2, side5col2); + String side3col0 = setColor(3, 0, piece, side4col0); + String side3col1 = setColor(3, 1, piece, side4col1); + String side3col2 = setColor(3, 2, piece, side4col2); + piece = switchPiece(piece); - // change side 2 - RubiksColor side2col0 = setValue(2,piece,0, side3col0); - RubiksColor side2col1 = setValue(2,piece,1, side3col1); - RubiksColor side2col2 = setValue(2,piece,2, side3col2); + if(piece==0) rotateSideClockwise(5); + else if(piece==2) rotateSideCounterClockwise(2); + } + + /* + * Rotates a side piece vertically by 180 degrees. + * @param piece The piece to rotate. + */ + private void rotateSidePieceVertical180(int piece) throws Exception { + // switch side 0 with side 4 + switchColors(0, piece, 2, 4, switchPiece(piece), 0); + switchColors(0, piece, 1, 4, switchPiece(piece), 1); + switchColors(0, piece, 0, 4, switchPiece(piece), 2); + + // switch side 1 with side 3 + switchColors(1, 2, piece, 3, 0, switchPiece(piece)); + switchColors(1, 1, piece, 3, 1, switchPiece(piece)); + switchColors(1, 0, piece, 3, 2, switchPiece(piece)); - if(piece==0) rotateSide90Clockwise(0); - else if(piece==2) rotateSide90CounterClockwise(4); + if(piece==0) rotateSide180(5); + else if(piece==2) rotateSide180(2); } - // rotate to right - private void rotatePieceHorizontalCounterClockwise90(int piece) { - if(!isValidPiece(piece)) return; + /* + * Rotates a piece horizontally by 90 degrees clockwise. + * @param piece The piece to rotate. + */ + private void rotatePieceHorizontalClockwise(int piece) throws Exception { // change side 1 - RubiksColor side1col0 = setValue(1,piece,0, cube[5][piece][0]); - RubiksColor side1col1 = setValue(1,piece,1, cube[5][piece][1]); - RubiksColor side1col2 = setValue(1,piece,2, cube[5][piece][2]); + String side1col0 = setColor(1, piece, 0, cube[2][piece][0]); + String side1col1 = setColor(1, piece, 1, cube[2][piece][1]); + String side1col2 = setColor(1, piece, 2, cube[2][piece][2]); + + // change side 5 + piece = switchPiece(piece); + String side5col0 = setColor(5, piece, 2, side1col0); + String side5col1 = setColor(5, piece, 1, side1col1); + String side5col2 = setColor(5, piece, 0, side1col2); + piece = switchPiece(piece); + + // change side 3 + String side3col0 = setColor(3, piece, 0, side5col0); + String side3col1 = setColor(3, piece, 1, side5col1); + String side3col2 = setColor(3, piece, 2, side5col2); // change side 2 - RubiksColor side2col0 = setValue(2,piece,0, side1col0); - RubiksColor side2col1 = setValue(2,piece,1, side1col1); - RubiksColor side2col2 = setValue(2,piece,2, side1col2); + String side2col0 = setColor(2, piece, 0, side3col0); + String side2col1 = setColor(2, piece, 1, side3col1); + String side2col2 = setColor(2, piece, 2, side3col2); + if(piece==0) rotateSideClockwise(0); + else if(piece==2) rotateSideCounterClockwise(4); + } + + /* + * Rotates a piece horizontally by 90 degrees counter clockwise. + * @param piece The piece to rotate. + */ + private void rotatePieceHorizontalCounterClockwise(int piece) throws Exception { // change side 3 - RubiksColor side3col0 = setValue(3,piece,0, side2col0); - RubiksColor side3col1 = setValue(3,piece,1, side2col1); - RubiksColor side3col2 = setValue(3,piece,2, side2col2); + String side3col0 = setColor(3, piece, 0, cube[2][piece][0]); + String side3col1 = setColor(3, piece, 1, cube[2][piece][1]); + String side3col2 = setColor(3, piece, 2, cube[2][piece][2]); // change side 5 - RubiksColor side5col0 = setValue(5,piece,0, side3col0); - RubiksColor side5col1 = setValue(5,piece,1, side3col1); - RubiksColor side5col2 = setValue(5,piece,2, side3col2); + piece = switchPiece(piece); + String side5col0 = setColor(5, piece, 2, side3col0); + String side5col1 = setColor(5, piece, 1, side3col1); + String side5col2 = setColor(5, piece, 0, side3col2); + piece = switchPiece(piece); + + // change side 1 + String side1col0 = setColor(1, piece, 0, side5col0); + String side1col1 = setColor(1, piece, 1, side5col1); + String side1col2 = setColor(1, piece, 2, side5col2); + + // change side 2 + String side2col0 = setColor(2, piece, 0, side1col0); + String side2col1 = setColor(2, piece, 1, side1col1); + String side2col2 = setColor(2, piece, 2, side1col2); - if(piece==0) rotateSide90CounterClockwise(0); - else if(piece==2) rotateSide90Clockwise(4); + if(piece==0) rotateSideCounterClockwise(0); + else if(piece==2) rotateSideClockwise(4); } - private void rotatePieceHorizontal180(int piece) { - if(!isValidPiece(piece)) return; + /* + * Rotates a piece horizontally by 180 degrees. + * @param piece The piece to rotate. + */ + private void rotatePieceHorizontal180(int piece) throws Exception { // switch side 2 with side 5 - switchValues(2, piece, 0, 5, piece, 0); - switchValues(2, piece, 1, 5, piece, 1); - switchValues(2, piece, 2, 5, piece, 2); + switchColors(2, piece, 0, 5, switchPiece(piece), 2); + switchColors(2, piece, 1, 5, switchPiece(piece), 1); + switchColors(2, piece, 2, 5, switchPiece(piece), 0); // switch side 1 with side 3 - switchValues(1, piece, 0, 3, piece, 0); - switchValues(1, piece, 1, 3, piece, 1); - switchValues(1, piece, 2, 3, piece, 2); + switchColors(1, piece, 0, 3, piece, 0); + switchColors(1, piece, 1, 3, piece, 1); + switchColors(1, piece, 2, 3, piece, 2); if(piece==0) rotateSide180(0); else if(piece==2) rotateSide180(4); } - // rotates the values in the same side. Does not manipulate any other side. - // the middle part never moves so there are 8 moves in total if its 3x3 - - - private void rotateSide90Clockwise(int side) { - if(!isValidSide(side)) return; + /* + * Rotates the color values of one side by 90 degrees clockwise. + * @param side The side to rotate. + */ + private void rotateSideClockwise(int side) throws Exception { + checkValidSide(side); // side corners - RubiksColor row0col0 = setValue(side,0,0, cube[side][2][0]); - RubiksColor row0col2 = setValue(side,0,2, row0col0); - RubiksColor row2col2 = setValue(side,2,2, row0col2); - RubiksColor row2col0 = setValue(side,2,0, row2col2); + String row0col0 = setColor(side, 0, 0, cube[side][2][0]); + String row0col2 = setColor(side, 0, 2, row0col0); + String row2col2 = setColor(side, 2, 2, row0col2); + String row2col0 = setColor(side, 2, 0, row2col2); // side edges - RubiksColor row0col1 = setValue(side,0,1, cube[side][1][0]); - RubiksColor row1col2 = setValue(side,1,2, row0col1); - RubiksColor row2col1 = setValue(side,2,1, row1col2); - RubiksColor row1col0 = setValue(side,1,0, row2col1); + String row0col1 = setColor(side, 0, 1, cube[side][1][0]); + String row1col2 = setColor(side, 1, 2, row0col1); + String row2col1 = setColor(side, 2, 1, row1col2); + String row1col0 = setColor(side, 1, 0, row2col1); } - private void rotateSide90CounterClockwise(int side) { - if(!isValidSide(side)) return; + /* + * Rotates the color values of one side by 90 degrees counter clockwise. + * @param side The side to rotate. + */ + private void rotateSideCounterClockwise(int side) throws Exception { + checkValidSide(side); // side corners - RubiksColor row0col0 = setValue(side,0,0, cube[side][0][2]); - RubiksColor row2col0 = setValue(side,2,0, row0col0); - RubiksColor row2col2 = setValue(side,2,2, row2col0); - RubiksColor row0col2 = setValue(side,0,2, row2col2); + String row0col0 = setColor(side, 0, 0, cube[side][0][2]); + String row2col0 = setColor(side, 2, 0, row0col0); + String row2col2 = setColor(side, 2, 2, row2col0); + String row0col2 = setColor(side, 0, 2, row2col2); // side edges - RubiksColor row0col1 = setValue(side,0,1, cube[side][1][2]); - RubiksColor row1col0 = setValue(side,1,0, row0col1); - RubiksColor row2col1 = setValue(side,2,1, row1col0); - RubiksColor row1col2 = setValue(side,1,2, row2col1); + String row0col1 = setColor(side, 0, 1, cube[side][1][2]); + String row1col0 = setColor(side, 1, 0, row0col1); + String row2col1 = setColor(side, 2, 1, row1col0); + String row1col2 = setColor(side, 1, 2, row2col1); } - private void rotateSide180(int side) { - if(!isValidSide(side)) return; + /* + * Rotates the color values of one side by 180 degrees. + * @param side The side to rotate. + */ + private void rotateSide180(int side) throws Exception { + checkValidSide(side); + // side corners - switchValues(side, 0, 0, side, 2, 2); - switchValues (side,2,0,side,0,2); + switchColors(side, 0, 0, side, 2, 2); + switchColors(side, 2, 0, side, 0, 2); // side edges - switchValues (side,0,1,side,2,1); - switchValues (side,1,0,side,1,2); + switchColors(side, 0, 1, side, 2, 1); + switchColors(side, 1, 0, side, 1, 2); } - // Sets the value with a another and returns the old value. - private RubiksColor setValue(int i, int j, int k, RubiksColor value) { - RubiksColor oldValue = cube[i][j][k]; - cube[i][j][k] = value; + /* + * Sets the color in a side-row-col coordinate of the cube with a new color. + * @param side The side of the cube. + * @param row The row of the cube. + * @param col The column of the cube. + * @param color The new color for the side-row-col. + */ + private String setColor(int side, int row, int col, String color) { + String oldValue = cube[side][row][col]; + cube[side][row][col] = color; return oldValue; } - // Switches the values of one side-row-column coordinate with another. - private void switchValues(int _i, int _j, int _k, int i, int j, int k) { - RubiksColor oldValue = cube[_i][_j][_k]; - cube[_i][_j][_k] = cube[i][j][k]; - cube[i][j][k] = oldValue; + /* + * Switches the values of one side-row-column coordinate with another. + */ + private void switchColors( + int _side, int _row, int _col, + int side, int row, int col) { + String oldValue = cube[_side][_row][_col]; + cube[_side][_row][_col] = cube[side][row][col]; + cube[side][row][col] = oldValue; + } + + /* + * Assures the parameter is in range of the amount of sides the cube has. + * @param side The number to be checked. + */ + private void checkValidSide(int side) throws Exception { + if(side >= CUBE_SIDES || side < 0) + throw new Exception(); + } + + /* + * Checks if piece is in range 0 to 1, only outer pieces can be rotated. + * @param piece The piece number. + */ + private void checkValidPiece(int piece) throws Exception { + if(piece > 1 || piece < 0) + throw new Exception(); } - private boolean isValidSide(int side) { - return side < CUBE_SIDES && side > -1; + /* + * Since middle piece cannot be moved, the piece value has to be change + * when necessary so it can fit the cube 3-d array. + * @param piece The piece number. + * @return The correct piece value. + */ + private int fixPieceValue(int piece) { + if(piece==1) return 2; else return 0; } - private boolean isValidPiece(int piece) { - return piece < CUBE_SIZE && piece > -1; + /* + * Since cube display is a flatten cube, some rotations require the + * piece value to switch. + * @param piece The piece to switch values. + * @return The opposite piece value. + */ + private int switchPiece(int piece) { + if(piece==2) return 0; else return 2; } } diff --git a/src/RubiksRotation.java b/src/RubiksRotation.java @@ -4,12 +4,15 @@ import java.util.*; * Different possible rotation directions in Rubik's cube. */ public enum RubiksRotation { - VERTICAL_CLOCKWISE_90, - VERTICAL_COUNTERCLOCKWISE_90, - VERTICAL_180_DEGREES, - HORIZONTAL_CLOCKWISE_90, - HORIZONTAL_COUNTERCLOCKWISE_90, - HORIZONTAL_180_DEGREES; + VERTICAL_CLOCKWISE, + VERTICAL_COUNTERCLOCKWISE, + VERTICAL_180, + SIDE_VERTICAL_CLOCKWISE, + SIDE_VERTICAL_COUNTERCLOCKWISE, + SIDE_VERTICAL_180, + HORIZONTAL_CLOCKWISE, + HORIZONTAL_COUNTERCLOCKWISE, + HORIZONTAL_180; private static final List<RubiksRotation> VALUES = Collections.unmodifiableList(Arrays.asList(values())); diff --git a/src/RubiksTest.java b/src/RubiksTest.java @@ -0,0 +1,45 @@ +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +class RubiksTest +{ + public static void main(String[] args) + { + /*RubiksCube cube = new RubiksCube(true); + String[][] export = cube.getState(); + String corners = export[0][0]; + String halfEdges = export[1][0]; + //String half2Edges = export[2][0]; + System.out.println(corners); + System.out.println(halfEdges); + //System.out.println(half2Edges); + + PatternTable cornersTable = new PatternTable("corners.txt"); + cornersTable.updateTable(corners, "1"); + cornersTable.updateTable(halfEdges, "2"); */ + //PatternTable cornersTable = new PatternTable("corners.txt"); + //System.out.println(cornersTable.valueOf("WYWWRORBRBYY")); + //cornersTable.clearTable(); + + RubiksCube cube = new RubiksCube(); + //cube.rotate(0, RubiksRotation.VERTICAL_CLOCKWISE); //CORRECT 1 + //cube.rotate(1, RubiksRotation.VERTICAL_CLOCKWISE); //CORRECT 2 + //cube.rotate(0, RubiksRotation.VERTICAL_COUNTERCLOCKWISE); //CORRECT 3 + //cube.rotate(1, RubiksRotation.VERTICAL_COUNTERCLOCKWISE); //CORRECT 4 + //cube.rotate(0, RubiksRotation.VERTICAL_180); //CORRECT 5 + //cube.rotate(1, RubiksRotation.VERTICAL_180); //CORRECT 6 + //cube.rotate(0, RubiksRotation.HORIZONTAL_CLOCKWISE); //CORRECT 7 + //cube.rotate(1, RubiksRotation.HORIZONTAL_CLOCKWISE); //CORRECT 8 + //cube.rotate(0, RubiksRotation.HORIZONTAL_COUNTERCLOCKWISE); //CORRECT 9 + //cube.rotate(1, RubiksRotation.HORIZONTAL_COUNTERCLOCKWISE); //CORRECT 10 + //cube.rotate(0, RubiksRotation.HORIZONTAL_180); //CORRECT 11 + //cube.rotate(1, RubiksRotation.HORIZONTAL_180); //CORRECT 12 + //cube.rotate(0, RubiksRotation.SIDE_VERTICAL_CLOCKWISE); //CORRECT 13 + //cube.rotate(1, RubiksRotation.SIDE_VERTICAL_CLOCKWISE); //CORRECT 14 + //cube.rotate(0, RubiksRotation.SIDE_VERTICAL_COUNTERCLOCKWISE); //CORRECT 15 + //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(); + } +} +\ No newline at end of file diff --git a/src/ThreeDMatrix.java b/src/ThreeDMatrix.java @@ -1,27 +0,0 @@ -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; - -class ThreeDMatrix -{ - public static void main(String[] args) - { - /*RubiksCube cube = new RubiksCube(true); - String[][] export = cube.getState(); - String corners = export[0][0]; - String halfEdges = export[1][0]; - //String half2Edges = export[2][0]; - System.out.println(corners); - System.out.println(halfEdges); - //System.out.println(half2Edges); - - PatternTable cornersTable = new PatternTable("corners.txt"); - cornersTable.updateTable(corners, "1"); - cornersTable.updateTable(halfEdges, "2"); */ - //PatternTable cornersTable = new PatternTable("corners.txt"); - //System.out.println(cornersTable.valueOf("WYWWRORBRBYY")); - //cornersTable.clearTable(); - - - - } -} -\ No newline at end of file