Last active
January 1, 2016 00:18
-
-
Save kimitoboku/8065248 to your computer and use it in GitHub Desktop.
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<time.h> | |
#include<stdlib.h> | |
#define H 10 | |
#define W 10 | |
void init(int cell[][H+2][W+2]); | |
void lifegame(); | |
void lifeupdata(int cell[][H+2][W+2],int array1); | |
void display(int cell[][H+2][W+2],int arrya1); | |
void readDataLifegame(char *filename); | |
void read_map(int cell[][H+2][W+2],char *filename); | |
int main(int argc, char *argv[]){ | |
if(*++argv){ | |
if(!strcmp(*argv,"emacs")){ | |
system("emacs -nw -f life"); | |
}else{ | |
readDataLifegame(*argv); | |
} | |
}else{ | |
lifegame(); | |
} | |
return 0; | |
} | |
void init(int cell[][H+2][W+2]){ | |
int i,j; | |
srand(time(NULL)); | |
for(i=1;i<H+1;i++){ | |
for(j=1;j<W+1;j++){ | |
cell[0][i][j] = rand()%2; | |
} | |
} | |
} | |
void display(int cell[][H+2][W+2],int array1){ | |
int i,j; | |
// system("clear"); | |
for(i=1;i<H+1;i++){ | |
for(j=1;j<W+1;j++){ | |
if(cell[array1][i][j]){ | |
printf("+"); | |
}else{ | |
printf(" "); | |
} | |
} | |
puts(""); | |
} | |
} | |
void lifeupdata(int cell[][H+2][W+2],int array1){ | |
int i,j,count=0; | |
for(i=1;i<H+1;i++){ | |
for(j=1;j<W+1;j++){ | |
count = cell[array1][i-1][j-1]+cell[array1][i-1][j]+cell[array1][i-1][j+1]+cell[array1][i][j-1]+cell[array1][i][j+1]+cell[array1][i+1][j-1]+cell[array1][i+1][j]+cell[array1][i+1][j+1]; | |
if(cell[array1][i][j]){ | |
if(count==3||count==2){ | |
cell[array1^1][i][j] = 1; | |
}else{ | |
cell[array1^1][i][j] = 0; | |
} | |
}else{ | |
if(count==3){ | |
cell[array1^1][i][j] = 1; | |
}else{ | |
cell[array1^1][i][j] = 0; | |
} | |
} | |
} | |
} | |
} | |
void lifegame(){ | |
int cell[2][H+2][W+2]; | |
int first=0; | |
init(cell); | |
while(1){ | |
sleep(1); | |
display(cell,first); | |
lifeupdata(cell,first); | |
first = first^1; | |
} | |
} | |
void read_map(int cell[][H+2][W+2],char *filename){ | |
FILE *fp; | |
int line[W]; | |
fp = fopen(filename,"r"); | |
if(fp == NULL){ | |
puts("Can not Open File"); | |
exit(1); | |
} | |
int i=1,j,k; | |
int t; | |
for(i=1;i<H+1;i++){ | |
for(k=0;k<W;k++){ | |
fscanf(fp,"%d",&line[k]); | |
} | |
for(j=1;j<W+1;j++){ | |
cell[0][i][j] = line[j-1]; | |
} | |
} | |
} | |
void readDataLifegame(char *filename){ | |
int cell[2][H+2][W+2]; | |
int first=0; | |
read_map(cell,filename); | |
while(1){ | |
sleep(1); | |
display(cell,first); | |
lifeupdata(cell,first); | |
first = first^1; | |
} | |
} |
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
0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 | |
0 0 0 1 1 1 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 | |
0 0 0 0 0 0 0 0 0 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment