Game of life not working

Can anyone tell me why my code is not working? There isn’t a syntax error, but everytime I run the code, the screen immeadiatelly turns white. I’m trying to create the game of life [Conway's Game of Life - Wikipedia]. Thanks!

My code:

int [][] grid;
int [][] ngrid;
int x, y;

void setup()
{
size(1000, 1000);
grid = new int [width] [height];
ngrid = new int [width] [height];

for(int n = 0; n < 10000; n++)
{
grid[floor(random(width/2 - width/10, width/2 + width/10))][floor(random(height/2 - height/10, height/2 + height/10))] = 1;
}
}

void draw()
{
background(255);
//for(int p = 0; p < 100; p++){
helloneighourino();
grid = ngrid;
//}
loadPixels();
for(int i = 0; i < width; i++)
{
for(int j = 0; j < width; j++)
{
int pix = j * width + i;
if(ngrid[i][j] == 1)
{
pixels[pix] = color(0);
}
else
{
pixels[pix] = color(255);
}
}
}
updatePixels();
}

void helloneighourino()
{
int num = 0;
for(int x = 1; x < width - 1; x++)
{
for(int y = 1; y < width - 1; y++)
{
if(grid[x-1][y] == 1)
{
num++;
}
else if (grid[x+1][y] == 1)
{
num++;
}
else if (grid[x][y-1] == 1)
{
num++;
}
else if (grid[x][y+1] == 1)
{
num++;
}
else if (grid[x-1][y-1] == 1)
{
num++;
}
else if (grid[x-1][y+1] == 1)
{
num++;
}
else if (grid[x+1][y-1] == 1)
{
num++;
}
else if (grid[x+1][y+1] == 1)
{
num++;
}
else
{
}

  if(num < 2 && num > 3)
  {
    ngrid [x][y] = 1;
  }
  else
  {
    ngrid [x][y] = 0;
  }
}          

}
}

What number is less than two and also greater than three?

Check your conditions in helloneighourino()! :wink:

I experimented around with it a bit and noticed it, but for some reason this didn’t work either, but thanks :slight_smile:

This is my changed version, but I still can’t get it to work. :frowning:

// game of life: sets random cells to 1 and changes the value of cells according to neighbouring cells

int [][] grid;
int [][] ngrid;
int x, y;

void setup()
{
  size(500, 500);
  frameRate(1);
  grid = new int [width] [height];
  ngrid = new int [width] [height];

  for (int n = 0; n < 10000; n++)
  {
    grid[int (random(10, width - 10))][int (random(10, height - 10))] = 1;      //sets random cells on the grid [][] to 1
  }
}

void draw()
{ 
  background(255);  
  helloneighourino();    

  loadPixels();
  for (int i = 0; i < width; i++)      //loops through all the cells in ngrid (newgrid) and if it's set to 1, displays the cell black
  {
    for (int j = 0; j < width; j++)
    {
      int pix = j * width + i;
      if (ngrid[i][j] == 1)
      {
        pixels[pix] = color(0);
      } else
      {
        pixels[pix] = color(255);
      }
    }
  }
  updatePixels();
  swap();
}


void helloneighourino()    // calculates the value of each cells on the grid and sets the new value to the newgrid, so if a cells value switches it doesn't affect the neighbours
{                          // checks the value of it's neighouring cells, if the number of neighbours that are set to 1 is higher than 3 or lower than 2, the cell will be set to 0
  int num = 0;
  for (int x = 1; x < width - 1; x++)
  {
    for (int y = 1; y < width - 1; y++)
    {
      if (grid[x-1][y] == 1)
      {
        num++;
      } else if (grid[x+1][y] == 1)
      {
        num++;
      } else if (grid[x][y-1] == 1)
      {
        num++;
      } else if (grid[x][y+1] == 1)
      {
        num++;
      } else if (grid[x-1][y-1] == 1)
      {
        num++;
      } else if (grid[x-1][y+1] == 1)
      {
        num++;
      } else if (grid[x+1][y-1] == 1)
      {
        num++;
      } else if (grid[x+1][y+1] == 1)
      {
        num++;
      } else
      {
      }

      if (num > 1 && num < 4)
      {
        print(num);
        println();
        print(x);
        println();
        print(y);
        println();
      }

      if (grid[x][y] == 1)
      {
        if (num == 2 || num == 3)
        {
          ngrid[x][y] = 1;
        } else
        {
          ngrid[x][y] = 0;
        }
      } else
      {
        if (num == 3)
        {
          ngrid[x][y] = 1;
        } else
        {
          ngrid[x][y] = 0;
        }
      }
    }
  }
}

void swap()      // sets the new grid equal to the old grid, the new grid values become the old ones in the next frame 
{
  int temp [][] = grid;
  grid = ngrid;
  ngrid = temp;
}

Reset “num” to zero for each local group.
At the moment it is accumulating across the entire grid.

Also, I do not think you want to use “else” statements for each test.

I didn’t notice that, I’m ganna try it out and see if it works then. Thank you!