Last active
January 1, 2016 23:39
-
-
Save maze1230/8218108 to your computer and use it in GitHub Desktop.
ライフゲームC言語バージョン
とりあえず読み込むファイルは1が生きてて0が死んでる
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#define SIZE 20 | |
void write_life(int life[][SIZE]){ | |
int i,j; | |
int life_temp[SIZE][SIZE]; | |
for(i = 0;i < SIZE;i++){ | |
for(j = 0;j < SIZE;j++){ | |
life_temp[i][j] = live_or_dead(life,i,j); | |
} | |
} | |
for(i = 0;i < SIZE;i++){ | |
for(j = 0;j < SIZE;j++){ | |
life[i][j] = life_temp[i][j]; | |
} | |
} | |
} | |
int live_or_dead(int life[][SIZE],int h,int w){ | |
int i; | |
int count = 0; | |
if(life[h-1][w] == 1 && h != 0) | |
count++; | |
if(life[h+1][w] == 1 && h != SIZE-1) | |
count++; | |
if(life[h][w-1] == 1 && w != 0) | |
count++; | |
if(life[h][w+1] == 1 && w != SIZE-1) | |
count++; | |
if(life[h-1][w-1] == 1 && h != 0 && w != 0) | |
count++; | |
if(life[h-1][w+1] == 1 && h != 0 && w != SIZE-1) | |
count++; | |
if(life[h+1][w-1] == 1 && h != SIZE-1 && w != 0) | |
count++; | |
if(life[h+1][w+1] == 1 && h != SIZE-1 && w != SIZE-1) | |
count++; | |
if(life[h][w] == 1){ | |
if(count == 2 || count == 3) | |
return 1; | |
else | |
return 0; | |
}else{ | |
if(count == 3) | |
return 1; | |
else | |
return 0; | |
} | |
return 0; | |
} | |
int main(void){ | |
int i=0,j=0; | |
int life[SIZE][SIZE]; | |
char file_name[126]; | |
FILE *fp; | |
scanf("%s",file_name); | |
fp = fopen(file_name,"r"); | |
if(fp == NULL) | |
return -1; | |
for(i = 0;i < SIZE;i++){ | |
for(j = 0;j < SIZE;j++){ | |
fscanf(fp,"%d",&life[i][j]); | |
} | |
} | |
system("clear"); | |
while(1){ | |
for(i = 0;i < SIZE;i++){ | |
for(j = 0;j < SIZE;j++){ | |
if(life[i][j] == 0) | |
printf("□ "); | |
else | |
printf("■ "); | |
} | |
printf("\n\a"); | |
} | |
sleep(1); | |
system("clear"); | |
write_life(life); | |
} | |
fclose(fp); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment