//Project_03 #include "stdafx.h" #include #include #include using namespace std; typedef unsigned int uint; const int W_SIZE =32; // Size of the world. const int DELAY=400;// Delay between generation display void delay(int ms) //delay ms miliseconds { int start=clock(); while(clock()<(start+ms)); } int neighbors(char cg[W_SIZE+2][W_SIZE+2],char i, char j) //Number of living neighbors of cg[i][j] { return cg[i-1][j-1]+cg[i][j-1]+cg[i+1][j-1]+\ cg[i-1][j]+cg[i+1][j]+ \ cg[i-1][j+1]+cg[i][j+1]+cg[i+1][j+1]; } void display(char cg[W_SIZE+2][W_SIZE+2]) //Display the world { system("cls"); for (char i=1;i>(32-j-1); mask>>=1; } } fclose(f); } void record_game(char cg[W_SIZE+2][W_SIZE+2]) //Pack cg to 32 hexa words { cout<<"Enter game file: "; FILE* f; char file_name[80]; do //get file name { scanf("%s",file_name); f=fopen(file_name,"w"); if(f==NULL) cout<<"\nFile not found, try again\n"; } while(f==NULL); uint data; for(int i=0 ; i<32 ; i++) //parse the file { for(int j=0 ; j<32 ; j++) { if(cg[i+1][j+1])data|=1; data<<=1; } fprintf(f,"%x\n",data); } fclose(f); } void play_gol(char cg[W_SIZE+2][W_SIZE+2],int steps) //Play the game. 1 step if steps=0. { char ng[W_SIZE+2][W_SIZE+2]={0}; while(1) // play the game { display(cg); for(char i=1;i3)) //occupied cell {ng[i][j]=0; continue;} //passed away ng[i][j]=cg[i][j]; //no change } memcpy(cg,ng,(W_SIZE+2)*(W_SIZE+2));// copy next generation back to current generation if(steps==0)return; if(kbhit() ) {_getch(); return;} delay(DELAY); // if(kbhit())break; } } int put_menue(void) //get menue selection { printf("1 next generation\n"); printf("2 record to file\n"); printf("3 load from file\n"); printf("4 change delay\n"); printf("5 exit\n"); int s; scanf("%d",&s); return s; } void main(void) { cout<<"Enter game file: "; FILE* f; char file_name[80]; do //get file name { scanf("%s",file_name); f=fopen(file_name,"r"); if(f==NULL) cout<<"\nFile not found, try again\n"; } while(f==NULL); char cg[W_SIZE+2][W_SIZE+2]={0}; //current generation prase_game_file(f,cg); play_gol(cg,1); //play continuously while(1) { switch(put_menue()) { case 5: return; //exit game case 1: play_gol(cg,0);//next generation break; case 2: record_game(cg);//record current generation break; default: printf("Not implemented yet\n"); } } _getch(); }