Skip to content

Instantly share code, notes, and snippets.

@sinannar
Created April 23, 2012 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sinannar/2472341 to your computer and use it in GitHub Desktop.
Save sinannar/2472341 to your computer and use it in GitHub Desktop.
game of life basic simulation
/*
example video link : http://www.youtube.com/watch?v=XcuBvj0pw-E
*/
/*
* FILE NAME : HW05_091044005_PART_3.c
*
* CREADET BY : Sinan NAR
* CREATION DATE : 08/04/2011
*
* DESCRIPTION :
* The Game of Life is a game that inventet by John H. Conway,is supposed to model the genetic laws for
* birth,survival and death.There is three condition to live or die or born too
* if space are has three neighbours,the time of next generation you will see a new organism there
* if full space with an organism has two or three neigbours,in next genearion,you will see same organism there
* if any organism has just one neighbour it will be die cause of loneless
* if any organism has four or more than four neighbours this organizm will die cause of crowded
*
*
*
* REFERENCES :
* [1] Problem Solving and Program Design in C
* [2] Slide of the course CS102
* [3] The paper based source of course CS108
*
*
* IMPORTANT NOTICE :
* Here is the program for two different way of compiling and running.
* On the website of moodle,teacher give us the testplane to test our program
* if you put two slash the start of line 34 it will compiling as random array 25 to 25
* if you just compile this program like that this will compiling as testplane array 25 to 40
* i used #ifdef and #else for this program and Umut DEMIRTAS who is our laboratuary teacher give me this idea and help me about it and using about it
*
*/
#include <stdio.h> // include printf,scanf and so on...
#include <time.h> // include srand and so on.
#include <string.h>
//#define TEST_PLANE // It will compiling as tesplane,if you want to see random values just print two slash before the symbol of #(square)
#define FULL 'X' //organzm definition for normal
#define EMPTY ' ' //space definition for normal
#define ROW 25 //row size of array
#ifdef TEST_PLANE //If TEST_PLANE defines
#define COLOUMN 40 //coloumn value is 40
#define NUMBER1 82 //number1 is 82 for beauty
#define NUMBER2 80 //number2 is 80 for beauty
#else
#define COLOUMN 25 //coloumn value is 40
#define NUMBER1 51 //number1 is 82 for beauty
#define NUMBER2 50 //number2 is 80 for beauty
#endif
#define WAIT 0.05 //waiting time
#define LOOPTURN 100 //how many time does the game turn
/* FOR TEST PLANE */
#define LIVE 'X' //orgamism definition for test plane
#define DEAD ' ' //death definition or empty area defintion for test plane
/* FOR TEST PLANE */
void fill_first(char [ROW][COLOUMN]); // fill first screen as random
void print_on_screen(char [ROW][COLOUMN]); //Print screen any array
void die_survive_born(char [ROW][COLOUMN],char [ROW][COLOUMN]); //decide die or survive or born for each element of array
void copy_to_original(char [ROW][COLOUMN],char [ROW][COLOUMN]) ; //copy the process to the original array that is used for printing on screen
void make_it_empty(char [ROW][COLOUMN]); //make an array with whitespace character as space
int control_neighbours(char [ROW][COLOUMN],int satir,int sutun); //control the how many neighbours is this element of array that is input parameter
void wait(double); //for waiting on screen
/* FOR TEST PLANE */
void createTestPlane(char [ROW][COLOUMN]); //fill array in order that given in test plane
int main(void)
{
char game_of_life[ROW][COLOUMN], //array that is main array to use
process[ROW][COLOUMN]; //array that is use for process
int i;
getchar();
make_it_empty(game_of_life); //make our array empty first
#ifdef TEST_PLANE
createTestPlane(game_of_life); //here if the TEST_PLANE define ,this function will be in process and fillinf array in order
#else
fill_first(game_of_life); //if the TEST_PLANE is not define this function will be in process and filling array as random
#endif
print_on_screen(game_of_life); //printing as first
for(i=0;i<LOOPTURN;++i) // loop for hundered time
{
wait(WAIT); //wait on screen as second that define as WAIT
system("clear");//clear the screen
die_survive_born(game_of_life, process); //make decisions and make new array from game_of_life to process
copy_to_original(game_of_life, process); //copy process to game_of_life
make_it_empty(process); //make process empty with space character
print_on_screen(game_of_life); //print array on screen
}
return (0); //no error should send to operation system
}
/*
* waiting function
* when we use it anywhere
* these screen should be freeze seconds
*/
void wait(double seconds)
{
clock_t endwait;
endwait = clock() + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {} /*waiting*/
}
/* FOR TEST PLANE */
void createTestPlane(char plane[ROW][COLOUMN])
{
int i,j;
for(i=0; i<ROW; ++i)
{
for(j=0; j<COLOUMN; ++j)
{
plane[i][j]=DEAD;
}
}
plane[6][1]= LIVE;
plane[6][2]= LIVE;
plane[7][1]= LIVE;
plane[7][2]= LIVE;
plane[6][11]= LIVE;
plane[7][11]= LIVE;
plane[8][11]= LIVE;
plane[5][12]= LIVE;
plane[9][12]= LIVE;
plane[4][13]= LIVE;
plane[4][14]= LIVE;
plane[10][13]= LIVE;
plane[10][14]= LIVE;
plane[7][15]= LIVE;
plane[5][16]= LIVE;
plane[9][16]= LIVE;
plane[6][17]= LIVE;
plane[7][17]= LIVE;
plane[8][17]= LIVE;
plane[7][18]= LIVE;
plane[4][21]= LIVE;
plane[5][21]= LIVE;
plane[6][21]= LIVE;
plane[4][22]= LIVE;
plane[5][22]= LIVE;
plane[6][22]= LIVE;
plane[3][23]= LIVE;
plane[7][23]= LIVE;
plane[2][25]= LIVE;
plane[3][25]= LIVE;
plane[7][25]= LIVE;
plane[8][25]= LIVE;
plane[4][35]= LIVE;
plane[4][36]= LIVE;
plane[5][35]= LIVE;
plane[5][36]= LIVE;
}
/*
* Fill any array with random character as ' ' or 'X'
* input paramters: just one array
* output parameters : these array is also output parameters
*
*/
void fill_first(char array1[ROW][COLOUMN])
{
int i,j,k;
srand ( time ( NULL ) ); //randoms should be different
for(i=1; i<ROW-1; ++i)
{
for(j=1; j<COLOUMN-1; ++j)
{
k=rand() % 2; //random value should be 1 or 0
if(k == 1) //if it is 1 ,fill it with 'X'
{
array1[i][j] = FULL;
}
else if(k == 0) //if random value is 0 fill it with ' '
{
array1[i][j] = EMPTY;
}
}
}
}
/*
* that is function prints array on screen
* input parameters : an array
* output parameters : same array
*/
void print_on_screen(char array1[ROW][COLOUMN])
{
int i,j,k;
/*this print NUMBER1 time _ for esthetic */
for(k=0; k<NUMBER1; ++k)
{
printf("_");
}
/*its for esthetic too*/
printf("\n");
/*print all elements of array on screen estheticly*/
for(i=0; i<ROW; ++i)
{
printf("|");
for(j=0; j<COLOUMN; ++j)
{
printf("%2c", array1[i][j]);
}
printf("|\n");
}
printf("|");
/*this print NUMBER2 time _ for esthetic */
for(k=0; k<NUMBER2; ++k)
{
printf("_");
}
/*its for esthetic too*/
printf("|");
printf("\n");
}
/*
* this function check the situation of neighbours and how much neighbours
* input parameters: an array and two integer
* output parameters: it is an integer to know how many integer of an array that is entered to function as parameters
*/
int control_neighbours(char array1[ROW][COLOUMN],int satir,int sutun)
{
int i,j,a=0;
i=satir;
j=sutun;
if(array1[i-1][j-1] == FULL) //left up part of element of array
{
++a;
}
if(array1[i-1][j] == FULL) //up part of element of array
{
++a;
}
if(array1[i-1][j+1] == FULL) //right up part of element of array
{
++a;
}
if(array1[i][j-1] == FULL) //left part of element of array
{
++a;
}
if(array1[i][j+1] == FULL) //right part of element of array
{
++a;
}
if(array1[i+1][j-1] == FULL) //left down part of element of array
{
++a;
}
if(array1[i+1][j] == FULL) //down part of element of array
{
++a;
}
if(array1[i+1][j+1] == FULL) //right down part of element of array
{
++a;
}
/*calculate conclusingly how many neighbours does our array elemts have with increasing a in each if body*/
return a; //return how many neighbours our element of array
}
/*
* This funtion decide to die or survive or born
* input parameter:two char arrays
* oupput parameter:also these arrays are output
* Program decides for the situation and write it to second array
*/
void die_survive_born(char array1[ROW][COLOUMN], char array2[ROW][COLOUMN])
{
int i,j,k;
for(i=1; i<ROW-1; ++i)
{
for(j=1; j<COLOUMN-1; ++j)
{
if(array1[i][j] == EMPTY) //check if available born situation
{
k=control_neighbours(array1, i, j);
if(k==3)
{
array2[i][j] = FULL; //there is born a new organism
}
else
{
array2[i][j] = EMPTY; //no born there
}
}
else if(array1[i][j] == FULL) //die or survive control situation
{
k=control_neighbours(array1, i, j);
if(k>3 || k<2)
{
array2[i][j] = EMPTY; //there is death
}
else if( k == 3 || k == 2)
{
array2[i][j] = FULL; //there is someone who is survive
}
}
}
}
}
/*
* copy new generation from the array2 to array1
* inputs:array1 and array2 that can be changed
* outputs:array1 and array2 and thay already changed by copying process
*/
void copy_to_original(char array1 [ROW][COLOUMN],char array2 [ROW][COLOUMN])
{
int i,j;
for(i=0; i<ROW; ++i)
{
for(j=0; j<COLOUMN; ++j)
{
array1[i][j] = array2[i][j];//there is a process to coph all element of array2 to array1
}
}
}
/*
* fill any arrys element with space character
* input:an array
* output:an array behaviors like pointer
*/
void make_it_empty(char array2[ROW][COLOUMN])
{
int i,j;
for(i=0; i<ROW; ++i )
{
for(j=0; j<COLOUMN; ++j )
{
array2[i][j] = EMPTY;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment