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 ('\r'!= (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));
}
}
}
除夕快乐;-)
C0nw4y's L!f3 G4me 代码实现的更多相关文章
- 阿拉伯数字1与英语字母l造成的代码bug
<img src="./images/demo3/1a.png" /> <img src="./images/demo3/la.png" /& ...
- 使用wc -l 来统计代码行数
Linux使用wc来统计行数真的好用 如在当前路径下统计一共多少行代码 $ find ./ -name *.c |xargs wc -l #包括了空行 ##-l :lines 如果不包括空行 ¥fin ...
- Adobe Dreamweaver代码编辑
Adobe Dreamweaver 同义词 DW(Adobe Dreamweaver的缩写)一般指Adobe Dreamweaver Adobe Dreamweaver,简称“DW”,中文名称 &qu ...
- IDEA 代码格式化,快捷键
一键格式化代碼: Ctrl+Alt+L 全局搜索替换:ctrl+shift+r 强大的搜索功能,shift+shift (无论您想要搜啥都能找到) ctrl+shift+R==搜索类 CTRL+N ...
- 佛祖保佑永无BUG代码注释
// // _oo0oo_ // o8888888o // 88" . "88 // (| -_- |) // 0\ = /0 // ___/`---'\___ // .' \\| ...
- 佛祖保佑 永无bug 代码注释
// // _oo0oo_ // o8888888o // 88" . "88 // (| -_- |) // 0\ = /0 // ___/`---'\___ // .' \\| ...
- Mysql:常用代码
C/S: Client Server B/S: Brower Server Php主要实现B/S .net IIS Jave TomCat LAMP:L Mysql:常用代码 Create table ...
- 使用 cloc 统计代码行数
可能大家都知道用 `wc -l` 命令进行代码行数统计,但是它会将代码中的注释.空行所占用的文本行都统计在内.如果想查看一个 tar 包或一个项目目录中“实际”的代码行数并且不愿意自己去写一个脚本来做 ...
- ThinkPHP函数详解:L方法
L方法用于启用多语言的情况下,设置和获取当前的语言定义.调用格式:L('语言变量'[,'语言值'])设置语言变量除了使用语言包定义语言变量之外,我们可以用L方法动态设置语言变量,例如: L('LANG ...
随机推荐
- HTML自制计算器
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- ie6 ie7下,Li不能自动换行,出现竖排文字现象(PS:li不固定宽度,所有li同一行显示),在ie8却可以
好久没写CSS,今天又发现一个ie兼容问题. 我需要所有的li在同一行显示,不固定Li的宽度,如果一行排不下,需要自动换行.当然Li的内容长度不同. 必须在li加white-space:nowrap; ...
- linux安装python3.*,更换Python2.*
下载并解压:Python-3.5.7.tgz [root@AH-aQYWTYSJZX01 python3]# ll total 20268 -rw-r----- 1 temp01 temp01 207 ...
- [vue/no-parsing-error] Parsing error: x-invalid-end-tag.eslint-plugin-vue
[vue/no-parsing-error] Parsing error: x-invalid-end-tag.eslint-plugin-vue 解决方案:vscode里面选择设置->搜索 ...
- STM32与STM8操作寄存器的区别
在STM8中,由于STM8寄存器较少,在头文件中定义寄存器的时候不用采取任何形式的封装,所以操作寄存器的时候直接可以用如下方式处理:PB_DDR |=0x20; 但是在STM32中,由于其寄存器实在太 ...
- Excel特殊符号的录入与录入的秘诀
软键盘就是输入法上的软键盘 右键单击软键盘 右键! 通过code函数得到符号的数字 按住alt键然后输入数字才可以得到符号 注意是在数字键盘 右边数字键盘区域 插入特殊符号 跳转方向的设置 如果超过 ...
- TCP&IP基础概念复习
第一章概述 NII(National Information Infrastructure):国家信息基础设施 GII(Global Information Infrastructure):全球信息基 ...
- JAVA8学习——从源码角度深入Stream流(学习过程)
从源代码深入Stream / 学习的时候,官方文档是最重要的. 及其重要的内容我们不仅要知道stream用,要知道为什么这么用,还要知道底层是怎么去实现的. --个人注释:从此看出,虽然新的jdk版本 ...
- 小白学 Python 爬虫(35):爬虫框架 Scrapy 入门基础(三) Selector 选择器
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- VMware 完成 27 亿美元的 Pivotal 收购 | 云原生生态周报 Vol. 34
作者 | 汪萌海.王思宇.李鹏 业界要闻 VMware 完成 27 亿美元的 Pivotal 收购 VMware 在 12 月 30 日宣布,已完成 27 亿美元的 Pivotal 收购,同一天 Pi ...