//Conway's Game of Life #include #include #include #include #define CONWAY_ARRAY 10 //#define num_of_gen 10 ofstream outfile("conway_out.txt"); void load_init(char [CONWAY_ARRAY] [CONWAY_ARRAY]); void step_generation (char [CONWAY_ARRAY] [CONWAY_ARRAY]); void print_grid (char [CONWAY_ARRAY] [CONWAY_ARRAY]); int neighbors(char [CONWAY_ARRAY] [CONWAY_ARRAY], int, int); void copy_array(char [CONWAY_ARRAY] [CONWAY_ARRAY], char [CONWAY_ARRAY][CONWAY_ARRAY]); void main(void) { char life[CONWAY_ARRAY] [CONWAY_ARRAY]; int i; unsigned int seed; int num_of_gen; cout<<"Welcome to Conway's Game Of Life!\n"; cout<<"This will replicate a grid of life forms.\n\n"; cout<<"These are the rules:\n"; cout<<"1. An organism is born in an empty cell when it has\n"; cout<<" exactly three neighbors.\n"; cout<<"2. An organism will die from starvation if it has\n"; cout<<" fewer than two neighbors.\n"; cout<<"3. An organism will die from overcrowding if it has\n"; cout<<" if it has more than three neighbors.\n"; cout<<"4. Otherwise, the organism survives.\n"; cout<<"Please enter a number to initiate the grid(positive integers): "; cin>>seed; cout<<"Please enter the number of generations you would like to see.\n"; cout<<"(use more than 10 to make it interesting): "; cin>>num_of_gen; srand(seed); load_init(life); for(i=1; i<= num_of_gen; i++) { outfile<<"\nGeneration: "<< i <<"\n\n"; print_grid(life); step_generation(life); } outfile.close; } void load_init(char INITIAL[CONWAY_ARRAY] [CONWAY_ARRAY]) { int y,x,z; for(y=0; y= 0) { for(i=y-1;i<=y+1;i++) { if((i >= 0) && (i < CONWAY_ARRAY)) { if(INITIAL[i][x-1]=='*') sum+=1; } } } if((x+1)<(CONWAY_ARRAY)) { for(i=y-1;i <=y+1;i++) { if((i>=0) && (i < CONWAY_ARRAY)) { if(INITIAL[i][x+1]=='*') sum+=1; } } } if((y-1) >= 0) { if(INITIAL[y-1][x] == '*') sum+=1; } if((y+1) < CONWAY_ARRAY) { if(INITIAL[y+1][x]=='*') sum+=1; } return sum; } void copy_array(char INITIAL[CONWAY_ARRAY][CONWAY_ARRAY], char COPY[CONWAY_ARRAY][CONWAY_ARRAY]) { int y,x; for(y=0;y