# Game of life not working

**URL:** <https://discourse.processing.org/t/game-of-life-not-working/27012>\
**Category:** Coding Questions\
**Created:** [January 13, 2021, 11:11am UTC](https://discourse.processing.org/t/game-of-life-not-working/27012 "2021-01-13T11:11:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![FrigittyFrog](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/frigittyfrog/32/11758_2.png) [@FrigittyFrog](https://discourse.processing.org/u/FrigittyFrog)\
**Post date:** [January 13, 2021, 11:11am UTC](https://discourse.processing.org/t/game-of-life-not-working/27012/1 "2021-01-13T11:11:05Z")

</div>

**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](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life)]. 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;
  }
}          

```

}  
}

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [January 13, 2021, 11:37am UTC](https://discourse.processing.org/t/game-of-life-not-working/27012/2 "2021-01-13T11:37:41Z")

</div>

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

Check your conditions in `helloneighourino()`! 😉

---

<div class="post-metadata">

**Author:** ![FrigittyFrog](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/frigittyfrog/32/11758_2.png) [@FrigittyFrog](https://discourse.processing.org/u/FrigittyFrog)\
**Post date:** [January 13, 2021, 3:00pm UTC](https://discourse.processing.org/t/game-of-life-not-working/27012/3 "2021-01-13T15:00:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![FrigittyFrog](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/frigittyfrog/32/11758_2.png) [@FrigittyFrog](https://discourse.processing.org/u/FrigittyFrog)\
**Post date:** [January 13, 2021, 3:01pm UTC](https://discourse.processing.org/t/game-of-life-not-working/27012/4 "2021-01-13T15:01:42Z")

</div>

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

```auto
// 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;
}

```

---

<div class="post-metadata">

**Author:** ![noahbuddy](https://avatars.discourse-cdn.com/v4/letter/n/73ab20/32.png) [@noahbuddy](https://discourse.processing.org/u/noahbuddy)\
**Post date:** [January 13, 2021, 6:42pm UTC](https://discourse.processing.org/t/game-of-life-not-working/27012/5 "2021-01-13T18:42:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![FrigittyFrog](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/frigittyfrog/32/11758_2.png) [@FrigittyFrog](https://discourse.processing.org/u/FrigittyFrog)\
**Post date:** [January 13, 2021, 6:46pm UTC](https://discourse.processing.org/t/game-of-life-not-working/27012/6 "2021-01-13T18:46:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![FrigittyFrog](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/frigittyfrog/32/11758_2.png) [@FrigittyFrog](https://discourse.processing.org/u/FrigittyFrog)\
**Post date:** [January 27, 2021, 4:03pm UTC](https://discourse.processing.org/t/game-of-life-not-working/27012/7 "2021-01-27T16:03:25Z")

</div>

Hwy, I just want to thank you for the help, the issues you pointed out helped me debug it, I managed to find one last problem (really simple, I forgot that the else if’s don’t run if the first if is true XD) after a break from coding this. This is the finished code if anyone wants to see it. 😛

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

int [][] grid;
int [][] ngrid;

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

  for (int n = 0; n < 100000; n++)
  {
    grid[int (random(width))][int (random(height))] = 1; //sets random cells on the grid [][] to 1
  }
  /* //glider (just to test it)
  grid[10][10] = 1;
  grid[12][9] = 1;
  grid[12][10] = 1;
  grid[12][11] = 1;
  grid[11][11] = 1;
  */
}

void draw()
{ 
  valset();    

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

void valset() // checks the value of neighouring cells, sets the value to ngrid according to the value of neighbouring cells
{                
  for (int y = 1; y < height - 1; y++)
  {
    for (int x = 1; x < width - 1; x++)
    {
      int num = 0;
      
      if (grid[x-1][y-1] == 1)
      {
        num++;
      } 
      if (grid[x][y-1] == 1)
      {
        num++;
      }
      if (grid[x+1][y-1] == 1)
      {
        num++;
      }
      if (grid[x-1][y] == 1)
      {
        num++;
      }
      if (grid[x+1][y] == 1)
      {
        num++;
      }
      if (grid[x-1][y+1] == 1)
      {
        num++;
      }
      if (grid[x][y+1] == 1)
      {
        num++;
      }
      if (grid[x+1][y+1] == 1)
      {
        num++;
      } 

      if (grid[x][y] == 1)
      {
        if (num > 1 && num < 4)
        {
          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;
}

```

Thanks again for the help!
