zoukankan      html  css  js  c++  java
  • C0nw4y's L!f3 G4me 代码实现

    这是我转载的博客,关于这个游戏的介绍。估计没人能get到这个游戏的blingbling的地方吧。还是蛮惊叹的。

    因为这里网络实在惨淡,闲来无事实现了下这个游戏,UI尽量美化了,可惜python配置不知道出了什么毛病,结果只能暂时用C实现一下了,起始页面通过方向键控制移动,空格键种下/拔起种子,回车键游戏开始,生命开始演化,我的博客那个logo gliter,动起来以后还是蛮漂亮的。

    #include <iostream>
    #include <cstdio>
    #include <windows.h>
    #include <conio.h>
    #include <ctime>
    #include <cstring>
    #define CELLCOLOR 4
    #define DEATHCOLOR 1
    using namespace std;
    
    const char UP= 72;
    const char DOWN= 80;
    const char LEFT= 75;
    const char RIGHT= 77;
    const char MOVE= ' ';
    const char LIVE= '0';
    const char DEATH= '*';
    const int WIDTH= 30;
    const int LENGTH= 56;
    int board[WIDTH][LENGTH];
    struct cell
    {
    	int x, y;
    	cell(){};
    	cell(int xx, int yy) : x(xx), y(yy) {}
    }player;
    
    void SetChessColor(int i, int j);
    void GoToxy(int x, int y);
    void Lif3_G4me(void);
    int AffineX(int x);
    int AffineY(int y);
    void FormGame();
    void Animation();
    void Update();
    bool InBoard(int x, int y);
    void Operation(int x, int y);
    bool LifeExist();
    void DrawFrame();
    
    int main()
    {
        SetConsoleTitle("C0nway's G4me 0f L!fe!!!!");
        system("color f5");
    	Lif3_G4me();
    	return 0;
    }
    void SetCellColor(int i, int j)
    {
        HANDLE c3ll;
        c3ll= GetStdHandle(STD_OUTPUT_HANDLE);
        if (board[i][j])
            SetConsoleTextAttribute(c3ll, DEATHCOLOR+15*0x10);
        else
            SetConsoleTextAttribute(c3ll, CELLCOLOR+15*0x10);
    }
    void GoToxy(int x, int y)
    {
        HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
        COORD pos;
        pos.X=y;
        pos.Y=x;
        SetConsoleCursorPosition(handle, pos);
    }
    void Lif3_G4me()
    {
    	DrawFrame();
    	GoToxy(AffineX(1), AffineY(1));
    	FormGame();
    	Animation();
    }
    int AffineX(int x)
    {
    	return x+10;
    }
    int AffineY(int y)
    {
    	return y*2+5;
    }
    void FormGame()
    {
    	memset(board, 0, sizeof(board));
    	player= cell(1, 1);
    	int nxtx, nxty;
    	char opr;
    	while ('
    '!= (opr= getch())){
    		if (UP== opr){
    			nxtx= player.x-1;
    			nxty= player.y;
    			Operation(nxtx, nxty);
    		}
    		else if (DOWN== opr){
    			nxtx= player.x+1;
    			nxty= player.y;
    			Operation(nxtx, nxty);
    		}
    		else if (LEFT== opr){
    			nxtx= player.x;
    			nxty= player.y-1;
    			Operation(nxtx, nxty);
    		}
    		else if (RIGHT== opr){
    			nxtx= player.x;
    			nxty= player.y+1;
    			Operation(nxtx, nxty);
    		}
    		else if (MOVE== opr){
    			char cl;
    			board[player.x][player.y]^= 1;
    			cl= board[player.x][player.y] ? LIVE : DEATH;
    			SetCellColor(player.x, player.y);
    			putchar(cl);
    			GoToxy(AffineX(player.x), AffineY(player.y));
    		}
    		else {
    			continue;
    		}
    	}
    }
    void Animation()
    {
    	while (LifeExist()){
    		Update();
    		DrawFrame();
    		Sleep(23);
    	}
    }
    void Update()
    {
    	int neibr, tboard[WIDTH][LENGTH];
    	for (int i= 1; i< WIDTH-1; ++i){
    		for (int j= 1; j< LENGTH-1; ++j){
    			neibr= 0;
    			for (int k= -1; k< 2; ++k){
    				for (int l= -1; l< 2; ++l){
    					neibr+= board[i+k][j+l];
    				}
    			}
    			neibr-= board[i][j];
    			if (board[i][j]){
    				tboard[i][j]= neibr<2 || neibr >3 ? 0 : 1;
    			}
    			else {
    				tboard[i][j]= 3== neibr ? 1 : 0;
    			}
    		}
    	}
    	for (int i= 1; i< WIDTH-1; ++i){
    		for (int j= 1; j< LENGTH-1; ++j){
    			board[i][j]= tboard[i][j];
    		}
    	}
    }
    bool InBoard(int x, int y)
    {
    	return x> 0 && x< WIDTH-1 && y> 0 && y< LENGTH-1;
    }
    void Operation(int x, int y)
    {
    	if(InBoard(x, y)){
    		player.x= x;
    		player.y= y;
    		GoToxy(AffineX(player.x), AffineY(player.y));
    	}
    }
    bool LifeExist()
    {
    	for (int i= 0; i< WIDTH; ++i){
    		for (int j= 0; j< LENGTH; ++j){
    			if (board[i][j]){
    				return true;
    			}
    		}
    	}
    	return false;
    }
    void DrawFrame()
    {
    	char cl;
    	for (int i= 0; i< WIDTH; ++i){
    		for (int j= 0; j< LENGTH; ++j){
    			GoToxy(AffineX(i), AffineY(j));
    			SetCellColor(i, j);
    			cl= board[i][j] ? LIVE : DEATH;
    			putchar(cl);
    			GoToxy(AffineX(i), AffineY(j));
    		}
    	}
    }
    

    除夕快乐;-)

  • 相关阅读:
    linux计划任务格式
    KVO监听数组的变化
    经典题目:输入半径求圆的面积
    /src/applicationContext.xml
    词法分析错题
    正规式与有限自动机
    词法分析程序的设计
    词法分析概述
    windows平台上 搭建 VisualSVN服务器 和 TortoiseSVN客户端
    maven error Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:3.1.0:single
  • 原文地址:https://www.cnblogs.com/Idi0t-N3/p/12232451.html
Copyright © 2011-2022 走看看