// version of maze solver template with a goblin // needs man15.gif and gremlin1.gif import java.awt.*; import java.awt.event.*; import java.awt.Graphics; import javax.swing.*; public class mazegob extends JFrame implements ActionListener { /* default values: */ public static int bh = 40; // height of a graphical block public static int bw = 40; // width of a graphical block public static int mh = 31; // height and width of maze public static int mw = 31; private int ah, aw; // height and width of graphical maze private int yoff = 40; // init y-cord of maze private Graphics g; private int dtime = 40; // 40 ms delay time private Image goblingif, mangif; // animated gif images public static byte[][] M; // the array for the maze private Button startbutton; public static final int SOUTH = 0; public static final int EAST = 1; public static final int NORTH = 2; public static final int WEST = 3; public goblin gob1; // args determine block size, maze height, and maze width public mazegob(int bh0, int mh0, int mw0) { bh = bw = bh0; mh = mh0; mw = mw0; ah = bh*mh; aw = bw*mw; startbutton = new Button("Begin"); startbutton.setBounds((aw/2)-40,yoff+20,80,30); Container pane = this.getContentPane(); // the "content pane" of window pane.setLayout(null); // else java will place items automatically pane.add(startbutton); startbutton.addActionListener( this ); M = new byte[mh][mw]; // initialize maze (all 0's - walls). this.setBounds(0,0,aw+10,10+ah+yoff); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); try{Thread.sleep(500);} catch(Exception e) {} // Synch with system g = getGraphics(); //g.setColor(Color.red); goblingif = Toolkit.getDefaultToolkit().getImage("gremlin1.gif"); prepareImage(goblingif,this); mangif = Toolkit.getDefaultToolkit().getImage("man15.gif"); prepareImage(mangif,this); try {Thread.sleep(500);} catch (Exception se) {} } // constructor public void paint(Graphics g) {} // override automatic repaint // distance function public static double dist(int x1, int y1, int x2, int y2) { int dx = x1-x2; int dy = y1-y2; return Math.sqrt(dx*dx+dy*dy); } public void actionPerformed( ActionEvent e ) { startbutton.setVisible(false); g.setColor(Color.green); g.fill3DRect(0,yoff,aw,ah,true); // fill raised rectangle g.setColor(Color.black); // showStatus("Generating maze..."); digout(mh-2,mw-2); // start digging! // digout exit M[mh-1][mw-2] = 1; drawblock(mh-2,mw-1); int gx=0, gy=0; do { gx=(int)(Math.random()*mw); gy=(int)(Math.random()*mh); } while (M[gy][gx]==0 || dist(gx,gy,1,1)<8); gob1 = new goblin(gy,gx); g.drawImage(goblingif,gob1.x*bw-4,gob1.y*bh+yoff-4,bw+8,bh+8,null); solve(); // showStatus("Maze Complete!"); } public static void main(String[] args) { int blocksize = 36, mheight = 21, mwidth = 27; // need to be odd if (args.length==3) { mheight=Integer.parseInt(args[0]); mwidth=Integer.parseInt(args[1]); blocksize=Integer.parseInt(args[2]); } mazegob W = new mazegob(blocksize,mheight,mwidth); } /*********************** Lab 9 *******************************/ public void drawblock(int y, int x) { g.setColor(Color.black); g.fillRect(x*bw,yoff+(y*bh),bw,bh); g.setColor(Color.yellow); // following line displays value of M[y][x] in the graphical maze: // g.drawString(""+M[y][x],(x*bw)+(bw/2-4),yoff+(y*bh)+(bh/2+6)); } public void drawdot(int y, int x) { // g.fillOval(x*bw,yoff+(y*bh),bw,bh); g.drawImage(mangif,x*bw,yoff+(y*bh),bw,bh,null); // draw goblin // erase old goblin if (M[gob1.y][gob1.x]==0) g.setColor(Color.green); else g.setColor(Color.black); g.fillRect(gob1.x*bw,yoff+(gob1.y*bh),bw,bh); // g.drawString(""+M[y][x],(x*bw)+(bw/2-4),yoff+(y*bh)+(bh/2+6)); gob1.move(); // randomly move to new location g.drawImage(goblingif,gob1.x*bw-4,gob1.y*bh+yoff-4,bw+8,bh+8,null); int dx = gob1.x -x; int dy = gob1.y-y; if (Math.sqrt(dx*dx+dy*dy) <= 1) // caught the human! { g.setColor(Color.red); g.setFont(new Font("Serif",Font.BOLD,24)); g.drawString("Got you!",(aw/2)-60,60); try {Thread.sleep(1000*60*60*24);} catch(Exception le) {} } try{Thread.sleep(250);} catch(Exception e) {} } /* *********** Paste your "digout" function here: ************* */ public void digout(int y, int x) {} public void solve() { } // solve } // mazeg class class stackcell { int x; int y; stackcell tail; public stackcell(int a, int b, stackcell t) {y=a; x=b; tail=t;} public void cut() { stackcell cutcell = null; stackcell i = this; while (i!=null) { if (i.x==x && i.y==y) cutcell=i; i=i.tail; } if (cutcell!=null) this.tail = cutcell.tail; } // cut } // stack class class goblin { int mw = mazegob.mw; int mh = mazegob.mh; int bh = mazegob.bh; int bw = mazegob.bw; boolean walkthroughwalls = false; // change this to make it stay on path int yoff = 40; int x, y; public goblin(int a, int b) {y=a; x=b;} public void move() { int dx = -2; int dy = -2; do { dx = (int)(Math.random()*3) - 1; dy = (int)(Math.random()*3) - 1; } while (!(x+dx>0 && x+dx0 && y+dy