menu Andy's Blog
more_vert
C语言实现生命游戏
2020-04-27 | 科学,日常 | 暂无评论 | 1574 次阅读 | 692字
Andy
author

头文件

先来说说这个假期的过程,我这假期基本啥也没学到,全在啃老本,看着人家前辈一会去CTF,一会去kaggle,心中好生羡慕。


可惜本人能力有限,3D的CAD建模不会,nodejs入门,甚至连mdui都不会用,emmm,我真是太没有用了 ::(泪)


于是乎,我去研究内核,研究浏览器,最后由于C语言不过关(我C比C++还差),所以,emm,不得不因为UEFI都不过关,所以退一步,还是Python大法好!


或说前几天我的andyccr.com域名通过备案了,人生第一个备案域名 ::(汗)


github的项目贡献也稳定起来了,算了,不求发展,目前只求不要考完试,圈内都没人认识我了 ::(你懂的)


所以,我去玩了生命游戏和元胞自动机,没办法,这就是我擅长的地方了,emm,在下C++和Python程序员唯一的用处。


高数除了高斯消元和微积分入门啥也没学,哦,对了,还有占仆,emm,字打不出来了。 :xwx2:

我熬夜干出的生命游戏,LC上面的第N道题,emm,之前还难住我了,这个横跨了半个世纪的题目将被我彻底改造,哈哈! :huaji1:


#include<stdio.h>
#include<windows.h>
#define High 25                     //游戏尺寸
#define Width 50
//定义全局变量
int cellsHigh;         //细胞生1死0
void HideCursor()                //隐藏光标
{
 CONSOLE_CURSOR_INFO cursor_info = {1, 0};
 SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void gotoxy(int x,int y) //光标移动到(x,y)位置
{
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(handle,pos);
}
void startup()  //数据初始化
{
    int i,j;
    for(i=0;i<=High;i++)
        for(j=0;j<=Width;j++)
            cellsi=1;                          //所有细胞初始生,可用rand()随机函数使细胞初始状态随机
 
 
    HideCursor();
}
void show()     //显示界面
{
    gotoxy(0,0);
    int i,j;
    for(i=1;i<High;i++)
    {
        for(j=0;j<Width;j++)
        {
            if(cellsi==1)
                printf("*");
            else
                printf(" ");
        }
        printf("n");
    }
    Sleep(200);            //控制刷新速度
}
void updateWithoutInput()           //无需用户输入
{
    int i,j;
    int NeibourNumber;
    int tempHigh;
    for(i=1;i<High;i++)
    {
        for(j=1;j<Width;j++)
        {
            NeibourNumber=cellsi-1+cellsi-1+cellsi-1+cellsi+cellsi+cellsi+1+cellsi+1+cellsi+1;
            if(NeibourNumber==3)            //周围有3个活细胞时,该细胞生
                tempi=1;
            else if(NeibourNumber==2)       ////周围有2个活细胞时,该细胞状态不变
                        tempi=cellsi;
            else
                tempi=0;
        }
    }
    for(i=1;i<High;i++)
        for(j=1;j<Width;j++)
            cellsi=tempi;
}
int main()
{
    startup();
    while(1)
    {
        show();
        updateWithoutInput();
    }
}

第二波

来点干货,这次附带规则,这是我返校前最后一次实验,最后的稳定结构与上面的不一样。


/**
** C语言模拟生命游戏(原细胞自动机)
**
** 每个细胞有两种状态-存活或死亡,每个细胞与以自身为中心的周围八格细胞产生互动。
** 当前细胞为存活状态时,当周围低于2个(不包含2个)存活细胞时, 该细胞变成死亡状态。(模拟生命数量稀少)
** 当前细胞为存活状态时,当周围有2个或3个存活细胞时, 该细胞保持原样。
** 当前细胞为存活状态时,当周围有3个以上的存活细胞时,该细胞变成死亡状态。(模拟生命数量过多)
** 当前细胞为死亡状态时,当周围有3个存活细胞时,该细胞变成存活状态。 (模拟繁殖)
**
/
 
 
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
 
#define SIZE    16                            //地图数组的长度
#define Death    printf("%c%c",0xA1,0xF5)    //死亡方块
#define Live    printf("%c%c",0xA1,0xF6)    //存活方块
 
boolean map_bSIZE+2 = {0};
void display(const int mapSIZE + 2);
void transform(const int mapSIZE + 2);
 
 
int main(int argc, char** argv) {
 
    int mapSIZE + 2 = { 0 };
    int *map_p = map;
    int count = 0;
    map4 = map4 = map4 = map3 = map2 = 1;
 
    while (1)
    {
        printf("            第%d代n", count++);
        display(map_p);
        Sleep(200);        //休眠 
        transform(map_p);    //为什么这里不能填数组,而是填数组名
        system("cls");        //清屏 
    }
    system("pause");        //暂停
    return 0;
}
 
 
/改变状态/
void transform(int mapSIZE + 2)
{
    int w, h, sum;
    sum = 0;
    int map_tSIZE + 2 = {0};
 
    for (w = 1; w <= SIZE; w++)
        for (h = 1; h <= SIZE; h++)
            map_tw = mapw;
 
    for (w = 1; w <= SIZE; w++){
 
        for (h = 1; h <= SIZE; h++){
 
            sum = map_tw - 1 + map_tw - 1 + map_tw - 1
                + map_tw + map_tw
                + map_tw + 1 + map_tw + 1 + map_tw + 1;
 
            switch (sum)
            {
            case 2:
                break;
            case 3:
                if (map_tw == 0)
                    mapw = 1;
                break;
            default:
                mapw = 0;
                break;
 
            }
        }
    }
}
 
 
/显示/
void display(const int mapSIZE + 2){
 
    int w, h;
    
    for (w = 1; w <= SIZE; w++){
        for (h = 1; h <= SIZE; h++){
            if (mapw == 1)
                Live;
            else
                Death;
        }
        printf("n");
    }
}

:huaji17: :huaji22:


本文就结束啦!回学校,回学校,回学校,,,,,,,,,,,,


发表评论
暂无评论
textsms
account_circle
email
link