The load function has already set the initial array.
The code I tried to implement in evolve function is this: its giving me a wrong output....can anyone tell where my logic is wrong pls:
Code:
/* Evolve a life arena by one generation. The array arena should
* have length width with each entry representing a column of
* cells. Columns that are empty should be NULL when the function is called
* and on return. Columns that have some occupied cells should be a
* malloc'ed character array of length height. Individual cells that
* are occupied should have their RH bit = 1 and empty cells should have
* their RH bit = 0.
*/
void
evolve(char **arena, int width, int height)
{
int x,y;
int i=0;
for(x=0;x<width;x++)
if(arena[x]!=NULL)
for(y=0;y<height;y++) {
if(arena[x][y]!= NULL)
arena[x][y] <<= 1;
else
arena[x][y] = arena[x][y] & 0;
//printf("\narena = %d %d %d", arena[x][y], x,y);
}
for(x=0;x<width;x++)
for(y=0;y<height;y++) {
int neighbours = 0;
// if the column cell is occupied
if(arena[x]!=0) {
// printf("\nx = %d y = %d", x,y);
// getchar();
if(arena[x-1]!=0) {
// printf("did this enter");
// getchar();
neighbours+= WAS_OCCUPIED_ANY(arena, x-1,y);
neighbours+= WAS_OCCUPIED_ANY(arena, x-1,y-1);
neighbours+= WAS_OCCUPIED_ANY(arena, x-1,y+1);
// printf("neighbours = %d", neighbours);
}
if(arena[x+1]!=0) {
// printf("did this enter\n");
// getchar();
neighbours+= WAS_OCCUPIED_ANY(arena, x+1,y);
neighbours+= WAS_OCCUPIED_ANY(arena, x+1,y-1);
neighbours+= WAS_OCCUPIED_ANY(arena, x+1,y+1);
// printf("neighbours = %d\n", neighbours);
}
neighbours+= WAS_OCCUPIED_ANY(arena, x,y-1);
neighbours+= WAS_OCCUPIED_ANY(arena, x,y+1);
if(neighbours==3 || neighbours==2 || IS_OCCUPIED(arena,x,y) ) {
arena[x][y]|=1;
} else if (neighbours > 3 || neighbours < 2) {
arena[x][y]=0; int check = 0;
for(i = 0;i < height; i++) {
if(!(arena[x][i]==0)) {
check = 1;
}
if(check == 0) {
free(arena[x]);
}
}
}
} else if(arena[x]==0) { // if the column cells are not occupied
if(arena[x-1]!=0) {
neighbours+= WAS_OCCUPIED_ANY(arena, x-1,y);
neighbours+= WAS_OCCUPIED_ANY(arena, x-1,y-1);
neighbours+= WAS_OCCUPIED_ANY(arena, x-1,y+1);
}
if(arena[x+1]!=0) {
neighbours+= WAS_OCCUPIED_ANY(arena, x+1,y);
neighbours+= WAS_OCCUPIED_ANY(arena, x+1,y-1);
neighbours+= WAS_OCCUPIED_ANY(arena, x+1,y+1);
}
if(neighbours==3 || neighbours==2) {
arena[x] = malloc(height);
for(i=0;i<height;i++)
arena[x][i]=0;
arena[x][y]|=1;
}
}