for those of you not familiar with the rules they can be found here:
i am sort of new to java and i am trying to write the game of life, i have got the board made and how to randomly assign each cell a true or false (living or dead) variable, but for each cell i now need to write a way to count its neighbors so the rules of the game can be followed...from examples the two if statements i have in the countaliveneighbors function i know that the first checks for one of the four surrounding neighbors, while the second if statement checks for one of the four diagonal neighbors, i have tried many thinks and thought about this for a while but i am not sure what the other if statements should be and what this function should return.
*also when i run this program i get an out of bounds error in line 14 and 15, i will star them*
package gameolife;
import java.util.Random;
public class Life {
private static final int ROW = 40;
private static final int COL = 40;
public static void main(String[] args) {
Random randGen = new Random();
boolean[][] nextBoard = new boolean[ROW+2][COL+2];
boolean[][] currBoard = new boolean[ROW+2][COL+2];
for(int i = 0; i <= currBoard.length; i++) {
for (int j = 0; i <= nextBoard.length; j++) {
* currBoard[i][j] = false;
* nextBoard[i][j] = false;
}
}
for (int k = 1; k < currBoard.length - 1; k++) {
for (int l = 1; l < currBoard.length - 1; l++) {
if (randGen.nextInt(10)==2) {
currBoard[k][l] = true;
}
}
}
}
public static int countLiveNeighbors(int row, int col, boolean[][] board){
int count = 0;
if (row-1 >= 0 && board[row-1][col] == true) {
count++;
}
if (row+1 < COL && board[row+1][col] == true) {
count++;
}
return count;
}
}