/*******************************/ /* ライフゲームプログラム */ /* life_game.cxx */ /********************************/ #include #include #include #include using namespace std; #define N 50 //ライフゲームの世界の大きさ #define MAXT 50 //繰り返しの回数 #define BUFSIZE 3000 /////関数のプロトタイプ宣言////// void initworld(int world[][N]) ; //初期値の読み込み void putworld(int world[][N]) ; //caの状態の出力 void nextt(int world[][N]) ; //次の時刻に更新 int calcnext(int world[][N], int i, int j); //1セルの状態の更新 char fout[20]="life_game.txt"; ofstream ofs(fout); /*****************/ /* main()関数 */ /****************/ int main() { int t; //時刻を表す反復回数のカウンタ int world[N][N]={0}; //ライフゲームの世界 //ca[]への初期値の読み込み initworld(world); ofs<< "t=0\n"; //cout << "t=0\n"; putworld(world); //world[][]の状態の出力 //時間発展の計算 for(t=1;t<50; t++){ nextt(world); //次の時刻に更新 ofs << "t="<< t<< "\n";//時刻の出力 //cout << "t="<< t<< "\n";//時刻の出力 putworld(world); //world[][]の状態の出力 } return 0; } /*******************************************/ /* calcnext()関数: 1セルの状態更新 */ /*******************************************/ int calcnext(int world[][N],int i,int j) { int no_of_one=0; //周囲にある状態1のセル数 int x, y; //状態1のs流を数える for(x=i-1;x<=i+1;x++){ for(y=j-1;y<=j+1;y++) no_of_one+=world[x][y];} no_of_one-=world[i][j];//自分自身はカウントしない if(no_of_one==3) {return 1;} //誕生 else if(no_of_one==2) {return world[i][j];}//依存 else {return 0;}//上記以外 } /*******************************************/ /* nextt()関数: worldの状態更新 */ /*******************************************/ void nextt(int world[][N]) { int nextworld[N][N]={0}; //次世代のworld[] int i, j ; //ルールの適用 for(i=1;i>world[i][j]; }} }