//============================================================================= // // Rubik's Cube Java Applet // // Version 1.2 -- June 1 1997 // // Copyright 1995, 1996, 1997 Michael Schubart (schubart@best.com) // http://www.best.com/~schubart/ // // The applet plus additional information can be found at // http://www.best.com/~schubart/rc/ // //============================================================================= import java.applet.AudioClip; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Checkbox; import java.awt.Event; import java.awt.Graphics; import java.awt.Image; import java.awt.image.CropImageFilter; import java.awt.image.FilteredImageSource; import java.awt.Label; import java.awt.MediaTracker; import java.awt.Panel; import java.awt.TextField; import java.net.MalformedURLException; import java.net.URL; class RCCubelet //============================================================================= // The RCCubelet class represents one of the 27 cubelets the cube is made of. //============================================================================= { // names for the faces final static int FACE_NONE=-1; // none final static int FACE_XV=0; // orthogonal to x-axis and visible final static int FACE_XH=1; // orthogonal to x-axis and hidden final static int FACE_YV=2; // and so on final static int FACE_YH=3; final static int FACE_ZV=4; final static int FACE_ZH=5; // names for the axis (axises ?) final static int AXIS_X=0; final static int AXIS_Y=1; final static int AXIS_Z=2; // stores the color of each of the 6 faces protected int[] colors=new int[6]; public RCCubelet() //=========================================================================== // Constructor, initializes each of the faces with a different color. //=========================================================================== { for (int i=0;i<6;i++) colors[i]=i; } public int getColor(int face) //=========================================================================== // Can be used to query the color of one face. // return value: the color //=========================================================================== { return colors[face]; } public void rotate(int axis, int direction) //=========================================================================== // Rotate the cubelet. // axis: the axis around which to rotate // direction: the direction in which to rotate //=========================================================================== { int buffer; switch (axis) { case AXIS_X: if (direction==1) { buffer=colors[FACE_YV]; colors[FACE_YV]=colors[FACE_ZH]; colors[FACE_ZH]=colors[FACE_YH]; colors[FACE_YH]=colors[FACE_ZV]; colors[FACE_ZV]=buffer; } if (direction==-1) { buffer=colors[FACE_YV]; colors[FACE_YV]=colors[FACE_ZV]; colors[FACE_ZV]=colors[FACE_YH]; colors[FACE_YH]=colors[FACE_ZH]; colors[FACE_ZH]=buffer; } break; case AXIS_Y: if (direction==1) { buffer=colors[FACE_XV]; colors[FACE_XV]=colors[FACE_ZV]; colors[FACE_ZV]=colors[FACE_XH]; colors[FACE_XH]=colors[FACE_ZH]; colors[FACE_ZH]=buffer; } if (direction==-1) { buffer=colors[FACE_XV]; colors[FACE_XV]=colors[FACE_ZH]; colors[FACE_ZH]=colors[FACE_XH]; colors[FACE_XH]=colors[FACE_ZV]; colors[FACE_ZV]=buffer; } break; case AXIS_Z: if (direction==1) { buffer=colors[FACE_XV]; colors[FACE_XV]=colors[FACE_YH]; colors[FACE_YH]=colors[FACE_XH]; colors[FACE_XH]=colors[FACE_YV]; colors[FACE_YV]=buffer; } if (direction==-1) { buffer=colors[FACE_XV]; colors[FACE_XV]=colors[FACE_YV]; colors[FACE_YV]=colors[FACE_XH]; colors[FACE_XH]=colors[FACE_YH]; colors[FACE_YH]=buffer; } break; } } } class RCCube //============================================================================= // The RCCube class represents the whole cube, which is made of 27 cubelets. //============================================================================= { // the cubelets protected RCCubelet[][][] cubelet=new RCCubelet[3][3][3]; protected int[][][][] moves={ // AXIS_X {{{0,0,0}, {0,0,2}, {0,2,2}, {0,2,0}, {0,0,1}, {0,1,2}, {0,2,1}, {0,1,0}}, {{1,0,0}, {1,0,2}, {1,2,2}, {1,2,0}, {1,0,1}, {1,1,2}, {1,2,1}, {1,1,0}}, {{2,0,0}, {2,0,2}, {2,2,2}, {2,2,0}, {2,0,1}, {2,1,2}, {2,2,1}, {2,1,0}}}, // AXIS_Y {{{0,0,0}, {2,0,0}, {2,0,2}, {0,0,2}, {1,0,0}, {2,0,1}, {1,0,2}, {0,0,1}}, {{0,1,0}, {2,1,0}, {2,1,2}, {0,1,2}, {1,1,0}, {2,1,1}, {1,1,2}, {0,1,1}}, {{0,2,0}, {2,2,0}, {2,2,2}, {0,2,2}, {1,2,0}, {2,2,1}, {1,2,2}, {0,2,1}}}, // AXIS_Z {{{0,0,0}, {0,2,0}, {2,2,0}, {2,0,0}, {0,1,0}, {1,2,0}, {2,1,0}, {1,0,0}}, {{0,0,1}, {0,2,1}, {2,2,1}, {2,0,1}, {0,1,1}, {1,2,1}, {2,1,1}, {1,0,1}}, {{0,0,2}, {0,2,2}, {2,2,2}, {2,0,2}, {0,1,2}, {1,2,2}, {2,1,2}, {1,0,2}}} }; public RCCube() //=========================================================================== // Constructor, creates the cubelets. Since all the cubelets are equal, the // cube is initially in its "solved" state. //=========================================================================== { for (int x=0;x<3;x++) for (int y=0;y<3;y++) for (int z=0;z<3;z++) cubelet[x][y][z]=new RCCubelet(); } public int getColor(int xPos, int yPos, int zPos, int face) //=========================================================================== // Can be used to query the color of one face of one cubelet. // xPos, yPos, zPos: the coordinates of the cubelet // face: the face of that cubelet // return value: the color //=========================================================================== { return cubelet[xPos][yPos][zPos].getColor(face); } public void rotate(int axis, int direction, int slice, boolean wholeCube) //=========================================================================== // Rotates one slice of the cube or the whole cube. // axis: the axis around which to rotate // direction: the direction in which to rotate // slice: the depth of the slice to rotate // wholeCube: true -> the whole cube is to be rotated. ignore "slice" //=========================================================================== { // saves one cubelet when we do circular swaps RCCubelet buffer; int[][] m=moves[axis][slice]; if (wholeCube) { // rotate the whole cube? then rotate each slice, then quit for (int i=0;i<3;i++) rotate(axis,direction,i,false); return; } if (direction==1) { // move corner cubelets buffer=cubelet[m[0][0]][m[0][1]][m[0][2]]; cubelet[m[0][0]][m[0][1]][m[0][2]]=cubelet[m[1][0]][m[1][1]][m[1][2]]; cubelet[m[1][0]][m[1][1]][m[1][2]]=cubelet[m[2][0]][m[2][1]][m[2][2]]; cubelet[m[2][0]][m[2][1]][m[2][2]]=cubelet[m[3][0]][m[3][1]][m[3][2]]; cubelet[m[3][0]][m[3][1]][m[3][2]]=buffer; // move edge cubelets buffer=cubelet[m[4][0]][m[4][1]][m[4][2]]; cubelet[m[4][0]][m[4][1]][m[4][2]]=cubelet[m[5][0]][m[5][1]][m[5][2]]; cubelet[m[5][0]][m[5][1]][m[5][2]]=cubelet[m[6][0]][m[6][1]][m[6][2]]; cubelet[m[6][0]][m[6][1]][m[6][2]]=cubelet[m[7][0]][m[7][1]][m[7][2]]; cubelet[m[7][0]][m[7][1]][m[7][2]]=buffer; } else { // move corner cubelets buffer=cubelet[m[3][0]][m[3][1]][m[3][2]]; cubelet[m[3][0]][m[3][1]][m[3][2]]=cubelet[m[2][0]][m[2][1]][m[2][2]]; cubelet[m[2][0]][m[2][1]][m[2][2]]=cubelet[m[1][0]][m[1][1]][m[1][2]]; cubelet[m[1][0]][m[1][1]][m[1][2]]=cubelet[m[0][0]][m[0][1]][m[0][2]]; cubelet[m[0][0]][m[0][1]][m[0][2]]=buffer; // move edge cubelets buffer=cubelet[m[7][0]][m[7][1]][m[7][2]]; cubelet[m[7][0]][m[7][1]][m[7][2]]=cubelet[m[6][0]][m[6][1]][m[6][2]]; cubelet[m[6][0]][m[6][1]][m[6][2]]=cubelet[m[5][0]][m[5][1]][m[5][2]]; cubelet[m[5][0]][m[5][1]][m[5][2]]=cubelet[m[4][0]][m[4][1]][m[4][2]]; cubelet[m[4][0]][m[4][1]][m[4][2]]=buffer; } // rotate the cubelets that have been moved for (int i=0;i<8;i++) cubelet[m[i][0]][m[i][1]][m[i][2]].rotate(axis,direction); // P.S.: I know, the comments here are a little sparse. I guess you'll // just have to trust me. :-) } public void scramble(int moves) //=========================================================================== // Brings the cube to disorder. This is done by performing a large number of // random moves, because not all of the cube's possible permutations can be // solved. // moves: how many moves //=========================================================================== { int axis; int direction; int slice; for (int i=0;i