I'm working with a double array and need to be able to calculate the surrounding <8 neighbors of one tile from class Tile. Since I want to know them individually, Tile contains 8 setter-methods that accept Tile as a parameter for every direction respectively. So first I'm iterating through the neighbors of a given tile and add them to an ArrayList. Now I want to pass those elements into the setter-methods but they won't accept it because it's not a Tile but a List<Tile>
How can I work around this? I tried changing the setter-Method to expect a List but that didn't change anything. Or do I maybe need to restructure my entire approach? For this I was looking at a tile that definitely had all 8 neighbors, I need to think of catching those at the edge who don't have as much neighbors once this works in general.
private void calculateNeighbors (int x, int y){
System.out.print("The neighbors of " + getTile(x,y));
List<Tile> neighbors = new ArrayList<>();
int[] coordinates = new int[]{
-1,-1,
-1, 0,
-1, 1,
0,-1,
0, 1,
1,-1,
1, 0,
1, 1
};
for (int i = 0; i < coordinates.length; i++) {
int columnX = coordinates[i];
int rowY = coordinates[++i];
int nextX = x + columnX;
int nextY = y + rowY;
if (nextX >= 0 && nextX < getListOfTiles().length
&& nextY >= 0 && nextY < getListOfTiles().length) {
neighbors.add(getTile(nextX, nextY));
}
}
//this here isn't working
tile.setTopLeft(neighbors[0]);
tile.setTop(neighbors[1]);
//etc
}
}