/* ライフゲーム(1) */ #include #include #define COLS0 41 #define COLS 40 #define LINES 20 char now[LINES][COLS0]= { " ", " ", " ", " ", " ooooo ", " ", " ", " ", " o ", " o ", " ooo ", " oooooo ", " oooooo ", " oo ", " ", " ", " ", " ooo ", " ", " " }; char nxt[LINES][COLS0]; int setnxt() { int x,y,i,j,jb,ib,je,ie,alivect; char alive='o'; char dead =' '; 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 main() { int ct,y; for(ct=0;1000>ct;ct++){ /*printf("%c[0;0H",(char)0x1b);*/ printf("\e[0;0H"); /* カーソルを左上に */ for(y=0;LINES>y;y++){ printf("%s*\n",now[y]); } if( setnxt() ) return 1; for(y=0;LINES>y;y++){ strcpy(now[y],nxt[y]); } printf("世代数:%d\n",ct); printf("終了するには [Ctrl]+[c] を押してください。\n"); printf(" "); sleep(1); /*scanf("%c", &a);*/ } return 0; }