i will listen to your advice in the future
i figured it out, and this time it works, i added in comments to make reading it easier, thank you to those who helped.
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(1);
// sets up a 40x40 array with a 2 row x column buffer
boolean[][] nextBoard = new boolean[ROW+2][COL+2];
boolean[][] currBoard = new boolean[ROW+2][COL+2];
// sets whole board equal to false
for(int i = 1; i <= currBoard.length - 1; i++) {
for(int j = 1; j <= currBoard.length - 1; j++) {
currBoard[i][j] = false;
nextBoard[i][j] = false;
}
}
// random life game
for (int k = 1; k < currBoard.length - 1; k++) {
for (int l = 1; l < currBoard.length - 1; l++) {
int random = randGen.nextInt(10);
if (random==2) {
currBoard[k][l] = true;
}
if (random!=2) {
currBoard[k][l] = false;
}
}
}
// glider
// currBoard[30][30] = true;
// currBoard[30][31] = true;
// currBoard[30][32] = true;
// currBoard[29][30] = true;
// currBoard[28][31] = true;
runMain(currBoard, nextBoard, 20);
}
private static void runMain (boolean[][] currBoard, boolean[][] nextBoard, int days) {
// executes the continuation of the program
printBoard(currBoard);
copyBoard(currBoard, nextBoard);
runStep(currBoard, nextBoard);
printBoard(nextBoard);
copyBoard(nextBoard, currBoard);
runStep(nextBoard, currBoard);
for (int g = 0; g <= days; g++) {
printBoard(currBoard);
copyBoard(currBoard, nextBoard);
runStep(currBoard, nextBoard);
printBoard(nextBoard);
copyBoard(nextBoard, currBoard);
runStep(nextBoard, currBoard);
}
}
private static int countLiveNeighbors(int row, int col, boolean[][] board){
// counts all the live neighbors around a specific cell (checks every cell)
int count = 0;
if(board[row-1][col]){
count++;
}
if(board[row+1][col]){
count++;
}
if(board[row][col-1]){
count++;
}
if(board[row][col+1]){
count++;
}
if(board[row-1][col-1]){
count++;
}
if(board[row-1][col+1]){
count++;
}
if(board[row+1][col-1]){
count++;
}
if(board[row+1][col+1]){
count++;
}
return count;
}
private static void runStep(boolean[][] currBoard, boolean[][] nextBoard){
// using countLiveNeighbors it determines if the surrounding cells, and the cell itself lives or dies that day
for (int f = 1; f < currBoard.length - 1; f++){
for (int p = 1; p < currBoard.length - 1; p++) {
if(countLiveNeighbors(f, p, currBoard) == 3)
nextBoard[f][p] = true;
if(countLiveNeighbors(f, p, currBoard) < 2)
nextBoard[f][p] = false;
if(countLiveNeighbors(f, p, currBoard) > 3)
nextBoard[f][p] = false;
}
}
}
private static void copyBoard(boolean[][] currBoard, boolean[][]nextBoard) {
// copies all the nextBoard values to the currBoard values
for (int y = 1; y < currBoard.length - 1; y++) {
for (int z = 1; z < currBoard.length - 1; z++) {
nextBoard[y][z] = currBoard[y][z];
}
}
}
private static void printBoard(boolean[][] nextBoard) {
// prints out the program, • being a living cell, a space being a dead cell
for (int a = 1; a <= ROW; a++) {
for (int b = 1; b <= COL; b++) {
if (nextBoard[a][b]){
System.out.print("•");
} else {
System.out.print(" ");
}
}
System.out.println("");
}
// shows when the day ends and begins
System.out.println("------------------------------------------------------------------------------------------------------------------------");
}
}