跳到正文

休闲·娱乐

Life Game

#SUSPENDED#约 6 分钟
原文

这是 2026年6月19日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2025年8月27日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 6 分钟
获赞
1

版本

  1. v2当前
  2. v1

LifeGameLifeGame

C++
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
using namespace std;
const int dx[]={0,0,0,1,1,1,-1,-1,-1},dy[]={0,1,-1,0,1,-1,0,1,-1};
int n,m,t,a[1005][1005],b[1005][1005],x=1,y=1,p=1,px=1,py=1;
HANDLE hConsole=GetStdHandle(STD_OUTPUT_HANDLE);
void Color(int color){
    SetConsoleTextAttribute(hConsole,FOREGROUND_INTENSITY|color);
}void Clear(){
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    COORD topLeft={0,0};
    DWORD written;
    GetConsoleScreenBufferInfo(hConsole,&csbi);
    DWORD cells=csbi.dwSize.X*csbi.dwSize.Y;
    FillConsoleOutputCharacter(hConsole,' ',cells,topLeft,&written);
    FillConsoleOutputAttribute(hConsole,FOREGROUND_INTENSITY|7,cells,topLeft,&written);
    SetConsoleCursorPosition(hConsole,topLeft);
}void gotoXY(int x,int y){
    COORD coord={(SHORT)y,(SHORT)x};
    SetConsoleCursorPosition(hConsole,coord);
}void draw_cell(int i,int j,bool is_cursor=false){
    gotoXY(i+1,j*2);
    if(a[i][j])Color(50);
    else Color(5);
    if(is_cursor&&p)printf("# ");
    else printf("+ ");
}void draw_init(){
    Color(3);
    if(p)printf("'W','S','A','D'移动光标,'空格'点亮/熄灭,按'B'开始模拟\n");
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            draw_cell(i,j,(i==x&&j==y));
}void update_cursor(){
    if(px!=x||py!=y)draw_cell(px,py);
    draw_cell(x,y,true);
    px=x,py=y;
}int main(){
    while(m<=0||m>1000)printf("输入场地长(整数)(0<n≤1000):"),scanf("%d",&m);
    while(n<=0||n>1000)printf("输入场地宽(整数)(0<n≤1000):"),scanf("%d",&n);
    while(t<=0)printf("输入每帧间隔(整数,单位毫秒)(0<t):"),scanf("%d",&t);
    Clear();
    draw_init();
    while(1){
        if(_kbhit()){
            char move=_getch();
            if(move=='w'&&x>1)x--;
            else if(move=='s'&&x<n)x++;
            else if(move=='a'&&y>1)y--;
            else if(move=='d'&&y<m)y++;
            else if(move==' '){
                a[x][y]=!a[x][y];
                draw_cell(x,y,true);
            }else if(move=='b')break;
            update_cursor();
        }Sleep(16);
    }p=0;
    Clear();
    Color(3);
    printf("模拟中...按任意键退出\n");
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            draw_cell(i,j);
    while(!_kbhit()){
        memset(b,0,sizeof(b));
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                int c=0;
                for(int k=1;k<=8;k++){
                    int ni=i+dx[k],nj=j+dy[k];
                    if(ni>=1&&ni<=n&&nj>=1&&nj<=m&&a[ni][nj])c++;
                }if(a[i][j]){
                    if(c<2)b[i][j]=0;
                    else if(c==2||c==3)b[i][j]=1;
                    else if(c>3)b[i][j]=0;
                }else if(c==3)b[i][j]=1;
            }
        }for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(a[i][j]!=b[i][j]){
                    a[i][j]=b[i][j];
                    draw_cell(i,j);
                }
            }
        }Sleep(t);
    }return 0;
}