Exemplo n.º 1
0
  public Pathfinder(int r, int c) // constructor
      {
    rows = r;
    cols = c;

    HX = new Hexagon[r][c];

    for (int i = 0; i < r; i++) {
      int odd = i % 2;
      for (int j = 0; j < c; j++) {
        HX[i][j] =
            new Hexagon(yoff / 2 + (j * 2 + odd) * hpdist, yoff + gap + (3 * gap / 2 * i), gap);
      }
    } // for i,j

    PG = new MyAstar(r, c); // note it's myastar, not astar

    XDIM = cols * hpdist * 2 + yoff; // ((cols+1)*gap*3)/2;
    YDIM = ((rows + 1) * gap * 3) / 2;
    this.setBounds(0, 0, XDIM + 5, YDIM + yoff + 5);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    display = this.getGraphics();

    diamondgif = Toolkit.getDefaultToolkit().getImage("gem1.gif");
    prepareImage(diamondgif, this);
    mangif = Toolkit.getDefaultToolkit().getImage("man15.gif");
    prepareImage(mangif, this);

    imagechar = new Image[4]; // image of character while on terrain type
    imagechar[Astar.OPEN] = mangif;
    imagechar[Astar.WATER] = Toolkit.getDefaultToolkit().getImage("boat.gif");
    prepareImage(imagechar[Astar.WATER], this);

    imagechar[Astar.DESERT] = Toolkit.getDefaultToolkit().getImage("camel.gif");
    prepareImage(imagechar[Astar.DESERT], this);

    imagechar[Astar.FOREST] = Toolkit.getDefaultToolkit().getImage("bear.gif");
    prepareImage(imagechar[Astar.FOREST], this);

    try {
      Thread.sleep(500);
    } catch (Exception e) {
    } // Synch with system
    // draw static background as a green rectangle
    display.setColor(Color.green);
    display.fillRect(0, 0, XDIM, YDIM + yoff); // fill background

    // 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 || Astar.distance(goby, gobx, profy, profx) < 20);

    // draw map
    drawmap();
    System.out.println(profy + "," + profx + " " + goby + "," + gobx);
    // draw professor and diamond, initial position
    int px = getx(profy, profx), py = gety(profy, profx); // center hx coords
    display.drawImage(
        imagechar[PG.M[profy][profx]], (px - gap / 2), (py - gap / 2), gap, gap, null);
    px = getx(goby, gobx);
    py = gety(goby, gobx);
    display.drawImage(diamondgif, px - gap / 2, py - gap / 2, gap, gap, null);
    /*
    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);
     */
    animate();
  } // constructor