import java.applet.*; import java.awt.*; import java.awt.image.*; import java.util.*; public class trap extends Applet { Color grey = new Color(200, 200, 200); Color dGray = new Color(90, 90, 90); Color mGray = new Color(145, 145, 145); Color lGray = new Color(210, 210, 210); boolean rFlag=false; // means there is a red box drawn boolean moving=false; // player is in the middle of a move boolean gameOn=true; // game is being played Date d = new Date(); Random r = new Random(d.getTime()); Font sysFont = new Font("Helvetica", Font.BOLD, 14); Font littleFont = new Font("Helvetica", Font.BOLD, 9); Font bigFont = new Font("Helvetica", Font.BOLD, 24); Font turnFont = new Font("Courier",Font.BOLD,36); int turn; int inX,xdelta,inY,ydelta; // used in the redbox int zx,zy; int startX=0; int startY=0; int width=224; int length=224; boolean[][] circle= new boolean[4][4]; // tells you if there is a circle on the square boolean[][] moogoo= new boolean[4][4]; // tells you if it's a moogoo boolean[][] legal = new boolean[4][4]; // tells you if it's a legal move int currentPlayer=0; int currentMoogoo=0; int[] mgLoc = new int[2]; int[] legalLoc = new int[4]; Image board,redchip,bluechip,goldchip,solved; Image buffer=null; Image[][] cell = new Image[4][4]; // the blue squares int wid,het; AudioClip beep,beep2,beepl,uwin1,uwin2,startup,restart; MediaTracker t = new MediaTracker(this); public void init() { setBackground(Color.black); int[] pixel=new int[400]; beep = getAudioClip(getDocumentBase(),"audio/beep.au"); beep2 = getAudioClip(getDocumentBase(),"audio/2beep.au"); beepl = getAudioClip(getDocumentBase(),"audio/beepl.au"); restart = getAudioClip(getDocumentBase(),"audio/restart.au"); startup = getAudioClip(getDocumentBase(),"audio/startup.au"); uwin1 = getAudioClip(getDocumentBase(),"audio/uwin1.au"); uwin2 = getAudioClip(getDocumentBase(),"audio/uwin2.au"); try { board = getImage(getDocumentBase(),"board.jpg"); t.addImage(board,0); solved = getImage(getDocumentBase(),"solved.gif"); t.addImage(solved,0); redchip = getImage(getDocumentBase(),"red.gif"); t.addImage(redchip,0); goldchip = getImage(getDocumentBase(),"gold.gif"); t.addImage(goldchip,0); t.waitForID(0); CropImageFilter f; // this is just an object that holds the size of your crop FilteredImageSource fis; // this is a container for the result for(int x=0;x<4;x++) { for(int y=0;y<4;y++) { // CropImageFilter(position X, position Y, width, height) f=new CropImageFilter(20+(x*55),20+(y*55),20,20); // set the size to the middle of one of the squares fis=new FilteredImageSource(board.getSource(),f); // get the piece cell[x][y]=createImage(fis); // create an image from the piece t.addImage(cell[x][y],0); PixelGrabber pg=new PixelGrabber(cell[x][y],0,0,20,20,pixel,0,20); pg.grabPixels(); for(int i=0;i<400;i++) if(((x+y)%2)==0) pixel[i]=addBlue(pixel[i],1); else pixel[i]=addBlue(pixel[i],0); cell[x][y] = createImage(new MemoryImageSource(20, 20, ColorModel.getRGBdefault(), pixel, 0, 20)); } } t.waitForAll(); } catch (InterruptedException e) {} ; wid=314; het=235; buffer = createImage(wid,het); startup.play(); resetGrid(); } // init public int addBlue(int pix, int delta) { // takes a piece of each board square and adds blue to it // used in demarking a legal move. Color c=new Color(pix); int r,g,b; r=c.getRed(); g=c.getGreen(); b=c.getBlue(); if(delta==1) b=200; else b=100; c = new Color(r,g,b); return (c.getRGB()); } public boolean mouseMove(Event e, int x, int y) { int i=0; if(gameOn) { if(!rFlag) { // means there isn't a red rect yet if((mPos(x,startX)<4)&&(mPos(y,startY)<4)){ if(((circle[mPos(x,startX)][mPos(y,startY)])&&(!moogoo[mPos(x,startX)][mPos(y,startY)]))|(legal[mPos(x,startX)][mPos(y,startY)])) { redRect(getGraphics(), mPos(x,startX),mPos(y,startY)); } } // now there's a rect! } // endif !rflag else { if((mPos(x,startX)!=zx)|(mPos(y,startY)!=zy)) { rFlag=false; paint(getGraphics()); } } } // endif !moving return true; } // mouseMove public boolean inGrid(int x, int y) { boolean localFlag=false; if((x > startX)&&(y > startY)&&(y230)&&(x<305)&&(y>198)&&(y<225))|(!gameOn)) { // reset and repaint resetGrid(); restart.play(); paint(getGraphics()); } else { if (inGrid(x,y)&&gameOn) { int col = mPos(x,startX); int row= mPos(y,startY); // now we know that they've clicked in the grid area at [col][row] if(!moving) { // he's clicking on the guy he wants to move if((circle[col][row])&&(!moogoo[col][row])) { beep.play(); moving=true; currentPlayer=col+(row*4); hesMoving(col,row); } } else { // he's clicking in the place he wants to move to. int px=currentPlayer%4; int py=(currentPlayer-px)/4; moving=false; if(legal[col][row]) { beep2.play(); movePlayer(px,py,col,row); checkMoogoos(); if(gameOn) { turn++; moveMoogoos(); redRect(getGraphics(), mPos(x,startX),mPos(y,startY)); clearLegals(); } else { turn++; } } else { beepl.play(); } } paint(getGraphics()); } } return true; } // mouseUp public void redRect(Graphics g, int x, int y) { // registers that a red rect is drawn and where zx=x; zy=y; g.setColor(new Color(175,0,0)); // a darker Red g.drawRect(startX+6+(x*55),startY+6+(y*55),47,47); g.drawRect(startX+7+(x*55),startY+7+(y*55),45,45); rFlag=true; } // redRect public void hesMoving(int x, int y) { clearLegals(); // up if((y-1)>=0) if(!circle[x][y-1]) legal[x][y-1]=true; // down if((y+1)<=3) if(!circle[x][y+1]) legal[x][y+1]=true; // left if((x-1)>=0) if(!circle[x-1][y]) legal[x-1][y]=true; // down if((x+1)<=3) if(!circle[x+1][y]) legal[x+1][y]=true; } public void drawLegals(Graphics g) { for(int i=0;i<4;i++) for(int j=0;j<4;j++) if(legal[i][j]) g.drawImage(cell[i][j],startX+20+(i*55),startY+20+(j*55),this); } public int getRow(int index) { int c=0; index-=4; while(index>=0) { index-=4; c++; } return c; } // getRow public int mPos(int x, int sx) { // returns the column or row of the coordinate sent int pos, i; boolean flag = true; i= x-sx; pos = 0; i-=60; while(flag) { if(i<=0) flag=false; else { pos++; i-=55; } } return pos; } // end mPos public void resetGrid() { int i,j; for(i=0;i<4;i++) { for(j=0;j<4;j++) { circle[i][j]=false; moogoo[i][j]=false; }} circle[0][0]=true; circle[3][0]=true; circle[0][3]=true; circle[3][3]=true; mgLoc[0]=5; circle[1][1]=true; moogoo[1][1]=true; mgLoc[1]=10; circle[2][2]=true; moogoo[2][2]=true; currentPlayer=0; gameOn=true; clearLegals(); turn=0; rFlag=false; } // resetGrid public void checkMoogoos() { // check to see if moogoos can move int i,j; boolean k=true; boolean l=true; hesMoving(mgLoc[0]%4,(mgLoc[0]-(mgLoc[0]%4))/4); for(i=0;i<4;i++) for(j=0;j<4;j++) if(legal[i][j]) k=false; hesMoving(mgLoc[1]%4,(mgLoc[1]-(mgLoc[1]%4))/4); for(i=0;i<4;i++) for(j=0;j<4;j++) if(legal[i][j]) l=false; if(k&&l) gameOn=false; } public void moveMoogoos() { // move a moogoo, to a middle square preferably int myMove=-1; int xMove,yMove,index; int chance=getNum(10); if(chance<5) index=0; else index=1; // move to center if possible xMove=mgLoc[index]%4; yMove=(mgLoc[index]-(mgLoc[index]%4))/4; hesMoving(xMove,yMove); if(numLegals()>0) for(int x=1;x<3;x++) for(int y=1;y<3;y++) if(legal[x][y]) myMove=y*4+x; if(myMove==-1) { if(index==0) index=1; else index=0; xMove=mgLoc[index]%4; yMove=(mgLoc[index]-(mgLoc[index]%4))/4; hesMoving(xMove,yMove); if(numLegals()>0) for(int x=1;x<3;x++) for(int y=1;y<3;y++) if(legal[x][y]) myMove=y*4+x; } if(myMove==-1) { if(index==0) index=1; else index=0; if((mgLoc[index]<5)|(mgLoc[index]>10)|(mgLoc[index]==8)|(mgLoc[index]==7)) { xMove=mgLoc[index]%4; yMove=(mgLoc[index]-(mgLoc[index]%4))/4; hesMoving(xMove,yMove); if(numLegals()>0) for(int x=0;x<4;x++) for(int y=0;y<4;y++) if(legal[x][y]) myMove=y*4+x; } } if(myMove==-1) { if(index==0) index=1; else index=0; if((mgLoc[index]<5)|(mgLoc[index]>10)|(mgLoc[index]==8)|(mgLoc[index]==7)) { xMove=mgLoc[index]%4; yMove=(mgLoc[index]-(mgLoc[index]%4))/4; hesMoving(xMove,yMove); if(numLegals()>0) for(int x=0;x<4;x++) for(int y=0;y<4;y++) if(legal[x][y]) myMove=y*4+x; } } if(myMove==-1) { if(index==0) index=1; else index=0; xMove=mgLoc[index]%4; yMove=(mgLoc[index]-(mgLoc[index]%4))/4; hesMoving(xMove,yMove); if(numLegals()>0) for(int x=0;x<4;x++) for(int y=0;y<4;y++) if(legal[x][y]) myMove=y*4+x; } if(myMove==-1) { if(index==0) index=1; else index=0; xMove=mgLoc[index]%4; yMove=(mgLoc[index]-(mgLoc[index]%4))/4; hesMoving(xMove,yMove); if(numLegals()>0) for(int x=0;x<4;x++) for(int y=0;y<4;y++) if(legal[x][y]) myMove=y*4+x; } movePlayer(xMove,yMove,myMove%4,(myMove-(myMove%4))/4); mgLoc[index]=myMove; } // moveMoogoo public int numLegals() { // returns number of legal moves available int k=0; for(int x=0;x<4;x++) for(int y=0;y<4;y++) if(legal[x][y]) k++; return k; } public void movePlayer(int x1, int y1, int x2, int y2) { // move a chip, player or moogoo circle[x1][y1]=false; // set up new spot circle[x2][y2]=true; if(moogoo[x1][y1]) { moogoo[x1][y1]=false; moogoo[x2][y2]=true; } } // movePlayer public void drawCircles(Graphics g) { int i,j; for(i=0;i<4;i++) { for(j=0;j<4;j++) { if(circle[i][j]) { if(moogoo[i][j]) g.drawImage(redchip,startX+11+(i*55),startY+11+(j*55),this); else g.drawImage(goldchip,startX+11+(i*55),startY+11+(j*55),this); } } } } // drawCircles public int getNum(int limit) { // returns a random number from 0 to limit-1 int z = r.nextInt() % limit; z = Math.abs(z); return z; } // GETNUM public void youWin() { int i = getNum(2); if(i==0) uwin1.play(); else uwin2.play(); } // youWin public void update(Graphics g) { paint(g); } // update public void paint(Graphics g) { // buffered graphics Graphics off; // temporary holding variable for the current screen off=g; // we make off equal to g so we can mess with g and not worry about losing our link to the current screen g=buffer.getGraphics(); // now when we write to g, we'll be writing to our buffer // this all writes to buffer if(gameOn) { g.drawImage(board,0,0,this); // this clears the screen with new board drawCircles(g); // you may have to clear your screen to white if(moving) drawLegals(g); if(rFlag) redRect(g,zx,zy); g.setColor(Color.green); g.setFont(turnFont); g.drawString(turn+" ",243,60); } else { g.setColor(Color.black); g.fillRect(0,0,314,235); g.drawImage(solved,7,65,this); g.setColor(Color.green); g.setFont(sysFont); g.drawString("Click to Restart", 120, 225); youWin(); } off.drawImage(buffer,0,0,null); // writes buffer to screen, woo hoo! } // paint } // end all