TheGameOfLife

#include <stdio.h>(standard input &output输入输出)
#include <stdlib.h>(定义杂项函数及内存分配函数)
#include <windows.h>(对话框程序或者窗口程序)
#include <time.h>(从系统时钟获取时间方式)
#include <ctype.h>(字符函数)


//define the size of the two-dimensional array(定义大小的二维数组)
#define SIZE 40
#define times 1000//总迭代次数
#define pause 20//暂停
#define interval 10//运行频率 1000=1sec

void rule(int array[SIZE][SIZE]);//function prototype(函数原型)

//change color(改变颜色)
void SetColor(unsigned short ForeColor = 5,unsigned short BackGroundColor = 8){
    HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hCon,ForeColor|BackGroundColor);
}

int main(){
    
    //0->virus(病毒) 1->cell(细胞) 2->boundary(边界)
    int data[SIZE][SIZE]={0};
    int i,j,instruct,m;
    char ch;
    
    //use the current time to seed the random number generator(使用当前时间种子的随机数发生器)
    srand(time(NULL));
    for(i=0;i<=SIZE-1;i++){
        for(j=0;j<=SIZE-1;j++){
            if(i*j==0||i==SIZE-1||j==SIZE-1){
                data[i][j]=22;
            }
            else data[i][j]=11*(rand()%2);
        }
    }
    
    //to set the size and the color of the window (设置大小和颜色的窗口)
    system("mode con cols=100 & color 0f");
    
    //input a random number except 0 to continue the game(输入一个随机数除0继续游戏)
    for(m=0;m<=times;m++){
        
        //clear the screen
        system("cls");
        printf("%d\n",m);
        Sleep(interval);
        for(i=0;i<=SIZE-1;i++){
            for(j=0;j<=SIZE-1;j++){
                if(data[i][j]%10==1){
                    SetColor(10);//function call(函数的调用)
                    printf("%c ",6);//display the character(显示字符)
                }
                else if(data[i][j]%10==0){
                    SetColor(6);//function call(函数的调用)
                    printf("%c ",4);//display the character(显示字符)
                }
                else{
                    SetColor(3);//function call(函数的调用)
                    printf("%c ",3);//display the character(显示字符)
                }
                if(i*j==0||i==SIZE-1||j==SIZE-1){
                    ;
                }
                else data[i][j]=(data[i][j]%10)*10;
            }
            printf("\n");
        }
        
        rule(data);//function call(函数的调用)
        if(m%pause==0) {
            printf("输入1继续:");
            scanf("%c",&ch);
         }
    }
    return 0;
}

void rule(int array[SIZE][SIZE]){
    int amount,i,j,k,l;
    for(i=1;i<=SIZE-2;i++){
        for(j=1;j<=SIZE-2;j++){
            amount=0;
            for(k=i-1;k<=i+1;k++){
                for(l=j-1;l<=j+1;l++){
                    if(array[k][l]/10==1&&(k!=i||l!=j)){
                        amount++;
                    }
                }
            }
            //** the rule of the game of life(游戏的规则生活)
            if(amount==3){
                array[i][j]++;
            }
            else if(amount==2){
                array[i][j]+=array[i][j]/10;
            }
            else ;
        }
    }
    
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值