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 ...
随机推荐
- 使用 node.js三行代码实现手机端访问html页面文件
首先确保你安装了node (全局安装) npm install -g browser-sync // --files 路径是相对于运行该命令的项目(目录) browser-sync start --s ...
- dynamic web module version
Ser vlet 3十二月2009开发平台标准版6,6可插性,易于开发,异步ser vlet,安全,文件上传 Ser vlet 2.5九月2005开发平台标准版5,5需要平台标准版5,支持注释 Ser ...
- Linux 内核同步 urb
不幸的是, 同步 urb 没有一个象中断, 控制, 和块 urb 的初始化函数. 因此它们必须在 驱动中"手动"初始化, 在它们可被提交给 USB 核心之前. 下面是一个如何正确初 ...
- 2018-10-17-Sublime-Text-好用的插件
title author date CreateTime categories Sublime Text 好用的插件 lindexi 2018-10-17 10:14:40 +0800 2018-2- ...
- 【Ubuntu】查看端口占用及关闭
1.查看已连接的服务端口 (ESTABLISHED) netstat有一个快捷键[ss] netstat -a ss -a 2.查看所有的服务端口(LISTEN,ESTABLISHED) netsta ...
- git push报错大文件,删除后重新commit依然报错
git push报错: github不能上传大文件,按道理删掉重新提交就行了 可是删掉后,git add -A,再git commit,再git push,依然报错 后来我想明白了 github上传时 ...
- 用mingw静态编译Qt4.8.2和Qt5.1.1(需要修改不少源码)
因为一些乱七八糟的原因,我需要用mingw静态编译Qt4.8.2和Qt5.1.1.经历了一天的折腾之后,自觉编译一下Qt还是件颇为麻烦的事情,故将过程略作总结,以备不时之需. 首先,在编译之前,我需要 ...
- 21.time和random
原文:https://www.cnblogs.com/yuanchenqi/article/5732581.html time模块 三种时间表示 在Python中,通常有这几种方式来表示时间: 时间戳 ...
- leetcode.1275找出井字棋的获胜者
A 和 B 在一个 3 x 3 的网格上玩井字棋. 井字棋游戏的规则如下: 玩家轮流将棋子放在空方格 (" ") 上.第一个玩家 A 总是用 "X" 作为棋子, ...
- Python3 pip换源
pip安装源 介绍 """ 1.采用国内源,加速下载模块的速度 2.常用pip源: -- 豆瓣:https://pypi.douban.com/simple -- 阿里: ...