I gave flood fill a go but it only seems to result in a stack overflow.
This gets called every time a new island is made. The int[] tiles array contains the tile ids of the map, 0 is land and 1 is water. Each island class has a List coords = new ArrayList();
public void floodFill(int[] tiles, int width, int height, Coord current, Coord target){
//Check if target x,y is out of bounds.
if(target.getX() < 0 || target.getY() < 0 || target.getX() >= width || target.getY() >= height) return;
//Check if target x,y is a land tile.
if(tiles[target.getX() + target.getY() * width] != 0) return;
//Checks if target x,y has already been filled.
if(contains(target.getX(), target.getY())) return;
//Add target to filled tiles list.
coords.add(target);
//Fill adjacent tiles.
floodFill(tiles, width, height, target, new Coord(current.getX() + 1, current.getY()));
floodFill(tiles, width, height, target, new Coord(current.getX() - 1, current.getY()));
floodFill(tiles, width, height, target, new Coord(current.getX(), current.getY() + 1));
floodFill(tiles, width, height, target, new Coord(current.getX(), current.getY() - 1));
return;
}
//Checks filled tiles list for specific coords.
public boolean contains(int x, int y){
return coords.contains(new Coord(x, y));
}
I’m not sure if I’m doing something completely wrong here.