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];
This is my changed version, but I still can’t get it to work.
// 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;
}
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.
// 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;
}