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