Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Life.countNeighbors(Life.java:34)
at Life.update(Life.java:77)
at Life.main(Life.java:148)
I know why its giving me the error but i can't for the life of me figure out how to fix it. Any suggestions?
public static int countNeighbors(boolean[][] board, int i, int j)
{
int count = 0;
if (board[i+1][j] == true){
count = count + 1;
}
if (board[i][j+1] == true){
count = count + 1;
}
if (board[i][j-1] == true){
count = count + 1;
}
if (board[i-1][j] == true){
count = count + 1;
}
if (board[i+1][j+1] == true){
count = count + 1;
}
if (board[i+1][j-1] == true){
count = count + 1;
}
if (board[i-1][j+1] == true){
count = count + 1;
}
if (board[i-1][j-1] == true){
count = count + 1;
}
return count;
}
public static void update(boolean[][] board){
boolean[][] original = Arrays.copyOf(board,board.length);
for (int i = 0; i < original.length ; i++){
for ( int j = 0; j <= original[0].length ; j++){
int neighbor = countNeighbors(board,i,j);
if (original[i][j] == false && neighbor == 3){
original[i][j] = true;
}
if (original[i][j] == true && neighbor <= 1){
original[i][j] = false;
}
if (original[i][j] == true && neighbor > 3){
original[i][j] = false;
}
}

New Topic/Question
Reply



MultiQuote





|