- Code: Select all
int array[360][240], timer;
int neighbors(int i, int j)
{
int count;
count += (!array[j-1][i]);
count += (!array[j+1][i]);
count += (!array[j-1][i+1]);
count += (!array[j+1][i+1]);
count += (!array[j-1][i-1]);
count += (!array[j+1][i-1]);
count += (!array[j][i-1]);
count += (!array[j][i+1]);
return count;
}
void createRand()
{
int i, j, life;
for(i=1; i<360; i++)
{
for(j=1; j<240; j++)
{
life = rand(25);
if(life > 0)
life = 1;
array[i][j]=life;
}
}
}
void display()
{
int i, j;
for(i=1; i<360; i++)
{
for(j=1; j<240; j++)
{
setpen(255, 255, 255, array[i][j], 1);
putpixel(i, j);
}
}
}
void simulate()
{
int i, j, count;
for(i=1; i<360; i++)
{
for(j=1; j<240; j++)
{
count = neighbors(i, j);
if(!array[i][j])
{
if(count == 2 || count == 3)
array[i][j] = 0;
else
array[i][j] = 1;
}
else
{
if(count == 3)
array[i][j] = 0;
}
}
}
display();
}
But the canvas just clears itself on the first "cycle" or whatever you want to call it.
For those who don't know the game (aka live under a rock )
http://www.math.com/students/wonders/life/life.html
Maybe someone sees where I went wrong? I feel like it's blatant. But oh well, just wanted to mess with it.