Conway's Game of Life logic isn't working properly?

Hello! I'm trying to make a visualization of Conway's Game of Life, but as the title suggests, I'm having a small bug:

The logic I implemented to check how many "alive neighbors" a cell consistently gets the number of alive neighbors incorrect... Below is the code, with some sample output.

 final int SIZE = 5;
final int SCREEN_WIDTH = 500;
final int SCREEN_HEIGHT = 500;

final color ON = color(255, 255, 255);
final color OFF = color(0);

final int SQUARE_LENGTH = SCREEN_WIDTH/SIZE;

boolean [][] GOL_Grid = new boolean[SIZE][SIZE];

void setup()
{
  size(500, 500);
  background(200);
  println("BEFORE ASSIGNMENT");
  printGrid(GOL_Grid);

  //Attempting to make the "Blinker" oscillator (found on the wikipedia page for Conway's Game Of Life)
  GOL_Grid[1][2] = true;
  GOL_Grid[2][2] = true;
  GOL_Grid[3][2] = true;
    println("AFTER ASSIGNMENT");
  printGrid(GOL_Grid);

  GOL_Grid = updateGrid(GOL_Grid);
  println("AFTER UPDATING");
  printGrid(GOL_Grid);
}

void drawGrid(final boolean[][] grid)
{
  println("------------------------");
  for (int i = 0; i < grid.length; i++)
  {
    int curr_Y = i*SQUARE_LENGTH;
    for (int j = 0; j < grid[i].length; j++)
    {
      if (grid[i][j])
        print( "X\t");
      else
        print("-\t");
      int curr_X = j*SQUARE_LENGTH;
      stroke(255);
      if (grid[i][j])
        fill(ON); 
      else
        fill(OFF);

      rect(curr_X, curr_Y, SQUARE_LENGTH, SQUARE_LENGTH);
    }
    println();
  }
  println("------------------------");
}

/************
 (1) Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
 (2) Any live cell with two or three live neighbours lives on to the next generation.
 (3) Any live cell with more than three live neighbours dies, as if by overpopulation.
 (4) Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

 *************/
boolean updateCell(final boolean[][] grid, int row, int col)
{
  int neighborCount = 0;
  boolean isAlive = grid[row][col];

  for (int newRow = row-1; newRow <= row+1; newRow++)
  {
    if (newRow < 0 || newRow >= SIZE) continue; //Bounds check

    for (int newCol = col-1; newCol <= col+1; newCol++)
    {
      if (newCol < 0 || newCol >= SIZE) continue; //Bounds check

      if (row == newRow && col == newCol) continue;
      if (grid[newRow][newCol] == true) //Checks if each neighbor is "alive"
        neighborCount++; //The current cell's alive/death state is based on how many of its neighbors are alive
    }
  }
  print(neighborCount + "\t"); //Prints how many neighbors there are, for debugging purposes
  boolean update = grid[row][col];

  if (neighborCount < 2 || neighborCount > 3)
    update = false; //Cell is dead;
  if (!grid[row][col] && neighborCount == 3) 
    update = true;

  return update;
}

boolean[][] updateGrid(final boolean[][] grid)
{
println("NOW UPDATING GRID (Now showing how many alive neigbors a particular cell has): "); 
  boolean[][] grid_COPY = grid;
  for (int row = 0; row < SIZE; row++) //Iterate through once per cell in the original grid
  {
    for (int col = 0; col < SIZE; col++)
    {
      grid_COPY[row][col] = updateCell(grid, row, col);
    }
    println();
  }
  return grid_COPY;
}

void printGrid(boolean[][] grid)
{
  println("------------------------");
  for (int i = 0; i < grid.length; i++)
  {
    int curr_Y = i*SQUARE_LENGTH;
    for (int j = 0; j < grid[i].length; j++)
    {
      if (grid[i][j])
        print( "X\t");
      else
        print("-\t");
    }
    println();
  }
  println("------------------------");
}

I should mention that while debugging, I think the error lies in either the updateGrid or the updateCell functions

OUTPUT:

BEFORE ASSIGNMENT
------------------------
-   -   -   -   -   
-   -   -   -   -   
-   -   -   -   -   
-   -   -   -   -   
-   -   -   -   -   
------------------------
AFTER ASSIGNMENT
------------------------
-   -   -   -   -   
-   -   X   -   -   
-   -   X   -   -   
-   -   X   -   -   
-   -   -   -   -   
------------------------
NOW UPDATING GRID (Now showing how many alive neighbors a particular cell has): 
0   1   1   1   0   
0   2   1   1   0   
0   2   1   1   0   
0   1   0   0   0   
0   0   0   0   0   
AFTER UPDATING
------------------------
-   -   -   -   -   
-   -   -   -   -   
-   -   -   -   -   
-   -   -   -   -   
-   -   -   -   -   
------------------------

Answers

  • Answer ✓

    I think your problem is in line 96.

    boolean[][] grid_COPY = grid;

    You assign "grid" to a new variable, but that does not create a copy of that array it just stores a reference to that array in a new variable. Instead you might want to create a new array:

    boolean[][] grid_COPY = new boolean[SIZE][SIZE];

    Does that solve your problem?

  • Yes, that must be the problem. You're anyways not using the values in the copy of the grid.
    This is the way classes and objects work in Java.

Sign In or Register to comment.