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 ...
随机推荐
- 2018-10-19-jekyll-添加-Valine-评论
title author date CreateTime categories jekyll 添加 Valine 评论 lindexi 2018-10-19 09:10:40 +0800 2018-2 ...
- CSS---cursor 鼠标指针光标样式(形状)
url 需使用的自定义光标的 URL. 注释:请在此列表的末端始终定义一种普通的光标,以防没有由 URL 定义的可用光标. default 默认光标(通常是一个箭头) auto 默认.浏览器设置的光标 ...
- <数论相关>逆元专题
逆元专题推荐这个blog:https://www.cnblogs.com/zjp-shadow/p/7773566.html 逆元问题应用的范围:处理分数模问题.例如求 42/4 + 42/8 + 3 ...
- FreeSql配合仓储实现软删除
该篇内容由个人博客点击跳转同步更新!转载请注明出处! 前段时间使用FreeSql作为ORM,写了一个简单的CMS,在这里总结一下其中的使用心得. 仓储配合全局过滤器 1. 统一的删除标志 如:数据库字 ...
- 一种HTML table合并单元格的思路
/** * 合并单元格 * @param table1 表格的ID * @param startRow 起始行 * @param col 合并的列号,对第几列进行合并(从0开始).如果传下来为0就是从 ...
- [梁山好汉说IT] 容器概念在北宋社会的应用
[梁山好汉说IT] 容器概念在北宋社会的应用 0x00 摘要 如何对没有软件开发经验的人解释容器? 集装箱真的能够完美解释容器嘛? 除了集装箱还有其他常见实体能够解释容器嘛? 我找到了一个能够 和集装 ...
- Spring Boot 定时+多线程执行
Spring Boot 定时任务有多种实现方式,我在一个微型项目中通过注解方式执行定时任务. 具体执行的任务,通过多线程方式执行,单线程执行需要1小时的任务,多线程下5分钟就完成了. 执行效率提升10 ...
- MySQL的读写分离与主从同步数据一致性
有没有做MySQL读写分离?如何实现mysql的读写分离?MySQL主从复制原理的是啥?如何解决mysql主从同步的延时问题? 高并发这个阶段,那肯定是需要做读写分离的,啥意思?因为实际上大部分的互联 ...
- 使用宝塔搭建nextcloud的过程(搭建、优化、问题)
宝塔部署教程 参考网址: 使用NextCloud来搭建我们的私有网盘.并结合Redis优化性能(宝塔) https://www.moerats.com/archives/175/ 宝塔面板下nextc ...
- python+selenium+Chrome options参数
python+selenium+Chrome options参数 Chrome Options常用的行为一般有以下几种: 禁止图片和视频的加载:提升网页加载速度. 添加代理:用于翻墙访问某些页面,或者 ...