C语言---动态图或者生命

修改参数就可以修改生命演进过程

复制代码
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#define High 20
#define Width 50

int cell[High][Width];

void HideCursor() { // if cursor
    CONSOLE_CURSOR_INFO cursor_info= {1,0}; //second value is 0 mean hide cursor
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}

void gotoxy(int x, int y) { // move mouse to x,y position, similer clear screen
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(handle, pos);
}

void startup() { // init data

    int i,j;

    for(i=0; i<High; i++)
        for(j=0; j<Width; j++)
            cell[i][j] = rand() % 2;

    HideCursor();
}

void show() { // show windows
    gotoxy(0,0);  //move mouse to 0,0 origin point, and redraw screen
    int i, j;

    for(i=0; i<High; i++) {
        for(j=0; j<Width; j++) {
            if(cell[i][j] == 1)
                printf("*");  //show bird *
            else
                printf(" ");  //show nothing
        }
        printf("\n");
    }
}

void updateWithoutInput() {
    int neighbernum=0,i,j,tempcell[High][Width];

    for(i=0; i<High; i++)
        for(j=0; j<Width; j++)
            tempcell[i][j] = cell[i][j];

    for(i=1; i<High-1; i++)
        for(j=1; j<Width-1; j++) {

            neighbernum = cell[i-1][j-1] + cell[i-1][j] + cell[i-1][j+1] +
                          cell[i][j-1] + cell[i][j] + cell[i][j+1] +
                          cell[i+1][j-1] + cell[i+1][j] + cell[i+1][j+1] ;
            if(neighbernum == 3)
                tempcell[i][j] = 1;
            else if(neighbernum == 2 || neighbernum == 4)
                tempcell[i][j] = cell[i][j];
            else
                tempcell[i][j] = 0;
        }

    for(i=0; i<High; i++)
        for(j=0; j<Width; j++)
            cell[i][j] = tempcell[i][j] ;


}

void updateWithInput() {
    char input;
    if(kbhit()) { //runing while user push keyboard
        input = getch();
    }
}
int main() {
    startup(); // init data
    while(1) { // game loop run
        show(); // show windows
        updateWithoutInput(); //update don't need user
        updateWithInput(); //update need user
    }
    return 0;
}
复制代码

posted @   yangly  阅读(882)  评论(0编辑  收藏  举报
编辑推荐:
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
阅读排行:
· [翻译] 为什么 Tracebit 用 C# 开发
· Deepseek官网太卡,教你白嫖阿里云的Deepseek-R1满血版
· 2分钟学会 DeepSeek API,竟然比官方更好用!
· .NET 使用 DeepSeek R1 开发智能 AI 客户端
· 刚刚!百度搜索“换脑”引爆AI圈,正式接入DeepSeek R1满血版
点击右上角即可分享
微信分享提示