生命游戏

来源:互联网 发布:三端口网络的s参数 编辑:程序博客网 时间:2024/06/08 08:57

在一个细胞群中,包含8个方向的邻居,对于一个细胞有如下的规则:

1.如果一个细胞的邻居少于1,则下一时刻他将死亡

2.如果一个细胞的邻居多余4个,则下一时刻他将死亡

3.如果一个细胞的邻居为2或者3,下一时刻他将存活

4.若一个位置没有细胞,而他又3个邻居,则下一时刻将会产生一个新细胞

#include <stdio.h>#include <stdlib.h>#include <ctype.h>#define MAXROW 10#define MAXCOL 25#define DEAD 0#define ALIVE 1int map[MAXROW][MAXCOL],newmap[MAXROW][MAXCOL];int life = 0;void init();int neighbors(int,int);void outputMap();void copyMap();int main(){    int row,col;    char ans;    init();    while(1)    {        outputMap();        //update the situation         for(row = 0; row < MAXROW; row++)        {            for(col = 0; col < MAXCOL; col++)            {                switch(neighbors(row,col))                {                    case 0:                    case 1:                    case 4:                    case 5:                    case 6:                    case 7:                    case 8:                        newmap[row][col] = DEAD;                        break;                    case 2:                        newmap[row][col] = map[row][col];                        break;                    case 3:                        newmap[row][col] = ALIVE;                        break;                }            }        }        copyMap();        printf("Continue next Generation?");        getchar();        ans = toupper(getchar());        if(ans != 'Y')            break;    }    return 0;}/*input init data*/void init(){    int row,col;    for(row = 0; row < MAXROW; row++)        for(col = 0; col < MAXCOL; col++)            map[row][col] = DEAD;    puts("Game of life Program");    puts("Enterx,y where x,y is living cell");    printf("0 <= x <= %d, 0 <= y <= %d\n",MAXROW - 1, MAXCOL -1 );    puts("Terminate with x,y = -1,-1");    while(1)    {        scanf("%d %d",&row, &col);        if(0 <= row && row <= MAXROW - 1 && 0 <= col && col <= MAXCOL - 1)            map[row][col] = ALIVE;        else if(row == -1 || col == -1)            break;        else            printf("x,y exceeds map ranage!\n");    }}/*return the neighbors number,which Alive*/int neighbors(int row, int col){    int count = 0, c, r;    for(r = row - 1; r <= row + 1; r++)        for(c = col - 1;c <= col +1; c++)        {            if(r < 0 || r >= MAXROW || c < 0 || c >= MAXCOL)                continue;            if(map[r][c] == ALIVE)                count++;        }    if(map[row][col] == ALIVE)        count--;    return count;}void outputMap(){    int row, col;    printf("\nGame of life cell %d status\n",life++);    for(row = 0; row < MAXROW; row++)    {        printf("\n");        for(col = 0; col < MAXCOL; col++)            if(map[row][col] == ALIVE)                putchar('#');            else                putchar('-');    }}void copyMap(){    int row, col;    for(row = 0; row < MAXROW; row++)        for(col  = 0; col < MAXCOL; col++)            map[row][col] = newmap[row][col];}
在这个过程中有一个有趣的现象,在初始化到达一定的程度时,经过若干次操作,细胞群的会趋于稳定,在两个状态之间无限切换,显而易见的是,这最终的两个状态与生存规则有相应的关联(一个平行或竖直的三连细胞组的生存变化就是无限的横竖切换)。比如下面这个初始细胞群。

------------------------------------------------------------------------------###----------------------##-------------------------#----------------------#--#---------------------------------------------------------------------------------------------
第十一次:
-------------------------------------###---------------------------------------------#-----#------------------#-----#------------------#-----#---------------------------------------------###------------------------------------------------------------第十二次:-------------#------------------------#------------------------#---------------------------------------------###---###---------------------------------------------#------------------------#------------------------#------------------------------------
第十三次:
-------------------------------------###---------------------------------------------#-----#------------------#-----#------------------#-----#---------------------------------------------###------------------------------------------------------------



0 0
原创粉丝点击