So, I have a 4x4 2D array (it will always be these dimensions). Starting with a location on the array, some row and column, I want to find all of its valid neighbors. So far, I
This is my solution:
public int[4][4] array2d;
//don't forget to fill it!
private void adjustNeighbors(int xCoord, int yCoord) {
for (int yi = y-1; yi <= yCoord+1; yi++) { //loop through the neighbors
for (int xi = x-1; xi <= xCoord+1; xi++) {
try {
if (!(xCoord != xi && yCoord != yi)) {
array2d[y][x]++; //do whatever you want to all the neighbors!
}
} catch (Exception e) {
// something is out of bounds
}
}
}
}