So I'm building a sort of "Game of Life" application in java and are having some trouble detecting when the index is out of bounds.The code below is a modified version from another qustion here from SO.
2D array:
1,1,1,1
1,1,1,1
1,1,1,1
1,1,1,1
Code
private void traverse() {
for (int i = 0; i < brickArray.length; i++) {
for (int j = 0; j < brickArray[i].length; j++) {
System.out.println(i - 1 + "," + j);
checkIfAllowed(i - 1, j);
}
}
}
public void checkIfAllowed(int row, int column) {
boolean incorrect = check(row) && check(column);
if (incorrect) {
System.out.println("Wall");
} else {
System.out.println("Road");
}
}
private boolean check(int index) {
return (index < 0 || index > board.getHeight());
}
Result:
Traversing:
-1,0
Road
-1,1
Road
-1,2
Road
-1,3
Road
0,0
Road
0,1
Road
0,2
Road
0,3
Road
1,0
Road
1,1
Road
1,2
Road
1,3
Road
2,0
Road
2,1
Road
2,2
Road
2,3
Road
In this example, the 4 top cells should have printed "Wall", but they didn't.