Life Game

· · 休闲·娱乐

LifeGame
#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){
    HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(handle,FOREGROUND_INTENSITY|color);
}void hidecur(){
    CONSOLE_CURSOR_INFO cur={1,0};
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cur);
}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(true){
        n=0,m=0,t=0,x=1,y=1,p=1,px=1,py=1;memset(a,0,sizeof a);memset(b,0,sizeof b);
        Color(6);hidecur();
        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();hidecur();
        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];
                    hidecur();draw_cell(x,y,true);
                }else if(move=='b')break;
                hidecur();update_cursor();
            }Sleep(16);
        }p=0;
        Clear();
        Color(3);hidecur();
        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];
                        hidecur();draw_cell(i,j);
                    }
                }
            }Sleep(t);
        }char ch=getch();
        system("cls");
    }return 0;
}