# Issue

This Content is from Stack Overflow. Question asked by AlexandrosS

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.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;  }
}

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

}

}
if (aux != null && (currentTile.distanceFromTarget(aux) <= attackRange)) {
Unit auxUnit = aux.getUnitOnTile();
}

}
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.Sometimes the program crashes.Any advice would be appreciated!

# Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

```