import java.awt.Color; import java.awt.event.KeyEvent; class mazedragon extends mazebase { public mazedragon(int bh, int mh, int mw) // don't change constructor { super(bh,mh,mw); } public void digout(int y, int x) // modify this function { // The following is a skeleton program that demonstrates the mechanics // needed for the completion of the program. // We always dig out two spaces at a time: we look two spaces ahead // in the direction we're trying to dig out, and if that space has // not already been dug out, we dig out that space as well as the // intermediate space. This makes sure that there's always a wall // separating adjacent corridors. /* M[y][x] = 1; // digout maze at coordinate y,x drawblock(y,x); // change graphical display to reflect space dug out delay(40); // slows animation // But the following won't work (but will compile) // sample code that tries to digout one space to the left: if (x-1>=0) digout(y,x-1); // sample code that tries to digout TWO space to the right IF it's not // already dug out: if (x+2=0 && nx*bgap=0 && ny*bgap0) for(int i=0;i0) for(int k=0;k Dragon; protected int ddx, ddy; // dragon movement protected CircQueue Miners; protected boolean dragonalive; protected int minersalive; protected java.awt.Image dragongif; int encode(int base, int y, int x) { return (base/65356) + x*256 + y; } // determine if miner's movement is orthogonal to dragon's boolean orthogonal(int dy, int dx, int ddy, int ddx) { if (ddy==0 && ddx==0) return true; return ddy!=dy && ddx!=dx; } public void play() { try { dragongif = java.awt.Toolkit.getDefaultToolkit().getImage("head1.gif"); prepareImage(dragongif,this); Thread.sleep(50); } catch (Exception ee) {dragongif=null;} maxlength=16; minlength=3; Dragon = new CircQueue(maxlength); // set initial position of dragon Dragon.push(encode(0,0,0)); Dragon.push(encode(0,0,1)); Dragon.push(encode(0,0,2)); Dragon.push(encode(0,0,3)); Dragon.push(encode(0,1,3)); // Dragon.push(encode(0,2,3)); // Dragon.push(encode(0,2,4)); // Dragon.push(encode(0,2,5)); dragonalive=true; // miner positions Miners = new CircQueue(maxlength/2); for (int i=0;i0) { /* int coord = Dragon.peek(); int y = coord%256, x = (coord/256)%256; int dy = y-py, dx =x-px; if (Math.abs(dy)!=0 && Math.abs(dx)!=0) { if (Math.random()<.5) { ddy = -1 * (dy/Math.abs(dy)); ddx = 0; } else { ddx = -1 * (dx/Math.abs(dx)); ddy = 0; } } else if (Math.abs(dy)!=0) { ddy = -1 * (dy/Math.abs(dy)); ddx = 0; } else if (Math.abs(dx)!=0) { ddx = -1 * (dx/Math.abs(dx)); ddy = 0; } */ for (Miner m:Miners) { if (m.alive && Math.random()<.2) { int dd = (int)(Math.random()*4); m.dy = DY[dd]; m.dx = DX[dd]; } } moveto(0,0); delay(dtime*2); } }//play - setup only protected void moveto(int dy,int dx) { int coord = Dragon.peek(); int cy = coord%256; int cx = (coord/256)%256; int ny = cy + ddy; int nx = cx + ddx; if (ny>=0 && ny=0 && nx=0 && ny=0 && nx2 && M[ny][nx]<4 && orthogonal(m.dy,m.dx,ddy,ddx)) { // cut of dragon's head dragonalive = false; System.out.printf("dragon dead M[%d][%d]==%d, dy,dx:%d,%d, ddy,ddx:%d,%d\n",ny,nx,M[ny][nx],m.dy,m.dx,ddy,ddx); // trace ddx = 0; ddy = 0; } else if (M[ny][nx]>=4) // cut off tail { int tailsize = Dragon.size()-M[ny][nx]+2; System.out.printf("dragon cut off M[%d][%d]==%d, dy,dx:%d,%d, ddy,ddx:%d,%d\n",ny,nx,M[ny][nx],m.dy,m.dx,ddy,ddx); // trace while (tailsize-->0) { int last = Dragon.dequeue(); int ly = last%256, lx = (last/256)%256; M[ly][lx] = 1; // clear cell } } M[m.y][m.x] = 1; m.y = ny; m.x = nx; // value won't be written to M until redraw }//m.alive and within bounds }//for each miner redraw(); }//moveto protected void redraw() { clear(); int coord = Dragon.getnth(0); int y = coord%256; int x = (coord/256) % 256; // draw dragon head if (dragongif!=null) drawgif(g2,dragongif,y,x); else drawdot(g2,y,x); M[y][x]=2; for (int i=1;i