//Instant Insanity Puzzle Applet by Mark Mammel import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class Insanity extends Applet implements ActionListener, MouseListener { int NCUBES = 4; //(NCOLORS = NCUBES) int SQRSIZE = 20; int RMARG = 20; int TMARG = 60; int[][] board = new int [NCUBES][6]; int[][] block = {{0,2,1,3,0,0},{3,0,2,1,0,1},{2,1,0,2,1,3},{1,3,3,0,2,2}}; //ACBDAA DACBAB CBACBD BDDACC initial config from Conway's book //ACBDAB BDCECD CEDAEA DAEBBC EBACDE simple five cube config //{{0,2,1,3,0,1},{1,3,2,4,2,3},{2,4,3,0,4,0},{3,0,4,1,1,2},{4,1,0,2,3,4}}; //five cube int[] boxx = {1,1,1,1,0,2}; //relative coordinates of faces int[] boxy = {0,2,1,3,1,1}; /* |0| faces are numbered this way |4|2|5| |1| |3| */ int[][] rot = {{2,5,3,4},{2,4,3,5},{0,4,1,5},{0,5,1,4},{0,3,1,2},{0,2,1,3}}; //rotation of 4 faces clockwise around each face boolean donefl; Button buttonA = new Button("Scramble"); Button buttonB = new Button("Reset"); Button buttonC = new Button("Answer"); public void init() { donefl=false; buttonA.addActionListener(this); buttonB.addActionListener(this); buttonC.addActionListener(this); add(buttonA); add(buttonB); add(buttonC); addMouseListener(this); reset(); scramble(); } //init() public void start() { } public void stop() { } public void actionPerformed(ActionEvent e) { if (e.getSource() == buttonA) { scramble(); } if (e.getSource() == buttonB) { reset(); } if (e.getSource() == buttonC) { solve(); } repaint(); } //action public void mouseClicked(MouseEvent e) { int x,y,x1,y1,i,j,k; //x=RMARG + i*SQRSIZE*4 + boxx[j]*SQRSIZE; //y=TMARG + boxy[j]*SQRSIZE; x=e.getX()-RMARG; y=e.getY()-TMARG; i=x/(4*SQRSIZE); x1=(x-i*SQRSIZE*4)/SQRSIZE; y1=y/SQRSIZE; j=-1; for (k=0; k<6; k++) if (boxx[k]==x1 && boxy[k]==y1) j=k; if (i-1) { rotate(i,j); donefl=isDone(); repaint(); } } //mouseclicked public void mousePressed(MouseEvent e) { } //required but not used public void mouseReleased(MouseEvent e) { } //required but not used public void mouseEntered(MouseEvent e) { } //required but not used public void mouseExited(MouseEvent e) { } //required but not used public void paint(Graphics g) { int i,j,x,y; for (i=0; i2) overfl=true; else //edge already used ? okfl=(lvl 2+lvl/NCUBES*2) okfl=false; if (++vert[board[cube][edge[lvl]*2+1]] > 2+lvl/NCUBES*2) okfl=false; if (lvl%NCUBES==NCUBES-1 && okfl==true) //check that each vertex is order 2 for (i=0; i-1) { cube=lvl%NCUBES; --vert[board[cube][edge[lvl]*2]]; --vert[board[cube][edge[lvl]*2+1]]; } lvl--; newfl=false; } } while (lvl>-2); if (edge[0]==3) ; //printf("No solutions.\n"); else //print solution { for (i=0; i<2; i++) //two subgraphs { for (j=0; j