I have a problem with my loop, it must be a basic fix but i cant seem to fix this issue, this is a 2D array that i am checking the neighbours of a grid for , now
if randomGrid [rows -1] and [col] == 1 , this means the cell that is on the left of the current cell on the x Axis
for(int rows = 0 ; rows < randomGrid.length ; ++rows){
for(int cols = 0 ; cols < randomGrid.length ; ++cols){
//all 8 neighbours
if(randomGrid[rows-1][cols] == 1){
neighbours++;
}
if(randomGrid[rows-1][cols-1] == 1){
neighbours++;
}
error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Control.count(Control.java:196)
at Control.main(Control.java:247)