📄 lifegame.c
字号:
//
// LifeGame.c
// Demonstration of life game.
// Compare 2 games (left and right) with different proliferation conditions.
// Snaps of these 2 games are shown below them.
//
// Modified by Likai Mao (Dec 2007)
//
#include <graphics.h>
#define SIZE 60 // number of dots, size of problem
void main()
{
int orgData1[SIZE][SIZE],resData1[SIZE][SIZE];
int orgData2[SIZE][SIZE],resData2[SIZE][SIZE];
int nCount1,nCount2,row,col,i,j,times;
int GraphDriver=DETECT,GraphMode;
int sx=640,sy=480; // screen size
int DOTS=2; // dot size
int nug=10; // nudge
int cxb=sx*2/7-nug, cxe=sx*5/7+nug, cyb=sy*3/7-nug, cye=sy*6/7-nug*3; // centers of 4 blocks (2 big upper, 2 small lower)
int y01b=cyb-(SIZE*(DOTS+1))/2, x01b=cxb-(SIZE*(DOTS+1))/2; // 4 up-left corners
int y01s=cye-SIZE/2, x01s=cxb-SIZE/2;
int y02b=cyb-(SIZE*(DOTS+1))/2, x02b=cxe-(SIZE*(DOTS+1))/2;
int y02s=cye-SIZE/2, x02s=cxe-SIZE/2;
int T=300; // number of simulation steps
int c1=CYAN, c2=MAGENTA, c=LIGHTGRAY;
// initiation
for (i=0;i<SIZE;i++)
for (j=0;j<SIZE;j++)
{
orgData1[i][j]=1;
orgData2[i][j]=1;
}
initgraph(&GraphDriver,&GraphMode," ");
printf("(Press any 6 keys and go 300 steps.)\n");
// draw small frames
setcolor(c1);
rectangle(x01s,y01s,x01s+SIZE-1,y01s+SIZE-1);
setcolor(c2);
rectangle(x02s,y02s,x02s+SIZE-1,y02s+SIZE-1);
// draw big grids
setcolor(c);
for(i=0;i<=SIZE-2;i++)
{
j=i*(DOTS+1);
line(x01b, y01b+j, x01b+(SIZE-2)*(DOTS+1),y01b+j); // 1 horizontal
line(x01b+j, y01b, x01b+j,y01b+(SIZE-2)*(DOTS+1)); // 1 vertical
line(x02b, y02b+j, x02b+(SIZE-2)*(DOTS+1), y02b+j); // 2 hor
line(x02b+j, y02b, x02b+j, y02b+(SIZE-2)*(DOTS+1)); // 2 ver
}
setcolor(c1);
rectangle(x01b,y01b,x01b+(SIZE-2)*(DOTS+1),y01b+(SIZE-2)*(DOTS+1));
setcolor(c2);
rectangle(x02b,y02b,x02b+(SIZE-2)*(DOTS+1),y02b+(SIZE-2)*(DOTS+1));
// main loop, simulation
for (times=1;times<=T;times++)
{
printf("\r%d",times);
for (row=1;row<SIZE-1;row++)
{
for (col=1;col<SIZE-1;col++)
{
nCount1=orgData1[row-1][col-1]+orgData1[row-1][col]
+orgData1[row-1][col+1]+orgData1[row][col-1]
+orgData1[row][col+1]+orgData1[row+1][col-1]
+orgData1[row+1][col]+orgData1[row+1][col+1];
nCount2=orgData2[row-1][col-1]+orgData2[row-1][col]
+orgData2[row-1][col+1]+orgData2[row][col-1]
+orgData2[row][col+1]+orgData2[row+1][col-1]
+orgData2[row+1][col]+orgData2[row+1][col+1];
switch(nCount1)
{
case 3:
resData1[row][col]=1;
putpixel(x01s+col,y01s+row,BLACK);
setfillstyle(SOLID_FILL, BLACK);
bar(x01b+1+(col-1)*(DOTS+1),y01b+1+(row-1)*(DOTS+1),
x01b+(col-1)*(DOTS+1)+DOTS,y01b+(row-1)*(DOTS+1)+DOTS);
break;
case 2:
resData1[row][col]=orgData1[row][col];
break;
default:
resData1[row][col]=0;
putpixel(x01s+col,y01s+row,WHITE);
setfillstyle(SOLID_FILL, WHITE);
bar(x01b+1+(col-1)*(DOTS+1),y01b+1+(row-1)*(DOTS+1),
x01b+(col-1)*(DOTS+1)+DOTS,y01b+(row-1)*(DOTS+1)+DOTS);
}
switch(nCount2)
{
case 3:
resData2[row][col]=1;
putpixel(x02s+col,y02s+row,BLACK);
setfillstyle(SOLID_FILL, BLACK);
bar(x02b+1+(col-1)*(DOTS+1), y02b+1+(row-1)*(DOTS+1),
x02b+(col-1)*(DOTS+1)+DOTS, y02b+(row-1)*(DOTS+1)+DOTS);
break;
case 1: // the only difference between 1 and 2
resData2[row][col]=orgData2[row][col];
break;
default:
resData2[row][col]=0;
putpixel(x02s+col,y02s+row,WHITE);
setfillstyle(SOLID_FILL, WHITE);
bar(x02b+1+(col-1)*(DOTS+1), y02b+1+(row-1)*(DOTS+1),
x02b+(col-1)*(DOTS+1)+DOTS, y02b+(row-1)*(DOTS+1)+DOTS);
}
}
}
for (i=1;i<SIZE-1;i++)
for (j=1;j<SIZE-1;j++)
{
orgData1[i][j]=resData1[i][j];
orgData2[i][j]=resData2[i][j];
}
switch(times)
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
getch(); // stop at some steps
default:
;
}
}
printf(" >> Press any key...");
getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -