The game of life(生命游戏)新算法
我写了一种常见的实现算法,和另一种新算法,即不是每次循环计算每个细胞的周围细胞数来产生下一时刻,而是每次每个产生状态变化的细胞主动通知周围的邻居,因此每个细胞增加一个用来记录邻居数的字段。由邻居数决定每个细胞的出生和死亡,然后影响周围邻居的邻居数。并且为了不影响后续细胞的判断,需要新旧邻居数两个状态,用旧邻居数决定自己生死,而自己的生死变化影响周围邻居的新邻居数。另外如果某个格子的新旧邻居数不变则状态不变,增加一个changed字段来表示。
下面分别是旧、新两种算法。
#include <stdio.h>
#include <stdlib.h> #define ROW 20
#define COL 60
#define FILEPATH "1.txt" int countNbor(char data[][COL],int i,int j);
void world(void); int main(void)
{
world();
return ;
} void world(void)
{
int i,j;
char data[ROW][COL];
char temp[ROW][COL];
int time=; FILE *fp=fopen(FILEPATH,"r"); for(i=;i<ROW;i++)
{
for(j=;j<COL;j++)
{
if(fgetc(fp)=='*') //表示细胞的字符
temp[i][j]=data[i][j]='*';
else temp[i][j]=data[i][j]=' ';
}
fgetc(fp);
}
fclose(fp); while()
{
time++; system("CLS");
for(i=;i<ROW;i++)
{
for(j=;j<COL;j++)
printf("%c",data[i][j]);
printf("\n");
}
printf("次数:%d\n",time); system("PAUSE>NUL"); for(i=;i<ROW;i++)
for(j=;j<COL;j++)
switch(countNbor(temp,i,j))
{
case :
data[i][j]='*';
break;
case :
break;
default:
data[i][j]=' ';
break;
} for(i=;i<ROW;i++)
for(j=;j<COL;j++)
temp[i][j]=data[i][j];
} } int countNbor(char data[][COL],int i,int j)
{
int m,n;
int count=; for(m=i-;m<=i+;m++)
for(n=j-;n<=j+;n++)
if( (m==i&&n==j) ||m<||n<||m==ROW||n==COL)
continue;
else if(data[m][n]=='*') count++; return count;
}
old.c
#include <stdio.h>
#include <stdlib.h> #define ROW 20
#define COL 60
#define FILEPATH "1.txt" typedef struct{
int live; //1、0表生死
int nbor_old; //旧邻居数,用于判断细胞生死
int nbor_new; //新邻居数,用于下一时刻
int changed; //邻居数是否变化
}Cell; typedef struct{
Cell cell[ROW+][COL+]; //+2留边
int lives_num; //细胞数目
int time_count; //第几轮
}World; void showWorld(World *world);
void sendNbor(Cell cell[][COL+],int i,int j,int live);
void processCell(World *world,int i,int j);
void iniWorld(World *world);
void runWorld(void); int main(void)
{
runWorld();
return ;
} void runWorld(void)
{
World world;
Cell (*cell)[COL+]=world.cell;
int i,j; //从文件初始化
iniWorld(&world); while()
{ world.time_count++;
showWorld(&world); //前提 cell[i][j].nbor_old==cell[i][j].nbor_new
//邻居数不变则状态不变,不用处理
for(i=;i<=ROW;i++)
for(j=;j<=COL;j++)
if(cell[i][j].changed)
processCell(&world,i,j); //判断自身,影响周围 for(i=;i<=ROW;i++)
for(j=;j<=COL;j++)
if(cell[i][j].nbor_old==cell[i][j].nbor_new)
cell[i][j].changed=;
else
{
cell[i][j].changed=;
cell[i][j].nbor_old=cell[i][j].nbor_new;
} }
} void iniWorld(World *world)
{
int i,j;
FILE *fp=fopen(FILEPATH,"r");
Cell (*cell)[COL+]=world->cell; world->time_count=;
world->lives_num=; for(i=;i<=ROW;i++)
{
for(j=;j<=COL;j++)
{
if(fgetc(fp)=='*') //表示细胞的字符
{
cell[i][j].live=;
world->lives_num++;
}
else
cell[i][j].live=; cell[i][j].nbor_new=cell[i][j].nbor_old=;
cell[i][j].changed=; //为了第一次循环每个细胞都能处理
}
fgetc(fp); //换行符
} fclose(fp); //填充nbor_old和nbor_new
for(i=;i<=ROW;i++)
for(j=;j<=COL;j++)
if(cell[i][j].live)
{
cell[i-][j-].nbor_old =++cell[i-][j-].nbor_new;
cell[i-][j].nbor_old =++cell[i-][j].nbor_new;
cell[i-][j+].nbor_old =++cell[i-][j+].nbor_new;
cell[i][j-].nbor_old =++cell[i][j-].nbor_new;
cell[i][j+].nbor_old =++cell[i][j+].nbor_new;
cell[i+][j-].nbor_old =++cell[i+][j-].nbor_new;
cell[i+][j].nbor_old =++cell[i+][j].nbor_new;
cell[i+][j+].nbor_old =++cell[i+][j+].nbor_new;
}
} //由old决定生死,并改变周围细胞的new
void processCell(World *world,int i,int j)
{
Cell (*cell)[COL+]=world->cell; switch(cell[i][j].nbor_old)
{
case :
if(!cell[i][j].live)
{
cell[i][j].live=;
sendNbor(cell,i,j,);
world->lives_num++;
}
break;
case : //不变
break;
default:
if(cell[i][j].live)
{
cell[i][j].live=;
sendNbor(cell,i,j,-);
world->lives_num--;
}
break;
}
} //细胞状态改变后,影响周围细胞的邻居值,live为-1或1
void sendNbor(Cell cell[][COL+],int i,int j,int live)
{
cell[i-][j-].nbor_new+=live;
cell[i-][j].nbor_new+=live;
cell[i-][j+].nbor_new+=live;
cell[i][j-].nbor_new+=live;
cell[i][j+].nbor_new+=live;
cell[i+][j-].nbor_new+=live;
cell[i+][j].nbor_new+=live;
cell[i+][j+].nbor_new+=live;
} void showWorld(World *world)
{
int i,j; system("CLS"); for(i=;i<=ROW;i++)
{
for(j=;j<=COL;j++)
{
if(world->cell[i][j].live)
printf("*");
else printf(" ");
}
printf("\n");
}
printf("细胞数:%d\n次数:%d\n",world->lives_num,world->time_count); system("PAUSE>NUL");
}
new.c
The game of life(生命游戏)新算法的更多相关文章
- LeetCode | 289. 生命游戏(原地算法/位运算)
记录dalao的位运算骚操作 根据百度百科 ,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在 1970 年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细 ...
- React项目(二):生命游戏
引子 这是16年最后的一个练手项目,一贯的感觉就是,做项目容易,写说明文档难.更何况是一个唤起抑郁感觉的项目,码下的每个字,心就如加了一个千斤的砝码. 2016年,有些事我都已忘记,但我现在还记得.2 ...
- 生命游戏 Java
本程序由四个类组成:其中Init_data,用于初始化各个活细胞的状态judge_state,用于判断下一代的细胞状态,并进行更新.set_color,用于给GUI界面中各个细胞涂色set_frame ...
- Conway生命游戏
版权申明:本文为博主窗户(Colin Cai)原创,欢迎转帖.如要转贴,必须注明原文网址 http://www.cnblogs.com/Colin-Cai/p/9986679.html 作者:窗户 Q ...
- [Swift]LeetCode289. 生命游戏 | Game of Life
According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...
- 【LeetCode】Game of Life(生命游戏)
这道题是LeetCode里的第289道题. 题目描述: 根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格 ...
- Leetcode 289.生命游戏
生命游戏 根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞.每个细胞具有一个初始状 ...
- 实用---生命游戏 Java
本程序由四个类组成: 其中Init_data,用于初始化各个活细胞的状态judge_state,用于判断下一代的细胞状态,并进行更新.set_color,用于给GUI界面中各个细胞涂色set_fram ...
- 生命游戏(python实现,pygame显示图形)
# 游戏规则:# 生命游戏(Game of Life),或者叫它的全称John Conway's Game of Life.是英国数学家约翰·康威在1970年代所发明的一种元胞自动机.# 1. 活细胞 ...
随机推荐
- oracle创建实例SID
用oracle用户登录 输入startx开发可视化界面,打开命令行模式 (如果只有壁纸,没有桌面图标和任务栏,按下 Ctrl + Alt + T 打开命令行) 输入dbca打开配置窗口 最后就各种下一 ...
- servlet的提交
servlet的提交和动态改变有点依赖xml 我们点击控件的时候改变了里面的变量,改变了xml,servlet发现变量变了,就会刷新页面 如果xml文档没有更新,浏览器采用缓存而不则行 <for ...
- 完整的 dataType=text/plain jquery ajax 登录验证
Html: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <m ...
- 《作业控制系列》-“linux命令五分钟系列”之十
本原创文章属于<Linux大棚>博客. 博客地址为http://roclinux.cn. 文章作者为roc 希望您能通过捐款的方式支持Linux大棚博客的运行和发展.请见“关于捐款” == ...
- Bootstrap_表单_图像
在Bootstrap框架中对于图像的样式风格提供以下几种风格: 1.img-responsive:响应式图片,主要针对于响应式设计2.img-rounded:圆角图片3.img-circle:圆形图片 ...
- Atom package安装失败的解决方案
cd ~/.atom/package git clone [package url] cd [package name] apm install [package name] if lack some ...
- 技术大牛面试 http://www.itmian4.com/forum.php?mod=viewthread&tid=3824
不久前,byvoid面阿里星计划的面试结果截图泄漏,引起无数IT屌丝的羡慕敬仰.看看这些牛人,NOI金牌,开源社区名人,三年级开始写Basic...在跪拜之余我们不禁要想,和这些牛人比,作为绝大部分技 ...
- Linux 环境下自动化测试工具,Redhat dogtail的安装
dogtail基于Accessibility(a11y)的GUI图形界面测试工具和自动化框架可以与linux桌面应用程序进行交互操作. dogtail是用Python语言写的.dogtail的测试脚本 ...
- python 编程之计算器
作业: 使用正则表达式和递归实现计算器功能. 实现: 1.实现带括号的计算 2.实现指数.加减乘除求余等功能 一.实例说明: 本实例自己写了个版本,但依旧存在一点bug,例:-2-2等计算问题,故最后 ...
- 开发DZ插件教程
插件制作的基本思路是:(初学者适用)1.形成插件思路2.制作插件界面3.构架程序模块4.搭建存储数据5.填充功能语句6.检查应用错误7.完善插件功能 前言:为方便互联网数万Discuz!爱好者,更加深 ...