import java.applet.Applet; import java.awt.*; import java.util.Random; import java.lang.Runnable; public class Game extends Applet { boolean loopFlag [] = {false, false, false, false}; Graphics g; // Image offScrImage = createImage (size().width, size().height); //Image(size().width, size().height); int temp; int grid ; //how many pieces in the grid int square ; //how big is each one int score=0; //score this move int [] player = new int [2]; //player scores int whosegoisit=0; int length=0; //the thing which makes you get more points the longer your loop is int moves=0; //moves so far this game int [][] gateX = new int[4][2]; int [][] gateY = new int[4][2]; Piece [][] theGrid = new Piece [64][64]; int xpiece, ypiece; int downX, downY; Graphics tempg; Image tempImage; boolean firstime = true; //*********************************************************** // INIT INIT INIT INIT INIT INIT INIT INIT INIT INIT public void init() { int tmp; Random rnums = new Random(); g = getGraphics(); tempImage=createImage (size().width, size().height); tempg = tempImage.getGraphics(); // offScrg = offScrImage.getGraphics(); grid = Integer.parseInt(getParameter("squares")); square = Integer.parseInt(getParameter("size")); // Define shape of pieces // down-to-the-right polygon gateX[0][0] = square/2-square/6; gateX[1][0] = square ; gateX[2][0] = square/2+square/6; gateX[3][0] = 1; gateY[0][0] = 1; gateY[1][0] = square/2-square/6; ; gateY[2][0] = square; gateY[3][0] = square/2+square/6; // up-to-the-left polygon gateX[0][1] = square/2+square/6; gateX[1][1] = square ; gateX[2][1] = square/2-square/6; gateX[3][1] = 1; gateY[0][1] = 1; gateY[1][1] = square/2+square/6; gateY[2][1] = square; gateY[3][1] = square/2-square/6; // set starting colours for (int i=0; ik) { drawConnect(tempg,i,j,k, i, j, k); } } } } /***********************************************/ void checkBoard () { if (firstime) { for (int i=0; i1) whosegoisit=0; //now there should be some way of changing the cursur colour... // draw current score tempg.setColor (Color.blue); tempg.fillRect(grid*square, 0, 200, 35); tempg.setColor (Color.black); tempg.fillRect(grid*square, 35, 200, 35); tempg.setColor (Color.red); tempg.fillRect(grid*square, 70, 200, 35); tempg.setFont (new Font("TimesRoman", Font.BOLD, 16)); tempg.setColor (Color.black); tempg.drawString(String.valueOf(player[0]), grid*square+2, 20); if (whosegoisit==0) tempg.setColor (Color.blue); else tempg.setColor (Color.red); tempg.drawString(String.valueOf(moves), grid*square+10, 55); tempg.setColor (Color.black); tempg.drawString(String.valueOf(player[1]), grid*square+2, 90); g.drawImage (tempImage,0,0,null); } //end of paint /****************** nextGate ***************************/ // which returns the address of the gate joining the one it is given. int [] nextGate (int i, int j, int gate) { int [] temp = {-1, -1, -1}; switch (gate) { case 0 : if (j!=0) { temp[0]=i; temp[1]=j-1; temp[2]=2; } break; case 1 : if (i!=grid-1) { temp[0]=i+1; temp[1]=j; temp[2]=3; } break; case 2 : if (j!=grid-1) { temp[0]=i; temp[1]=j+1; temp[2]=0; } break; case 3 : if (i!=0) { temp[0]=i-1; temp[1]=j; temp[2]=1; } break; default : break; } return temp; } /********************* drawStep *****************************/ // draw an actual line void drawStep(Graphics graf, int i, int j, int k) { int red, green, blue; int i1 = square*i; int j1 = square*j; if (theGrid[i][j].conn[k]!=-1) { int [] xarray = {i1+gateX[k][0], i1+gateX[k][1], i1+gateX[theGrid[i][j].conn[k]][0], i1+gateX[theGrid[i][j].conn[k]][1]}; int [] yarray = {j1+gateY[k][0], j1+gateY[k][1], j1+gateY[theGrid[i][j].conn[k]][0], j1+gateY[theGrid[i][j].conn[k]][1]}; red= theGrid[i][j].hue[k] /0x10000; green= theGrid[i][j].hue[k] %0x10000 /0x100; blue= theGrid[i][j].hue[k] %0x100; graf.setColor(new Color(red, green, blue)); graf.fillPolygon(xarray, yarray, 4); } }//end drawstep /**************************************************/ boolean Loop (int i, int j, int gate) { boolean lflag = false; if (theGrid[i][j].conn[gate]!=-1) { int [] link = nextGate(i,j,gate); if (link[0]!=-1) // ie there is a next gate lflag = Connect(link[0], link[1], link[2], i, j, gate); } return lflag; //which is the same as cflag in Connect(...) } /*****************************************************/ // The recursive thing which runs around loops. // i,j are the current square, g is the gate we're checking // p, q boolean Connect (int i, int j, int g, int p, int q, int r) { boolean cflag = false; if (((i==p)&&(j==q))&&(theGrid[i][j].conn[g]==r)) cflag=true; // this is the original square. LOOP! else if (((i==p)&&(j==q))&&(g==r)) cflag=true; // this is the original square. LOOP! else { int [] link = nextGate(i,j,theGrid[i][j].conn[g]); if (link[0]!=-1) cflag = Connect(link[0], link[1], link[2], p, q, r); } return cflag; } /****************************************************/ void drawLoop (Graphics g, int gate) { int i = xpiece; int j = ypiece; int tmp = 3; if (theGrid[i][j].conn[gate]!=-1) { drawStep(g, i, j, gate); int [] link = nextGate(i, j, gate); if (link[0] != -1) colourPath(link[0], link[1], link[2],theGrid[i][j].hue[gate], i, j, gate,0); } // how to find out if this score is changing? // score = score +(tmp*tmp); } /****************************************************/ void eraseLoop (Graphics g, int gate) { int i = xpiece; int j = ypiece; score++; if (theGrid[i][j].conn[gate]!=-1) { theGrid[i][j].conn[theGrid[i][j].conn[gate]]=-1; theGrid[i][j].conn[gate]=-1; theGrid[i][j].roads--; theGrid[i][j].cross=false; int [] link = nextGate(i, j, gate); if (link[0] != -1) // ie there is a link eraseConnect(g, link[0], link[1], link[2], i, j, gate); } } /****************************************************/ void drawConnect(Graphics g, int i, int j, int gate, int p, int q, int r) { // i,j, gate is where we are now; p, q, r is where we started. maybe drawStep(g, i, j, gate); if ((i!=p)||(j!=q)||(theGrid[i][j].conn[gate]!=r)) if ((i!=p)||(j!=q)||(gate!=r)) { int [] link = nextGate(i, j, theGrid[i][j].conn[gate]); if (link[0]!=-1) drawConnect(g, link[0], link[1], link[2], p,q,r); } } /****************************************************/ void eraseConnect(Graphics g, int i, int j, int gate, int p, int q, int r) { int temp; // temp and gate are the two ends of the line temp=theGrid[i][j].conn[gate]; if (temp!=-1) { theGrid[i][j].conn[temp]=-1; theGrid[i][j].conn[gate]=-1; theGrid[i][j].roads--; theGrid[i][j].cross=false; length++; //exponential score increase if (length>4) score=score+((length-3)/2); if ((i!=p)||(j!=q)||(temp!=r)) if ((i!=p)||(j!=q)||(gate!=r)) { int [] link = nextGate(i, j, temp); if (link[0]!=-1) eraseConnect(g, link[0], link[1], link[2], p,q,r); } } } /***********************************************************/ //random colours int rgbness() {//random colours float hue = (float)(Math.random()); float saturation = (float) (0.9+Math.random()/10.0); float brightness = (float) (0.75+Math.random()/4.0);//(1.0-Math.pow(Math.random(),3.0));// return (Color.HSBtoRGB(hue, saturation, brightness)); } } /*** end Game ***/ /****************************************************/ class Piece extends Game { int roads; boolean cross; int [] conn = new int[4]; int [] hue = {0,0,0,0}; int [] tempConn = new int[4]; int [] tempHue = new int[4]; void rotate (int rotation) { for (int i=0; i<4; i++) { if (conn[((i-rotation)+32)%4] == -1) tempConn[i] = -1; else tempConn[i] = (conn[((i-rotation)+32)%4]+rotation)%4; tempHue[(i+rotation+64)%4]=hue[i]; } for (int i=0; i<4; i++) { conn[i] = tempConn[i]; hue[i] = tempHue[i]; } } //************* Create //sets up a piece void Create(int i, int j, int m, int n, int r, boolean c) { conn[0] = i; conn[1] = j; conn[2] = m; conn[3] = n; cross=c; roads=r; /*for (int gate=0; gate<3; gate++) { if (conn[gate]!=-1) { hue[conn[gate]]=hue[gate]; } } */ } //end create } //end Piece