0

I'm trying to implement a bfs algorithm in Java,but it doesn't work as it should be.

I've made a game map comprised of HexTile objects(custom objects,similar to matrix elements). Each HexTile includes one adjacency list containing references to the elements that it's connected to, one function that returns those elements and one function that computes the distance between two HexTiles. The bfs algorithm is excecuted in another class called unit(units are placed in HexTiles) and finds every unit available in a given range from the room(currentTile). It then creates an ArrayList with the given units.

class HexTile {

  static final int MAX_NEIGHBOURS = 6;
  private HexTile[] neighbours;

  public HexTile[] getNeighbours() {
    return this.neighbours;
  }

  public double distanceFromTarget(HexTile target) {
    double distance = Math.sqrt(Math.pow((this.getRow() - target.getRow()), 2) + Math.pow((this.getCol() - target.getCol()), 2));
    return distance;
  }
}
class Unit {

  private ArrayList<Unit> unitsWithinRange = new ArrayList<Unit>();

  private void findUnitsWithinRange(HexTile currentTile, int attackRange) {
    Queue<HexTile> queue = new LinkedList<>();
    ArrayList<HexTile> visited = new ArrayList<HexTile>();
    queue.add(currentTile);
    visited.add(currentTile);
    while (!queue.isEmpty()) {
      HexTile aux = queue.poll();
      for (HexTile auxNeigh : aux.getNeighbours()) {
        if (auxNeigh != null && (!visited.contains(auxNeigh))) {

          visited.add(auxNeigh);
          queue.add(auxNeigh);
        }
      }
      if (aux != null && (currentTile.distanceFromTarget(aux) <= attackRange)) {
        Unit auxUnit = aux.getUnitOnTile();
        this.unitsWithinRange.add(auxUnit);
      }

    }
    queue.clear();
    visited.clear();
  }
}

What happens whenever findUnitsWithinRange is excecuted is that it return a list of units,but the units that are in range 1 are not included(direct neighbours to root).Sometimes the program crashes,because units need to be able to know if there are any nearby units,to excecute some other functions.Any advice would be appreciated!

Chaosfire
  • 4,818
  • 4
  • 8
  • 23
  • "Sometimes the program crashes" - for what reason? – Alexander Ivanchenko Sep 18 '22 at 10:33
  • 1
    "Sometimes the program crashes" - please include the exact exception message and stacktrace. Also prepare [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). Current code is not enough to reproduce your issue(it can't even compile). – Chaosfire Sep 20 '22 at 10:41
  • Have you tried using a debugger? It would probably help a lot – DL33 Sep 20 '22 at 10:42
  • There's a bit of redundancy in your code. But I don't see a logical flaw in the BFS implementation you've provided. – Alexander Ivanchenko Sep 24 '22 at 23:24
  • 2
    The only assumption I could make: there might be an issue with `distanceFromTarget()` method. I see that it's calculating a hypotenuse of a right-angled triangle (that perfectly clear). But since your tiles are *hexagonal* (that's my interpretation of the class name `HexTile`), I've no idea `row` and `col` could mean (because tiles are not rectangular). – Alexander Ivanchenko Sep 25 '22 at 00:04
  • It can still be squares with 6 faces (as in chess for example) – Pedro Silva Sep 26 '22 at 23:21

0 Answers0