java:2by2rubikcube
Differences
This shows you the differences between two versions of the page.
| Previous revision | |||
| — | java:2by2rubikcube [2022/12/02 21:02] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== The Rubik' | ||
| + | |||
| + | |||
| + | {{ : | ||
| + | |||
| + | Because I'm not good at all at solving this puzzle, I've decided to give Java an opportunity to solve it for me. | ||
| + | |||
| + | So, I've made a program to solve the rubik' | ||
| + | |||
| + | {{: | ||
| + | |||
| + | Just uncompress the contents of this zip file and you will find a file called '' | ||
| + | |||
| + | <code bash> | ||
| + | java -jar rubik.jar | ||
| + | </ | ||
| + | |||
| + | And you the program will start. | ||
| + | |||
| + | Remember that the faces of the cube must be read as if you were reading a text: from left to right, topmost to bottom. | ||
| + | |||
| + | And the source code of the project: | ||
| + | |||
| + | {{: | ||
| + | |||
| + | ===== My comments ===== | ||
| + | |||
| + | The data structure I've choose for the cube is in fact a 4x4 matrix of colors: | ||
| + | |||
| + | <code java> | ||
| + | |||
| + | public class Cube22 implements Cube { | ||
| + | |||
| + | private static final int SIDE_SIZE = 2; | ||
| + | private static final String nl = System.getProperty(" | ||
| + | private Color[][][] cube = new Color[SIDE_SIZE+2][SIDE_SIZE+2][SIDE_SIZE+2]; | ||
| + | |||
| + | |||
| + | // default constructor: | ||
| + | // with all the colors correct | ||
| + | public Cube22() | ||
| + | { | ||
| + | /* | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | */ | ||
| + | cube[0][0][0] = Color.NOCOLOR; | ||
| + | cube[0][1][0] = Color.NOCOLOR; | ||
| + | cube[0][2][0] = Color.NOCOLOR; | ||
| + | cube[0][3][0] = Color.NOCOLOR; | ||
| + | /* | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | */ | ||
| + | cube[0][0][1] = Color.NOCOLOR; | ||
| + | cube[0][1][1] = Color.WHITE; | ||
| + | cube[0][2][1] = Color.WHITE; | ||
| + | cube[0][3][1] = Color.NOCOLOR; | ||
| + | /* | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | */ | ||
| + | cube[0][0][2] = Color.NOCOLOR; | ||
| + | cube[0][1][2] = Color.WHITE; | ||
| + | cube[0][2][2] = Color.WHITE; | ||
| + | cube[0][3][2] = Color.NOCOLOR; | ||
| + | /* | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | */ | ||
| + | cube[0][0][3] = Color.NOCOLOR; | ||
| + | cube[0][1][3] = Color.NOCOLOR; | ||
| + | cube[0][2][3] = Color.NOCOLOR; | ||
| + | cube[0][3][3] = Color.NOCOLOR; | ||
| + | | ||
| + | } // Cube22 | ||
| + | |||
| + | @Override | ||
| + | public Color getColor( int x, int y, int z ) | ||
| + | { | ||
| + | return cube[x][y][z]; | ||
| + | } | ||
| + | @Override | ||
| + | public void setColor( int x, int y, int z, Color color ) | ||
| + | { | ||
| + | cube[x][y][z] = color; | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public Cube getCopy() | ||
| + | { | ||
| + | Cube22 result = new Cube22(); | ||
| + | | ||
| + | for( int x = 0; x < 4; x++ ) | ||
| + | for( int y = 0; y < 4; y++ ) | ||
| + | for( int z = 0; z < 4; z++ ) | ||
| + | { | ||
| + | result.setColor(x, | ||
| + | } | ||
| + | | ||
| + | return result; | ||
| + | | ||
| + | } // getCopy | ||
| + | | ||
| + | @Override | ||
| + | public String toString() | ||
| + | { | ||
| + | StringBuilder out = new StringBuilder(); | ||
| + | | ||
| + | out.append( " | ||
| + | out.append( " | ||
| + | out.append( " | ||
| + | out.append( " | ||
| + | out.append( " | ||
| + | out.append( " | ||
| + | out.append( " | ||
| + | out.append( " | ||
| + | out.append( " | ||
| + | out.append( " | ||
| + | out.append( " | ||
| + | out.append( " | ||
| + | out.append( " | ||
| + | | ||
| + | return out.toString(); | ||
| + | |||
| + | } // toString | ||
| + | |||
| + | /** | ||
| + | * Return true if the cube is solved, in other words, all the colors | ||
| + | * are grouped in the same face of the cube. | ||
| + | * | ||
| + | * @return | ||
| + | */ | ||
| + | @Override | ||
| + | public boolean isSolved() | ||
| + | { | ||
| + | boolean facade1; | ||
| + | boolean facade2; | ||
| + | boolean facade3; | ||
| + | boolean facade4; | ||
| + | boolean facade5; | ||
| + | boolean facade6; | ||
| + | | ||
| + | facade1 = cube[1][1][0] == cube[2][1][0] && cube[2][1][0] == cube[1][2][0] && cube[1][2][0] == cube[2][2][0]; | ||
| + | facade2 = cube[3][2][1] == cube[3][1][1] && cube[3][1][1] == cube[3][1][2] && cube[3][1][2] == cube[3][2][2]; | ||
| + | facade3 = cube[1][3][1] == cube[2][3][1] && cube[2][3][1] == cube[1][3][2] && cube[1][3][2] == cube[2][3][2]; | ||
| + | facade4 = cube[1][1][3] == cube[2][1][3] && cube[2][1][3] == cube[1][2][3] && cube[1][2][3] == cube[2][2][3]; | ||
| + | facade5 = cube[0][2][1] == cube[0][1][1] && cube[0][1][1] == cube[0][1][2] && cube[0][1][2] == cube[0][2][2]; | ||
| + | facade6 = cube[1][0][1] == cube[2][0][1] && cube[2][0][1] == cube[1][0][2] && cube[1][0][2] == cube[2][0][2]; | ||
| + | | ||
| + | return facade1 && facade2 && facade3 && facade4 && facade5 && facade6; | ||
| + | } | ||
| + | |||
| + | |||
| + | /** | ||
| + | * Determine if two cubes are equal regardless of their orientation. | ||
| + | * This is important because this, applied to the search algorithm | ||
| + | * prevents to explore branches that in fact are the seame than a | ||
| + | * previous one but with the colors rotated. | ||
| + | * | ||
| + | */ | ||
| + | @Override | ||
| + | public boolean equals( Object comp ) | ||
| + | { | ||
| + | if( this == comp ) | ||
| + | return true; | ||
| + | if( comp instanceof Cube22 ) | ||
| + | { | ||
| + | Cube22 compCube = (Cube22) comp; | ||
| + | return equalsXYZ(compCube); | ||
| + | } | ||
| + | else | ||
| + | return false; | ||
| + | } // equals | ||
| + | |||
| + | |||
| + | | ||
| + | { | ||
| + | boolean equal = true; | ||
| + | for( int x = 0; x < SIDE_SIZE+2 && equal; x++ ) | ||
| + | for( int y = 0; y < SIDE_SIZE+2 && equal; y++ ) | ||
| + | for( int z = 0; z < SIDE_SIZE+2 && equal; z++ ) | ||
| + | equal = equal && (cube[x][y][z] == compCube.getColor(x, | ||
| + | return equal; | ||
| + | } // equals | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | After two days developing (in my spare time) I've realized that it was a wrong decision, because there is a better way to store the cube and allow the rotation operations to be more easy: is to create a '' | ||
| + | |||
| + | An idea of what it would be: | ||
| + | |||
| + | <code java> | ||
| + | class Sticker | ||
| + | { | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | So, the cube is constructer by linking the respective north, south, east ... links between them. But I think it would be needed to keep a link in order to know the orientation of the cube (to know what is '' | ||
| + | |||
