//package Dasearch; // Pathfinder with alligator lurking at the water's edge import java.awt.*; import java.awt.event.*; import java.awt.Graphics; import javax.swing.*; public class alligator extends JFrame { private Image diamondgif, mangif; // animated gif images private Image gatorgif; private Graphics display; private int gap = 24; // size of each square/cell of map private int yoff = 24; private astar PG; int rows, cols; int XDIM, YDIM; // window dimensions int gobx, goby, profx, profy; private Image[] imageof; // image vector for terrain. private Image[] imagechar; //image vector for character based on terrain. // animation buffer Image clip, dbuf; Graphics cg, wg; /* Chance of getting eaten by alligator at water's edge */ public static double gatorchance = 0.1; public void paint(Graphics g) {} // override automatic repaint public alligator(int r, int c) { rows = r; cols = c; PG = new myastar(r,c); XDIM = cols*gap; YDIM = rows*gap; this.setBounds(0,0,XDIM,YDIM+yoff+5); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // display = this.getGraphics(); clip = createImage(XDIM,YDIM+yoff+5); cg = clip.getGraphics(); dbuf = createImage(XDIM,YDIM+yoff+5); display = dbuf.getGraphics(); wg = this.getGraphics(); diamondgif = Toolkit.getDefaultToolkit().getImage("gem1.gif"); prepareImage(diamondgif,this); mangif = Toolkit.getDefaultToolkit().getImage("man15.gif"); prepareImage(mangif,this); gatorgif = Toolkit.getDefaultToolkit().getImage("gator.gif"); prepareImage(mangif,this); imageof = new Image[4]; imagechar = new Image[4]; imageof[astar.WATER]= Toolkit.getDefaultToolkit().getImage("Water.gif"); prepareImage(imageof[astar.WATER],this); imageof[astar.OPEN] = Toolkit.getDefaultToolkit().getImage("grass1.gif"); prepareImage(imageof[astar.OPEN],this); imagechar[astar.OPEN] = mangif; imagechar[astar.WATER] = Toolkit.getDefaultToolkit().getImage("boat.gif"); prepareImage(imagechar[astar.WATER],this); try{Thread.sleep(500);} catch(Exception e) {} // Synch with system // generate random starting positions. // generate initial positions of professor and diamond do { gobx = (int)(Math.random() * PG.COLS); goby = (int)(Math.random() * PG.ROWS); } while (PG.M[goby][gobx]!=PG.OPEN); do { profx = (int)(Math.random() * PG.COLS); profy = (int)(Math.random() * PG.ROWS); } while (PG.M[profy][profx]!=PG.OPEN); // draw map drawmap(); display.drawImage(clip,0,0,this); // draw professor and diamond, initial position display.drawImage(imagechar[PG.M[profy][profx]], (profx*gap),(profy*gap)+yoff,gap,gap,null); display.drawImage(diamondgif,gobx*gap,goby*gap+yoff,gap,gap,null); wg.drawImage(dbuf,0,0,this); animate(); } // constructor boolean wedge(int y, int x) // check if at water's edge { if (y>0 && PG.M[y-1][x]==PG.WATER) return true; if (x>0 && PG.M[y][x-1]==PG.WATER) return true; if (y