// // This is a small russian game "Perevertishi" // import java.awt.*; import java.applet.*; import java.awt.event.*; // // class Rotations // public class Rotations extends Applet implements MouseListener, KeyListener { Label label1 = new Label(); Color colors[] = new Color[6]; // colors of cube sides private int free = 4; // position of free place int attitude[]={0,0,0,0,0,0,0,0,0}; // attitudes of cubes int history[] = new int[16000]; // commands history int hi; // history index int hi_max; // length of commands history // cubes attitude information int cube_info[] = { 0,1,2, 5,20, 7,21, 3,1,5, 6,21, 4,20, 3,4,2, 7,23, 5,22, 0,4,5, 4,22, 6,23, 1,0,5, 1, 9, 3, 8, 4,0,2, 2, 8, 0, 9, 4,3,5, 3,10, 1,11, 1,3,2, 0,11, 2,10, 2,0,1, 13, 4,15, 5, 5,0,4, 14, 5,12, 4, 5,3,1, 15, 7,13, 6, 2,3,4, 12, 6,14, 7, 0,2,4, 9,17,11,16, 3,2,1, 10,16, 8,17, 3,5,4, 11,18, 9,19, 0,5,1, 8,19,10,18, 1,2,0, 21,12,23,13, 4,2,3, 22,13,20,12, 4,5,0, 23,15,21,14, 1,5,3, 20,14,22,15, 2,1,3, 17, 1,19, 0, 5,1,0, 18, 0,16, 1, 5,4,3, 19, 2,17, 3, 2,4,0, 16, 3,18, 2 }; // The entry point for the applet. public void init() { // this.setLayout(new FlowLayout()); this.setBackground(Color.white); label1.setText("0 "); this.add(label1); for(int i=0; i<6; i++) colors[i] = Color.getHSBColor(i/6.0f, 0.99f, 0.8f); addMouseListener(this); addKeyListener(this); } private final int size = 60; // total size of cube place private final int border = 4; // width of cube border private final int gap = 2; // gap between cubes private final int center = size-border*2-gap; // width of central cube side private final int field = size*3+gap+1; // width of game field private final int origin = 40; // public void start(){label1.setLocation(gap, field+50);} public void paint(Graphics g) { g.setPaintMode(); g.setColor(Color.lightGray); g.drawRect(origin,origin,field,field); for(int pos=0; pos<9; pos++) { int x = origin+gap+border+1 + size*(pos%3); int y = origin+gap+border+1 + size*(pos/3); if(pos==free){ g.setColor(Color.white); g.fillRect(x-border, y-border, size, size); continue; } int i = attitude[pos]*7; int c; g.setColor(colors[cube_info[i++]]); g.fillRect(x, y, center, center); g.setColor(colors[c=cube_info[i++]]); g.fillRect(x, y-border, center, border); g.setColor(colors[(c+3)%6]); g.fillRect(x, y+center, center, border); g.setColor(colors[c=cube_info[i]]); g.fillRect(x-border, y, border, center); g.setColor(colors[(c+3)%6]); g.fillRect(x+center, y, border, center); } } public void mouseReleased(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mouseClicked(MouseEvent e){} public void mousePressed(MouseEvent e){ int x=(e.getX()-origin-gap)/size; int y=(e.getY()-origin-gap)/size; if(0<=x && x<=2 && 0<=y && y<=2) { int pos = y*3+x; int dir; switch(free-pos){ case -3: dir=0; break; case 1: dir=1; break; case 3: dir=2; break; case -1: dir=3; break; default: return; } history[hi++] = dir; hi_max = hi; move(dir); } } private final void move(int dir) { final int arr[] = {-3,1,3,-1}; int next = (free-arr[dir]); if(next<0 || next>8 || ((dir&1)!=0 && (free/3!=next/3) )){ hi_max = --hi; return; } attitude[free] = cube_info[attitude[next]*7+3+dir]; free = next; label1.setText(""+hi); // repaint(); paint(getGraphics()); } public void keyReleased(KeyEvent e){} public void keyTyped(KeyEvent e){} public void keyPressed(KeyEvent e) { int dir; switch(e.getKeyCode()) { case e.VK_R: case e.VK_Z: // Do Redo operation on keys "R", "Z", "Shift-Ctrl-Z" and other if( e.isShiftDown() || !e.isControlDown()) { if(hi0) move( (history[--hi]+2)%4 ); return; case e.VK_UP: dir=0; break; case e.VK_RIGHT: dir=1; break; case e.VK_DOWN: dir=2; break; case e.VK_LEFT: dir=3; break; default: return; } history[hi++] = dir; hi_max = hi; move(dir); } }