FANDOM


/* MADE BY Kevin Duke and Tomi Babalola*/
/* Program 3 -- Conway's GAME OF LIFE in C */
/* 11/30/09 */
 
#include <stdio.h>
#define ARRAYSIZE 25
 
void printfunction(int printarray[ARRAYSIZE+2][ARRAYSIZE+2])
{
int x, y;
char character;
 
	for (y=1; y <= ARRAYSIZE; y++)
	   {
		 for (x=1; x <= ARRAYSIZE; x++)
		 {/*Search and replace 1's and 0's with X's and ' 's */
 
		   if (printarray[x][y] == 1)
			  character = 'X';
		   else
			  character = ' ';
       printf("%c", character);
		 }
		 printf("\n");
     }
 
}
 
 
int main(void)
{
int x, y;
int jesus;
int generation[ARRAYSIZE+2][ARRAYSIZE+2];
int nextgeneration[ARRAYSIZE+2][ARRAYSIZE+2];
char generation_C[ARRAYSIZE][ARRAYSIZE];
char run;
 
FILE *infilep;
 
infilep = fopen("seed.txt", "r");
 
/*Loop to enter zeros into all locations, including the borders*/
	for (y=0; y <= ARRAYSIZE+1; y++)
	{
	  for (x=0; x <= ARRAYSIZE+1; x++)
	  {
	     generation[x][y] = 0;
	     nextgeneration[x][y] = 0;
	  }
	}
/*scan in the input array*/
for (y=1; y <= ARRAYSIZE; y++)
   {
   for (x=1; x <= ARRAYSIZE+1; x++)
      {
      fscanf(infilep, "%c", &generation_C[x][y]);
 
 
      /*Search and replace X and space with 1 or 0*/
      {
         if (generation_C[x][y] == '\n' )
           generation_C[x][y] = ' ';
         else if (generation_C[x][y] == 'X' || generation_C[x][y] == 'x')
           generation[x][y] = 1;
         else
           generation[x][y] = 0;
      }
      }
 
   }
 
printf("To quit, enter 'Q'. Otherwise hit Enter to run the next generation.\n");
printf("The initial seed population:\n");
/* Print out generation array*/
printfunction(generation);
 
/*Loop until Q is entered*/
scanf("%c", &run);
while(run != 'Q' && run != 'q')
{
	/*while loop that goes through the whole array up to ARRAYSIZE*/
	for (y=1; y <= ARRAYSIZE; y++)
	{
	  for (x=1; x <= ARRAYSIZE; x++)
	  {
	   /*Check to see what the living neighbors are so Jesus can determine fate*/
	   jesus = 0;
	   if (generation[x][y] == 1)
	    {
       if (generation[x+1][y] == 1)
			jesus++;
		 if (generation[x-1][y+1] == 1)
			jesus++;
		 if (generation[x][y+1] == 1)
			jesus++;
		 if (generation[x+1][y+1] == 1)
			jesus++;
       if (generation[x-1][y] == 1)
			jesus++;
       if (generation[x-1][y-1] == 1)
			jesus++;
       if (generation[x][y-1] == 1)
			jesus++;
       if (generation[x+1][y-1] == 1)
			jesus++;
	    }
 
       /*Jesus will now judge whether the cell should live or die*/
 
         if (jesus < 2 || jesus > 3)
           nextgeneration[x][y] = 0;
 
         if (jesus == 2 || jesus == 3 )
           nextgeneration[x][y] = 1;
 
 
      /*Now check dead cells so Jesus can see if they are worthy to be reincarnated*/
         jesus = 0;
         if (generation[x][y] == 0)
         {
          if (generation[x+1][y] == 1)
            jesus++;
          if (generation[x-1][y+1] == 1)
            jesus++;
          if (generation[x][y+1] == 1)
            jesus++;
          if (generation[x+1][y+1] == 1)
            jesus++;
          if (generation[x-1][y] == 1)
            jesus++;
          if (generation[x-1][y-1] == 1)
            jesus++;
          if (generation[x][y-1] == 1)
            jesus++;
          if (generation[x+1][y-1] == 1)
            jesus++;
         }
 
       /*Jesus will reincarnate the cells*/
         if (jesus == 3)
         nextgeneration[x][y] = 1;
        }
 
	 }
 
/*generation gets nextgeneration*/
 
 for (y=1; y <= ARRAYSIZE; y++)
   {
   for (x=1; x <= ARRAYSIZE; x++)
      generation[x][y] = nextgeneration[x][y];
   }
 
/* Print out generation array*/
 printfunction(generation);
 scanf("%c", &run);
 }
 fclose(infilep);
 return 0;
}