Created
April 23, 2014 08:34
-
-
Save kimitoboku/11207185 to your computer and use it in GitHub Desktop.
lifegame
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
/* | |
学籍番号,氏名:B131820,川上 賢十 | |
*/ | |
#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(int loop); | |
void lifeupdata(int cell[][H+2][W+2],int array1); | |
void display(int cell[][H+2][W+2],int arrya1); | |
void readDataLifegame(char *filename,int loop); | |
void read_map(int cell[][H+2][W+2],char *filename); | |
int density(int cell[][H+2][W+2],int x,int y,int array1); | |
int main(int argc, char *argv[]){ | |
int loop=0,i; | |
char *p; | |
for(i=1;i<argc;i++){ | |
p=argv[i]; | |
if(*p=='-'){//Check Optrions | |
p++; | |
switch(*p){ | |
case 'h': | |
printf("Usage: this_command [options] [filename]\nOpions:\n -h :Display Information\n -a :Auto Step\n other options:ignore\nFilename:\n Map Data File (%d*%d)\n",H,W); | |
return 0; | |
break; | |
case 'a': | |
loop=1; | |
break; | |
} | |
}else{ | |
readDataLifegame(p,loop); | |
} | |
} | |
lifegame(loop); | |
return 0; | |
} | |
void init(int cell[][H+2][W+2]){ | |
/* | |
引数: | |
cell[][H+2][W+2]:マップデータを格納する配列 | |
関数の説明: | |
受け取った配列cellに0一旦全てに格納し、その後マップデータをランダムで生成し代入する | |
*/ | |
int i,j; | |
srand(time(NULL)); | |
for(i=0;i<H+2;i++){ | |
for(j=0;j<W+2;j++){ | |
cell[0][i][j] = 0; | |
cell[1][i][j] = 0; | |
} | |
} | |
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){ | |
/* | |
引数: | |
cell[][H+2][W+2] :表示するマップデータを格納した配列 | |
array1 :どちらの面を表示したらよいのかのフラグ | |
関数の説明: | |
現在のマップデータを読み込みそのデータを表示する | |
*/ | |
int i,j; | |
for(i=1;i<H+1;i++){ | |
for(j=1;j<W+1;j++){ | |
if(cell[array1][i][j]){ | |
printf("■"); | |
}else{ | |
printf("□"); | |
} | |
} | |
puts(""); | |
} | |
} | |
int density(int cell[][H+2][W+2],int x,int y,int array1){ | |
/* | |
引数: | |
cell[][H+2][W+2]:現在のマップを格納している配列 | |
x :指定するx座標 | |
y :指定するy座標 | |
array1 :配列のどちらの面を見ればいいかのフラグ | |
関数の説明: | |
指定された座標(x,y)のまわりのセルの生存を確認する | |
戻り値: | |
生存が確認された数 | |
*/ | |
return cell[array1][x-1][y-1]+cell[array1][x-1][y]+cell[array1][x-1][y+1]+cell[array1][x][y-1]+cell[array1][x][y+1]+cell[array1][x+1][y-1]+cell[array1][x+1][y]+cell[array1][x+1][y+1]; | |
} | |
void lifeupdata(int cell[][H+2][W+2],int array1){ | |
/* | |
引数: | |
cell[][H+2][W+2]:現在のマップを格納しているデータ | |
array1 :配列のどちら面を見ればいいのかのデータ | |
関数の説明: | |
配列cellに格納されている現在のマップのデータを読み、その裏側に次のマップのデータを格納する | |
*/ | |
int i,j,count=0; | |
for(i=1;i<H+1;i++){ | |
for(j=1;j<W+1;j++){ | |
count = density(cell,i,j,array1); | |
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 loop){ | |
/* | |
引数: | |
loop:自動でループを行うかどうかの判定 | |
関数の説明: | |
マップデータ無しのライフゲームを行う関数 | |
*/ | |
int cell[2][H+2][W+2]; | |
int first=0; | |
init(cell); | |
while((loop == 1)?1:(getchar()!=EOF)){ | |
if(loop){ | |
sleep(1); | |
puts(""); | |
} | |
display(cell,first); | |
lifeupdata(cell,first); | |
first = first^1; | |
} | |
} | |
void read_map(int cell[][H+2][W+2],char *filename){ | |
/* | |
引数: | |
cell[][H+2][W+2]:マップを読み出しでそのデータを格納する予定の配列 | |
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=0;i<H+2;i++){ | |
for(j=0;j<W+2;j++){ | |
cell[0][i][j] = 0; | |
cell[1][i][j] = 0; | |
} | |
} | |
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 loop){ | |
/* | |
引数: | |
filename:読み込むマップが格納されたファイルの名前 | |
loop :AutoStepするかどうか | |
関数の説明: | |
マップデータのファイルをしていしそれに応じたライフゲームを行う | |
*/ | |
int cell[2][H+2][W+2]; | |
int first=0; | |
read_map(cell,filename); | |
while((loop == 1)?1:(getchar()!=EOF)){ | |
if(loop){ | |
sleep(1); | |
puts(""); | |
} | |
display(cell,first); | |
lifeupdata(cell,first); | |
first = first^1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment