logo CodeStepByStep logo

gameOfLife

Write a method named gameOfLife that performs one update on a Game of Life board represented as a 2D array. Conway's Game of Life is a simple representation of cellular automata behavior using a board of characters representing cells. A given cell can be either alive (1) or dead (0). To update the board, each cell's status should change based on the number of living direct neighbors it has in each of the eight directions. The rules are as follows:

  • A living cell with 0 or 1 living neighbors dies from underpopulation.
  • A living cell with 2 or 3 living neighbors stays alive.
  • A living cell with more than 3 living neighbors dies from overpopulation.
  • A dead cell with exactly 3 living neighbors comes to life.

For example, suppose the array contains the following state:

int[][] board = {
    {0, 0, 1, 0, 1},
    {0, 0, 1, 0, 0},
    {0, 0, 1, 0, 0},
    {0, 0, 1, 1, 0},
    {0, 0, 0, 0, 0},
};

Then the call of gameOfLife(board); should modify it to store the following:

{
    {0, 0, 0, 1, 0},
    {0, 1, 1, 0, 0},
    {0, 1, 1, 0, 0},
    {0, 0, 1, 1, 0},
    {0, 0, 0, 0, 0}
}

Your code should work for a board of any size, even one with 0 rows or columns. You may assume that the 2-D array is rectangular, that is, that each row of the 2-D array contains the same number of columns. You may assume that no values appear in the board other than 0 and 1.

Method: Write a Java method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.