/*lifegame.c*/ #include <stdio.h> #include <graphics.h> #include <time.h> #include <stdlib.h> #define MAX 60 #define SIZE 5
void initG(); /*图形模式初始化*/ void closeG();/*关闭图形模式*/ void live(int col,int row); void death(int col,int row); void initG() { int gdriver=DETECT,gmode; initgraph(&gdriver,&gmode,"d:\\tc"); cleardevice(); } void closeG() { cleardevice(); settextstyle(1,0,5); setcolor(BLUE); outtextxy(80,50,"Programme Desiged:Y.Z.X"); outtextxy(80,160,"MAIL:yzx_xue@163.com"); outtextxy(200,330,"2006.5"); getch(); closegraph(); } void live(int col,int row) { setcolor(WHITE); setfillstyle(SOLID_FILL,WHITE); bar(SIZE*(col+1)+1,SIZE*(row+1)+1,SIZE*(col+2)-1,SIZE*(row+2)-1); } void death(int col,int row) { setcolor(BLACK); setfillstyle(SOLID_FILL,BLACK); bar(SIZE*(col+1)+1,SIZE*(row+1)+1,SIZE*(col+2)-1,SIZE*(row+2)-1); } void table() { int i; setcolor(RED); for(i=0;i<=MAX;i++) { line(SIZE*(i+1),SIZE,SIZE*(i+1),SIZE*(MAX+1)); line(SIZE,SIZE*(i+1),SIZE*(MAX+1),SIZE*(i+1)); } } void game() { int nrow,ncol,i,resData[MAX][MAX],ori[MAX][MAX]; int count; char str[20]; /*if life is live color is balck*/ /*init the life*/ table(); for(nrow=0;nrow!=MAX;nrow++) for(ncol=0;ncol!=MAX;ncol++) { ori[nrow][ncol]=1; resData[nrow][ncol]=1; } for(i=0;i<=100;i++) { count=0; for(nrow=1;nrow!=MAX;nrow++) for(ncol=1;ncol!=MAX;ncol++) { if(resData[nrow][ncol]==1) live(nrow,ncol); if(resData[nrow][ncol]==0) death(nrow,ncol); count+=resData[nrow][ncol]; } table(); delay(5000); for(nrow=1;nrow<MAX;nrow++) for(ncol=1;ncol<MAX;ncol++) switch((ori[nrow-1][ncol-1]+ori[nrow-1][ncol]+ori[nrow-1][ncol+1] +ori[nrow][ncol-1]+ori[nrow][ncol+1] +ori[nrow+1][ncol-1]+ori[nrow+1][ncol]+ori[nrow+1][ncol+1])) { case 3: resData[nrow][ncol]=1;break; case 2: resData[nrow][ncol]=ori[nrow][ncol]; break; default: resData[nrow][ncol]=0; }/*end switch*/ for(nrow=1;nrow!=MAX;nrow++) for(ncol=1;ncol!=MAX;ncol++) ori[nrow][ncol]=resData[nrow][ncol]; sprintf(str,"%d",count); setcolor(RED); rectangle(450,30,getmaxx(),200); settextstyle(1,0,4); outtextxy(455,40,"LIFE GAME"); outtextxy(455,100,"LIVE:"); setfillstyle(SOLID_FILL,BLACK); bar(550,100,getmaxx()-5,140); outtextxy(550,100,str); } /*end time for*/ } void main() { initG(); game(); getch(); closeG();
} |