#include #include #define COLS0 41 #define COLS 40 #define LINES 30 char alive='o'; char dead =' '; char now[LINES][COLS0]= { " ", " ", " ", " o ", " o ", " o ", " o ", " ooooo o ", " ", " ", " ", " o ", " o ", " ooo o o ", " oooooooooooooooo ", " ", " ", " ", " ", " ", " ", " ", " ooooo ", " ", " ", " ", " ", " ", " ", " " }; char nxt[LINES][COLS0]; int setnxt() { int x,y,i,j,jb,ib,je,ie,alivect; int ylast=LINES-1; int xlast=COLS-1; for(y=0; ylast>=y; y++){ for(x=0; xlast>=x; x++){ jb = y-1; je = y+1; ib = x-1; ie = x+1; if(y==0) {jb = 0;} if(y>ylast){je = ylast;} if(x==0) {ib = 0;} if(x>xlast){ie = xlast;} alivect=0; for(j=jb; je>=j; j++){ for(i=ib; ie>=i; i++){ if (now[j][i]==alive) alivect++; } } if(now[y][x]==alive){ alivect--; if(alivect==2 || alivect==3) { nxt[y][x]=alive; } else { nxt[y][x]=dead; } } else{ if(alivect==3) {nxt[y][x]=alive;} else {nxt[y][x]=dead;} } }/* x */ nxt[y][COLS0-1]='\0'; }/* y */ return 0; } int knjdisp() { int x,y; char jalive[]="● "; //char jdead[] ="○ "; char jdead[] =" "; int ylast=LINES-1; int xlast=COLS-1; printf("\e[0;0H"); /* カーソルを左上に */ for(y=0; ylast>=y; y++){ for(x=0; xlast>=x; x++){ if(now[y][x]==alive) printf(jalive); else printf(jdead); } printf("|\n"); } for(x=0; xlast>=x; x++){ printf("窶 "); } printf("\n"); } int main() { int ct,y; for(ct=0;1000>ct;ct++){ /*printf("%c[0;0H",(char)0x1b);*/ knjdisp(); if( setnxt() ) return 1; for(y=0;LINES>y;y++){ strcpy(now[y],nxt[y]); } printf("世代数:%d\n",ct); printf("終了するには [Ctrl]+[c] を押してください。\n"); printf(" "); usleep(50000); /* 0.5sec usleep :マイクロ秒(1/1000000秒)*/ /*scanf("%c", &a);*/ } return 0; }