/* life.c Function: ライフゲーム ver0.1 Author: Ken Date: 03/08/25 URL: http://blue.ribbon.to/~kenweb/ mail: kenweb@s8.xrea.com */ #include <stdio.h> #include <time.h> #include <stdlib.h> #define N 20 #define MAX 300 int bord1[N][N]; int bord2[N][N]; int x=1; void print_bord(); int search1(); int search2(int p,int q); void sonyu(); int main() { int p,q; sonyu(); print_bord(); for(x=2;x<MAX+1;x++){ search1(); for(p=0;p<N;p++){ for(q=0;q<N;q++) bord1[p][q]=bord2[p][q]; } getchar(); print_bord(); } } void print_bord() { int p,q,people=0; system("cls"); printf("%d世代目\n",x); for(p=0;p<N;p++){ printf("\t"); for(q=0;q<N;q++){ if(bord1[p][q]==1) { printf("●"); people++; } else printf("−"); } printf("\n"); } printf("個体数: %d\n",people); } int search1() { int s,t,kazu; for(s=0;s<N;s++){ for(t=0;t<N;t++){ kazu=search2(s,t); if((bord1[s][t]==0)&&(kazu==3)) bord2[s][t]=1; else if (bord1[s][t]==1){ if((kazu==2)||(kazu==3)) bord2[s][t]=1; else bord2[s][t]=0; } } } } int search2(int p,int q) { int a,s,t,k=0; for(s=p-1;s<p+2;s++){ for(t=q-1;t<q+2;t++){ if(((0<=s)&&(s<=N))&&((0<=t)&&(t<=N))){ if((p!=s)||(q!=t)){ a = bord1[s][t]; if(a==1) k++; } } } } return(k); } void sonyu() { int m,n,a; srand((unsigned)time(NULL)); for(m=0;m<N;m++){ for(n=0;n<N;n++){ a=rand()%2; bord1[m][n]=a; bord2[m][n]=0; } } }