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;
    }

}

sorry for the double post, but this is what i was thinking would work, but i am not sure if it checks every surrounding cell..?

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 >= 0 && board[row+1][col] == true){
            count++;
        }
        if (col-1 >=0 && board[row+1][col] == true){
            count++;
        }
        if (col-1 >=0 && board[row-1][col] == true){
            count++;
        }
        if (row+1 < ROW && board[row+1][col] == true) {
            count++;
        }
        if (row+1 < ROW && board[row-1][col] == true) {
            count++;
        }
        if (col+1 < COL && board[row+1][col] == true) {
            count++;
        }
        if (col+1 < COL && board[row-1][col] == true) {
            count++;
        }        
        return count;
    }

Your program seems to be very linear, which may or may not be a good thing. I would recommend making each "cell" an object that has the following attributes:

a private data member that determines whether or not the cell is live or dead (a boolean, probably)

a getter and setter for that private data member

a function that keeps track of how many other "cell" objects are next to it. This function would take the grid that the cell is inside as a parameter.

Seeing as to how the rules are:

1.) Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.

2.) Any live cell with more than three live neighbours dies, as if by overcrowding.

3.) Any live cell with two or three live neighbours lives on to the next generation.

4.) Any dead cell with exactly three live neighbours becomes a live cell.

You could easily check those four conditions with a case statement.

Afterwards populate the grid with these "cell" objects (with a generator determining whether or not each cell is initially alive or dead) and create a loop that goes through the grid, calling the function that each cell object contains.

* an important note, if you're going to have the program check around each square you might want to include try catch statements so that the program doesn't try to check a grid location of -1.


to check each cell around a surrounding cell it would be easier to write down on paper what the grid (including numbering) would look like:


(0,0) (0,1) (0,2) (0,3)
(1,0) (1,1) (1,2) (1,3)
(2,0) (2,1) (2,2) (2,3)
(3,0) (3,1) (3,2) (3,3)

Above is an example of a [4][4] grid. Java handles arrays in a [row][column] format. If you would want to check each surrounding cell around (1,2) you would want to check the following:

(0,2) - row-1, col
(2,2) - row+1, col
(1,1) - row, col-1
(1,3) - row, col+1
(0,1) - row-1, col-1
(0,3) - row-1, col+1
(2,1) - row+1, col-1
(2,3) - row+1, col+1

Thanks for the suggestions, but...my program to the best of my understanding includes a buffer of 2 cells on each side of the row/column and it already has a boolean to see if the cell is alive or dead, in my countaliveneighbors function. with my countaliveneighbors function i was hoping for it to check if the neighbors were alive or not and how many surrounding neighbors, and their places are alive or dead. my main class should already assign each space at pseudorandom living or dead. if this countaliveneighbors worked as i hope then all i would need is a copy board function to sequence into the next board, along with some print statements and adjustments to the main.

did you read all of my post? I think i covered how to check all the adjacent cells and why your program throws errors.

I'm a little hazy at the game of life, but can you expand on the idea of why you need the buffer of 2 in the arrays (i.e. 42 x 42 array instead of a 40 x 40)? Are those edge and corner cells used in the game or are they blank cells which are not alive or dead and simply exist to avoid possible array index overflow errors?

The buffer of 2 rows and copumns is supposed to revent the overflow but never be printed; only there as a precaution. I was thinking that i should just set it false (dead) and only use the 40x40 array

after some more editing I think I have found a way to make it work properly, and for the countallneighbors to be all enclusive of what it checks. now i need help on figuring out what the function should return and how to go about making a way to return what needs to be returned

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 = 1; i <= currBoard.length - 1; i++) {
            for(int j = 1; j <= currBoard.length - 1; 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 && col-1 >= col && board[row-1][col]){
                count++;
            }
            if(row-1 >= 0 && col-1 >= col && board[row+1][col]){
                count++;
            }
            if(row-1 >= 0 && col-1 >= col && board[row][col-1]){
                count++;
            }
            if(row-1 >= 0 && col-1 >= col && board[row][col+1]){
                count++;
            }
            if(row+1 < ROW && col+1 < COL && board[row-1][col-1]){
                count++;
            }
            if(row+1 < ROW && col+1 < COL && board[row-1][col+1]){
                count++;
            }
            if(row+1 < ROW && col+1 < COL && board[row+1][col-1]){
                count++;
            }
            if(row+1 < ROW && col+1 < COL && board[row+1][col+1]){
                count++;
            }
     return count;
    }

}
if(row-1 >= 0 && col-1 >= col && board[row-1][col]){
count++;
}

col - 1 >= col is always going to be false.

after much editing and thought i have most of my code done, but i am having a hard time with printing out the board in a 40x40 table as the array is set to be. i would appreciate being pointed in the right direction, i assume my mistake is careless.

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 = 1; i <= currBoard.length - 1; i++) {
            for(int j = 1; j <= currBoard.length - 1; 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;
                }
            }
        }
        runStep(currBoard,nextBoard);
        copyBoard(currBoard, nextBoard);
        printBoard(nextBoard);


    }

    private static int countLiveNeighbors(int row, int col, boolean[][] board){
        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){
        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) {
        for (int y = 0; y < currBoard.length; y++) {
            for (int z = 0; z < currBoard.length; z++) {
                currBoard[y][z] = nextBoard[y][z];
            }
        }
    }

    private static void printBoard(boolean[][] nextBoard){
        for (int a = 0; a < nextBoard.length; a++) {
            for (int b = 0; b < nextBoard.length; b++) {
                if (nextBoard[a][b] = true) {
                     System.out.print("[O]");
                }
                if (nextBoard[a][b] = false) {
                    System.out.print("[]");
                }
            }
        }
    }
}

in order to make your game work currently, you would need to do the following:

'mark' the cells so that they all change at the same time, as opposed to changing them one at a time. Since you're dealing with each cell one at a time you're constantly changing the environment for each individual cell. You have to deal with all the cells at the same time in order to make the game operate correctly.

you can do that by creating another "board" that contains the locations of grids that contains true/false values. This new board will be called "markerboard" in my example. You use the countliveneighbors method to see which cells need to be "marked" and then switch the corresponding cell in the "markerboard" to true. Then you would run through the "markerboard" with a loop and use it as a map basically to change all the values in the actual game board.

I'm not sure if this would help, but this is a game of life that I created, it won't be the exact same but some of the concepts are still there.

public class Main {

	
	public static void main(String[] args) { 
		Cell[][] Grid = new Cell[40][40];
		Main x = new Main(); 
		
		//makes the game go through 50 complete steps
		for (int loop=0; loop < 50; loop++) 
		x.act(Grid); 
		
	}
	
	public void act(Cell[][] Grid) { 
		//first run through the grid checks which cells need to be toggled
		for (int i=0; i < Grid.length; i++) { 
			for (int j=0; j < Grid[0].length; j++) { 
				Grid[i][j].determineNeighbors(Grid, i, j);
			}
		}
		//second run through the grid toggles all the cells that meet the 
		//requirement
		for (int i=0; i < Grid.length; i++) { 
			for (int j=0; j < Grid[0].length; j++) { 
				Grid[i][j].Switch(); 
			}
		}
	}
}
import java.util.Random;

public class Cell {

	// alive determines whether or not the cell is alive or dead, while
	// markedforswitch determines whether or not the alive variable
	// will switch by the end of the turn. This way the computer calculates
	// which cells need to be switched before actually switching any of the
	// cells

	private boolean alive;
	private boolean markedforswitch;

	// constructors
	public Cell() {
		Random generate = new Random(2);
		init(generate.nextInt()); 
		markedforswitch = false;
		alive = false;
	}

	public Cell(int x) {
		
		init(x); 
		markedforswitch = false;
		
	}

	private void init(int x){ 
		switch (x) {
		case 0:
			alive = false;
			break;
		case 1:
			alive = true;
			break;
		default:
			alive = true;
			break;
		}
	}
	// getters and setters
	public boolean getalive() {
		return alive;
	}

	public void setalive(boolean x) {
		alive = x;
	}

	public boolean getmarkedforswitch() {
		return markedforswitch;
	}

	private void setmarkedforswitch(int numNeighbors) {
		switch (numNeighbors) {
		case 2:
			if (alive)
				markedforswitch = false;
			break;
		case 3:
			if (alive)
				markedforswitch = false;
			if (!alive)
				markedforswitch = true;
			break;
		default:
			if (alive)
				markedforswitch = true;
			break;

		}
	}

	public void determineNeighbors(Cell[][] grid, int row, int col) {
		int numNeighbors = 0;
		for (int currentrow = row - 1; currentrow < row + 1; currentrow++) {
			for (int currentcol = col - 1; currentcol < col + 1; currentcol++) {
				try {
					if ((currentrow != row) && (currentcol != col)) {
						if (grid[currentrow][currentcol].alive)
							numNeighbors++;

					}
				} catch (Exception e) {
					// one of the checked indexes was out of bounds
				}
			}

		}
		setmarkedforswitch(numNeighbors);
	}

	// switches alive to the opposite of what it was
	public void Switch() {

		if (markedforswitch) {
			alive = !alive;
		}
	}
}

what you describe seems to be what my copy board function would like to output. it copys the current board (currBoard) to a new identical board (nextBoard). with what i have, i already found the locations of the true and false cells, i just have to write a function to find each and either print a [O] if it is true and a [ ] if it is false in a 40x40 table, using the 2D array i have made. would a for loop work for this? or what way would i go about doing this without having to make a class, i would rather just work in main

ive made some changes, but still all the values are set to true so it is only printing [O], i do not know why this is occuring, also, how would i go about printing it into a 40x40 table and not just a line?

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 = 1; i <= currBoard.length - 1; i++) {
            for(int j = 1; j <= currBoard.length - 1; 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;
                }
                if (randGen.nextInt(10)!=2) {
                    currBoard[k][l] = false;
                }
            }
        }
        runStep(currBoard,nextBoard);
        copyBoard(currBoard, nextBoard);
        printBoard(nextBoard);
    }
    private static int countLiveNeighbors(int row, int col, boolean[][] board){
        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){
        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) {
        for (int y = 1; y < currBoard.length - 1; y++) {
            for (int z = 1; z < currBoard.length - 1; z++) {
                currBoard[y][z] = nextBoard[y][z];
            }
        }
    }

    private static void printBoard(boolean[][] nextBoard){
        String[][] pboard = new String[ROW+2][COL+2];
        int count = 0;
        for (int c = 1; c < nextBoard.length - 2; c++) {
            for (int d = 1; d < nextBoard.length - 2; d++) {
                pboard[c][d] = " ";
                if (nextBoard[c][d] = true) {
                    pboard[c][d] = "[O]";
                        count++;
                }
                if (nextBoard[c][d] = false) {
                    pboard[c][d] = "[ ]";
                    count++;
                }
            }
        }
        for (int a = 1; a < 40; a++) {
            for (int b = 1; b < 40; b++) {
                System.out.print(pboard[a][b]);
            }
        } System.out.println("");
    }
}

Looks to me like your board starts entirely false, so according to the rules of the game, it should STAY entirely false, right? Any cell with all neighbors dead stays dead according to the game rules, right?

yes, it starts false, but then i assign it randomly true or false, but it still prints them all as true

I think you have a problem with your printBoard function. One, to have it be of any use, you need to put the correct line breaks in there so you can tell which cell is which. Two, and this isn't a defect, but will make the program much easier to debug, make it a 5 x 5 grid, not a 40 x 40 grid so you can see what is going on. Beofre worrying about the next board, make sure it can generate and properly display a current board in a way that is accurate and meaningful. You could be debugging code that is actually correct. Have it generate a non-random board and display it and make sure uit's displaying accurately.

You almost definitely have a problem with printBoard.

private static void printBoard(boolean[][] nextBoard){
        String[][] pboard = new String[ROW+2][COL+2];
        int count = 0;
        for (int c = 1; c < nextBoard.length - 2; c++) {
            for (int d = 1; d < nextBoard.length - 2; d++) {
                pboard[c][d] = " ";
                if (nextBoard[c][d] = true) {
                    pboard[c][d] = "[O]";
                        count++;
                }
                if (nextBoard[c][d] = false) {
                    pboard[c][d] = "[ ]";
                    count++;
                }
            }
        }
        for (int a = 1; a < 40; a++) {
            for (int b = 1; b < 40; b++) {
                System.out.print(pboard[a][b]);
            }
        } System.out.println("");
    }

= instead of == in the if statement. Also, 40 is hard-coded. Use ROW and COL.

with those corrections everything is false. what is wrong with the part of my code assigning random true/falses to the 40x40 array? also how do i tell it to make a new line after the first 10, 2nd 10, 3rd 10, etc.

the supposed random true/false generator:

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;
                }
            }
        }

the full code:

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);

        boolean[][] nextBoard = new boolean[ROW+2][COL+2];
        boolean[][] currBoard = new boolean[ROW+2][COL+2];

        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;
            }    
        }
        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;
                }
            }
        }
        runStep(currBoard,nextBoard);
        copyBoard(currBoard, nextBoard);
        printBoard(nextBoard);
    }
    private static int countLiveNeighbors(int row, int col, boolean[][] board){
        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){
        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) {
        for (int y = 1; y < currBoard.length - 1; y++) {
            for (int z = 1; z < currBoard.length - 1; z++) {
                currBoard[y][z] = nextBoard[y][z];
            }
        }
    }

    private static void printBoard(boolean[][] nextBoard){
        String[][] pboard = new String[ROW+2][COL+2];
        int count = 0;
        for (int c = 1; c < nextBoard.length - 2; c++) {
            for (int d = 1; d < nextBoard.length - 2; d++) {
                pboard[c][d] = " ";
                if (nextBoard[c][d] == true) {
                    pboard[c][d] = "[O]";
                        count++;
                }
                if (nextBoard[c][d] == false) {
                    pboard[c][d] = "[ ]";
                    count++;
                }
            }
        }
        for (int a = 1; a < ROW; a++) {
            for (int b = 1; b < COL; b++) {
                System.out.print(pboard[a][b]);
            }
        } System.out.println("");
    }
}

Fix printBoard first. You've made it way too complicated. Here is a very easy one.

private static void printBoard(boolean[][] nextBoard)
    {
        for (int a = 1; a <= ROW; a++)
        {
            for (int b = 1; b <= COL; b++) 
            {
                if (nextBoard[a][b])
                    System.out.print("[0]");
                else
                    System.out.print("[ ]");

            }
            System.out.println("");
        }
        System.out.println("");
    }
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);

        boolean[][] nextBoard = new boolean[ROW+2][COL+2];
        boolean[][] currBoard = new boolean[ROW+2][COL+2];

        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;
            }    
        }
        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;
                }
            }
        }
        printBoard (currBoard);
        runStep(currBoard,nextBoard);
        copyBoard(currBoard, nextBoard);
        printBoard(nextBoard);

Add the line in red above. You'll see the ORIGINAL board. Until that board makes sense and displays well, do nothing else. You cannot debug, if indeed anything else is wrong, before getting THAT part correct.

i have figured out that it is not assigning currBoard random true/false but rather everything false, as you have earlier stated. I am not sure why this is happening and have tried multiple variations of the same code to randomly generate the true/false, but nothing seems to work
i thought that might work..but it doesn't seem to

for (int k = 1; k < currBoard.length - 1; k++) {
            for (int l = 1; l < currBoard.length - 1; l++) {
                Random randGen = new Random();
                    currBoard[k][l] = randGen.nextBoolean();
            }
        }

i have figured out that it is not assigning currBoard random true/false but rather everything false, as you have earlier stated.

???????

You had it fine before. It is randomly assigning things and it's not all false or all true. If you fix printBoard, you should be OK. You may or may not have problems going from one board to the next, but the initial random assignments work fine. Here's your code with the updates I suggested. This code should give you a board that is not all false and not all true.

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);

        boolean[][] nextBoard = new boolean[ROW+2][COL+2];
        boolean[][] currBoard = new boolean[ROW+2][COL+2];

        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;
            }
        }
        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;
                }
            }
        }
        printBoard(currBoard);
//        runStep(currBoard,nextBoard);
//        copyBoard(currBoard, nextBoard);
//        printBoard(nextBoard);
    }
    private static int countLiveNeighbors(int row, int col, boolean[][] board){
        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){
        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) {
        for (int y = 1; y < currBoard.length - 1; y++) {
            for (int z = 1; z < currBoard.length - 1; z++) {
                currBoard[y][z] = nextBoard[y][z];
            }
        }
    }

    private static void printBoard(boolean[][] nextBoard)
    {
        for (int a = 1; a <= ROW; a++)
        {
            for (int b = 1; b <= COL; b++) 
            {
                if (nextBoard[a][b])
                    System.out.print("[0]");
                else
                    System.out.print("[ ]");
            }
            System.out.println("");
        }
        System.out.println("");
    }
}

Note lines that 31 - 33 are commented out. There may or may not be problems with them, but again, you need to get the original random board generated and displayed correctly before worrying about that. The above code will do that and it is your code with my printBoard function.

Once you are comfortable with that, then uncomment the linesd and go from there.

commented: you helped this kid for x hours, he didn't even mark solved. pf. +5

mission accomplished, thank you both, you have been a tremendous help!

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) {
        //while (true) {
        Random randGen = new Random(1);

        boolean[][] nextBoard = new boolean[ROW+2][COL+2];
        boolean[][] currBoard = new boolean[ROW+2][COL+2];

        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;
            }
        }
        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;
                }
            }
        }
        
        copyBoard(currBoard, nextBoard);
        runStep(currBoard,nextBoard);
        printBoard(currBoard);
        printBoard(nextBoard);
    }
    //}
    private static int countLiveNeighbors(int row, int col, boolean[][] board){
        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){
        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) {
        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) {
        for (int a = 1; a <= ROW; a++) {
            for (int b = 1; b <= COL; b++) {
                if (nextBoard[a][b]){
                    System.out.print("[0]");
                } else {
                    System.out.print("[ ]");
                }
            }
            System.out.println("");
        }
        System.out.println("");
    }
}

actually it turns out i am not done, i want to run the whole game in a for loop lets say 2 times. but when i do this, it prints the same currBoard and nextBoard. when i try setting the nextBoard = currBoard it does not work. i want this to be a continuous game, not repeating the same pattern.

If you want to start a completely new game, then the ideal way would be to have Life set up as a class, and you'd simply create another Object of that class type. Alternatively, you could set all your variables back to blank/however they were initially. But if you're referring to somehow continuing a game already in progress, then nevermind what I just said. You should also create another thread and explain your problem in there.

i hope this post clarifies what i was trying to say. the game of life is a continuos sequence of different frames following the three rules. my program prints out the current board (original) and the next board (after the rules have been followed once). i want the game to run 20 frames of the same life, continuing from the original board. a succession, so the rules would be applied to the current board and that would make the next board, then the next board would have the rules applied to it and the first next board would become current board and the next board of the first next board would be the next succession

Well, first of all, I'd make it Object Oriented. . so the board would be part of a class, and you could create a new board by creating a new Object of that class type. You would also need two types of constructors: one that took an existing board (so that your new Object's board would be the same as the previous board). And one empty constructor, which would create a blank board.

i understand the way you are suggesting, but i am trying to write it all within the main class. if i could assign the next board to current board and then have it run again within the program with a for loops that might possibly work but i have tried and have been unsuccessful

i figured out a way to do it, in the for loop i can change the <= to any number to see how many times it runs

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);

        boolean[][] nextBoard = new boolean[ROW+2][COL+2];
        boolean[][] currBoard = new boolean[ROW+2][COL+2];

        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;
            }
        }
        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;
                }
            }
        }
        runMain(currBoard, nextBoard);
    }    
    private static void runMain (boolean[][] currBoard, boolean[][] nextBoard) {
        printBoard(currBoard);    
        copyBoard(currBoard, nextBoard); 
        runStep(currBoard, nextBoard);
        printBoard(nextBoard);
            for (int g = 0; g <= 2; g++) {
                currBoard = nextBoard;
                runStep(nextBoard, currBoard);
                printBoard(nextBoard);
                nextBoard = currBoard;
                runStep(nextBoard, currBoard);
                printBoard(currBoard);
            }
    }    
    private static int countLiveNeighbors(int row, int col, boolean[][] board){
        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){
        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) {
        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) {
        for (int a = 1; a <= ROW; a++) {
            for (int b = 1; b <= COL; b++) {
                if (nextBoard[a][b]){
                    System.out.print("[0]");
                } else {
                    System.out.print("[ ]");
                }
            }
            System.out.println("");
        }
        System.out.println("");
    }
}

the way above that i thought would work doesn't seem to be working after the first two "days." I am not sure how to go about this any other way.

You should also create another thread and explain your problem in there.

Mark this thread as solved, go start a new thread, and post the specific problem you are having in there. You should at least be able to identify a portion of code that isn't working. At this point, when the thread is 3 pages long, the people who have been helping you probably don't remember everything about what problems you were having, the people who have been helping you probably don't know which problems you successfully solved and which remain unsolved, and the people who weren't helping you don't want to read 3 pages in order to figure out what is going on in here. I explain all of this only because you ignored my advice last time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.