Search code examples
cconsole-applicationconways-game-of-lifecellular-automata

Why do my gliders never delete and instead remain in the bottom of my screen in my Game of Life sim?


I am attempting to code a console based Conway's game of life simulator but for some reason when my gliders reach the bottom of the screen they turn into a stationary 2*2 square that never dissapears, I have no idea why they are there or what is causing it. To see them you might need to wait five or six generations.

code:

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#ifdef __unix__
# include <unistd.h>
#elif defined _WIN32
# include <windows.h>
#define sleep(x) Sleep(1000 * (x))
#endif

int sizeX = 20;
int sizeY = 20;

int grid[20][20] = {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
                    {0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};

int count_nbr(int grid[sizeX][sizeY], int x, int y){
 int n_count = 0;
 if (grid[x-1][y-1]==1)
     n_count++;

 if(grid[x-1][y]==1)
     n_count++;

 if(grid[x-1][y+1]==1)
     n_count++;

 if(grid[x][y-1]==1)
     n_count++;

 if(grid[x][y+1]==1)
     n_count++;

 if(grid[x+1][y-1]==1)
     n_count++;

 if(grid[x+1][y]==1)
     n_count++;

 if(grid[x+1][y+1]==1)
     n_count++;

 return n_count;
}

int main(void){
 int neighbour_count[sizeX][sizeY];
    int x,y,iterations;

 for(iterations=0;iterations<500;iterations++){
  system("cls"); //Clear screen
  for(x=0;x<sizeX;x++){
    printf("\n");
    for(y=0;y<sizeY;y++){
     if(grid[x][y]==1){
      printf("@");
             }
    else{
      printf(" ");
    }
    neighbour_count[x][y] = count_nbr(grid,x,y);
         }
  }
                    
  for(x=0;x<sizeX;x++){
     for(y=0;y<sizeY;y++){
        if(neighbour_count[x][y] < 2 || neighbour_count[x][y] > 3)
     grid[x][y] = 0;
    else if(neighbour_count[x][y] == 3)
     grid[x][y]=1;
   }
  }
  printf("\n");
 }
}

Solution

  • There may be bugs in your code, but the behavior you observe is not one. Instead, it's exactly what you'd expect to see in Conway's Game of Life played on a finite grid, with boundary conditions specifying that all cells outside the grid edges are to be always treated as dead:

    1:        2:        3:        4:        5:        6:
    ......    ......    ......    ......    ......    ......
    ...X..    ..X...    ...X..    ......    ......    ......
    .X.X.. -> ...XX. -> ....X. -> ..X.X. -> ....X. -> ...XX.
    ..XX..    ..XX..    ..XXX.    ...XX.    ...XX.    ...XX.
    ######    ######    ######    ######    ######    ######
    

    Here, X denotes a live cell, . denotes a dead cell that could become live, and # denotes a cell outside the grid boundary that is always treated as dead.

    In step 3 above, one of the always-dead cells marked with # has three live neighbors. If it was a normal dead cell, it would become live in step 4 and the glider would continue to propagate. But because it can't become live, what actually appears in step 4 is a four-cell pattern consisting of a glider with one live cell missing. This pattern then evolves in two steps into a 2x2 block (and stays that way, as the block is a stable still life).