示例#1
0
  /**
   * A* implementation over Tile graph
   *
   * @param start : coordinates of start
   * @param fin : coordinates of end
   * @return LinkedList<Tile> representing path
   */
  private static LinkedList<? extends Node> getPath(
      Node[][] board, Vec2i start, Vec2i fin, Master m, Iterable<? extends Sprite> spriteSet) {
    LinkedList<Node> path = new LinkedList<Node>();
    PriorityQueue<Node> open = new PriorityQueue<Node>();
    open.add(board[start.x][start.y]);
    HashSet<Node> done = new HashSet<Node>();
    if (board[fin.x][fin.y].impassible()) {
      return null;
    }
    for (Sprite s : spriteSet) {
      if (s.getPlayer() == m.whoseTurn() && s.getLocation().equals(new Vec2i(fin.y, fin.x))) {
        return null;
      }
    }

    board[start.x][start.y].setDistance(0);
    while (!open.isEmpty()) {
      Node curr = open.poll();
      if (fin.x == curr.getCoords().x && fin.y == curr.getCoords().y) {
        Node t = curr;
        while (t.getPrev() != null) {
          path.addFirst(t);
          t = t.getPrev();
        }

        return path;
      }

      done.add(curr);

      for (Node neigh : Pathfinding.passableNeighbors(board, curr, m, spriteSet)) {
        if (done.contains(neigh)) {
          continue;
        }

        if (neigh.getDistance() > curr.getDistance() + 1) {
          neigh.setDistance(curr.getDistance() + 1);
          neigh.setPrev(curr);
          neigh.setEstimate(board[fin.x][fin.y]);
        }
        open.add(neigh);
      }
    }
    return null;
  }