#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#define FieldSize 20
/*#define TimeMax 200*/
/*フィールドの初期化 (ファイルの読み込み)*/
void init_field(int field[FieldSize][FieldSize], char *fname){
int i,j;
FILE *fp;
if ((fp=fopen(fname,"r"))==NULL){
printf("init_field(): Cannot open \"%s\"\n", fname);
exit(1);
}
for (i=0; i<FieldSize; i++) {
for(j=0; j<FieldSize; j++) {
fscanf(fp, "%d ", &field[i][j]);
}
fscanf(fp, "\n");
}
fclose(fp);
}
int around_count(int I, int J, int field[FieldSize][FieldSize]){
int i,j;
int sum = 0;
for(i=-1; i<=1; i++){
for(j=-1 ;j<=1; j++){
if (I+i < 0) continue;
if (J+j < 0) continue;
if (I+i >= FieldSize) continue;
if (J+j >= FieldSize) continue;
if (i==0 && j==0) continue;
if (field[I+i][J+j]==1)
sum++;
}
}
return sum;
}
void update_field(int field[FieldSize][FieldSize]){
int i,j;
int around;
int new_field[FieldSize][FieldSize];
for(i=0; i<FieldSize; i++){
for(j=0; j<FieldSize; j++){
new_field[i][j] = 1;
around = around_count(i,j,field);
if (field[i][j]==0 && around==3) continue;
if (field[i][j]==1 && (around==3 || around==2)) continue;
new_field[i][j] = 0;
}
}
memcpy((void*)field, (void*)new_field, sizeof(new_field));
}
void display(int field[FieldSize][FieldSize]){
int i,j;
for (i=0; i<FieldSize; i++) {
for (j=0; j<FieldSize; j++) {
if (field[i][j]==1) {
printf("@ ");
} else {
printf("_ ");
}
}
printf("\n");
}
}
int main(int argc, char *argv[]){
int t;
int field[FieldSize][FieldSize];
if(argc!=2){
printf("実行方法 : ./a.out 初期状態のファイル\n");
exit(1);
}
init_field(field, argv[1]);
system("clear");
for (;;) {
system("clear");
update_field(field);
display(field);
/*usleep(200000);*/
getchar();
}
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPG1lbW9yeS5oPgoKI2RlZmluZSBGaWVsZFNpemUgMjAKLyojZ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