以前写的,主要是受到golly这个软件的启发(感觉是个很好玩的软件)
这个生命游戏可调节代数,存活条件,繁殖条件
like this

运行效果



代码:

function lifeinput(arg1)
% life game which can input point as you will.
% move the red cross with the key
% 'uparrow','downarrow','leftarrow','rightarrow'
% use the key 'space'to input
% 'bacspace'to delete
% 'b' to begin 'c'to stop and 'a' to restart
% size is standing of the size of the background
% live means the needs of lives around for living
% born means the needs of lives around for borning
% if you want the new lives to take place the elder input 1 else input 2
% try to type lifeinput('life'),lifeinput('brian'),lifeinput('firework')...if nargin < 1||strcmp(arg1,'hot')||strcmp(arg1,'cold')size=input('the board size is:');live=input('living needs are:');born=input('borning needs are:');generation=input('generation is:');       background=[0 0 0];if generation==1coloris=[1 1 1];endif generation~=1co=[0;sort((1./(1:(generation-1)))')];coloris=[ones(generation,1),co,zeros(generation,1)];if nargin < 1else if strcmp(arg1,'hot')coloris=[ones(generation,1),co,zeros(generation,1)];else strcmp(arg1,'cold')coloris=[zeros(generation,1),co,-sort(-co)];endendendarg1='start';endtake=3;if  strcmp(arg1,'life') size=50;live=[2 3];born=3;generation=1;coloris=[1 1 1];background=[0 0 0];take=1;endif  strcmp(arg1,'star wars') size=300;live=[3 4 5];born=2;generation=3;co=[0;sort((1./(1:(generation-1)))')];coloris=[ones(generation,1),co,zeros(generation,1)];background=[0 0 0];take=3;endif  strcmp(arg1,'brian')size=100;live=20;born=2;generation=2;co=[0;sort((1./(1:(generation-1)))')];coloris=[ones(generation,1),co,zeros(generation,1)];background=[0 0 0];take=3;endif  strcmp(arg1,'signet') size=100;live=[3 4 5];born=2;generation=4;co=[0;sort((1./(1:(generation-1)))')];coloris=[ones(generation,1),co,zeros(generation,1)];background=[0 0 0];take=2;endif  strcmp(arg1,'fireworks') size=300;live=2;born=[1 3];generation=21;co=[0;sort((1./(1:(generation-1)))')];coloris=[ones(generation,1),co,zeros(generation,1)];background=[0 0 0];take=3;end
%..........................................................................persistent save BT BI
%..........................................................................axis equalaxis([-0.5,size+0.5,-0.5,size+0.5])set(gca,'xtick',[],'ytick',[],'xcolor','w','ycolor','w')set(gca,'color',background)hold on%A=randi(9,[size,size]);%A(A<2.1)=0;%A(A>2)=1;A=zeros(size,size);%A=[input sqare with only ones and zeros][a,b]=find(A(:,:)==1);B=[b,length(sum(A,2))-a];if ~isempty(B)B=[B,ones(length(sum(B,2)),1)*coloris(1,:)];elseB=[1 1 1 1 1];B(1,:)=[];endpostion=[floor(size/2),floor(size/2)];control=1;plotl=scatter(gca,B(:,1),B(:,2),1200/size,B(:,3:5),'s','filled');plotpostion=scatter(gca,postion(1,1),postion(1,2),150,'rx');set(gcf, 'KeyPressFcn', @key)                                                     fps = 20;                                    game = timer('ExecutionMode', 'FixedRate', 'Period',1/fps, 'TimerFcn', @lifeGame);start(game)
set(gcf,'tag','co','CloseRequestFcn',@clo);function clo(~,~)stop(game)delete(findobj('tag','co'))clfcloseendfunction lifeGame(~,~)B=[1 1 1 1 1];B(1,:)=[];chang=length(sum(A,2));postion(postion>chang-1)=postion(postion>chang-1)-chang;postion(postion<0)=postion(postion<0)+chang;if control==2save=A;A(A~=0&A~=1)=0;B1=[A;zeros(1,chang)];B1(1,:)=[];B2=[zeros(1,chang);A];B2(end,:)=[];B3=[zeros(chang,1),A];B3(:,end)=[];B4=[A,zeros(chang,1)];B4(:,1)=[];C1=[zeros(chang,1),B1];C1(:,end)=[];C2=[B1,zeros(chang,1)];C2(:,1)=[];C3=[zeros(chang,1),B2];C3(:,end)=[];C4=[B2,zeros(chang,1)];C4(:,1)=[];NewA=B1+B2+B3+B4+C1+C2+C3+C4;progressA=zeros(chang,chang);livingA=zeros(chang,chang);for sborn=bornprogressA(NewA==sborn)=1;endfor slive=livet3=find(NewA==slive);t2=find(A==1);[lalala,~,~]=intersect(t3,t2);progressA(lalala)=1;livingA(lalala)=1; endA=progressA;save(save~=0)=save(save~=0)+1;save(save>generation)=0;if take==1save(save==0&A==1)=A(save==0&A==1);A=save;endif take==2A(save~=0&A~=1)=save(save~=0&A~=1);endif take==3A(save~=0&livingA~=1)=save(save~=0&livingA~=1);endfor gets=1:generation[a,b]=find(A(:,:)==gets);if ~isempty(a)BT=[b,length(sum(A,2))-a];BI=[BT,ones(length(b),1)*coloris(gets,:)];elseBI=[1 1 1 1 1];BI(1,:)=[];endB=[B;BI];endendif control==1for gets=1:generation[a,b]=find(A(:,:)==gets);if ~isempty(a)BT=[b,length(sum(A,2))-a];BI=[BT,ones(length(b),1)*coloris(gets,:)];elseBI=[1 1 1 1 1];BI(1,:)=[];endB=[B;BI];endendset(plotl,'XData',B(:,1),'YData',B(:,2),'CData',B(:,3:5))set(plotpostion,'XData',postion(1,1),'YData',postion(1,2))endfunction key(~,event)switch event.Keycase 'uparrow'postion=postion+[0,1];case 'downarrow'postion=postion+[0,-1];case 'leftarrow'postion=postion+[-1,0];case 'rightarrow'postion=postion+[1,0];case 'space'A(length(sum(A,2))-postion(1,2),postion(1,1))=1;case 'backspace'A(length(sum(A,2))-postion(1,2),postion(1,1))=0;case 'b'control=2;case 'c'control=1;case 'a'A=zeros(100,100);B=[1 1 1 1 1];B(1,:)=[];control=1;endend
end

matlab 生命游戏(可调节代数,存活条件,繁殖条件)相关推荐

  1. matlab 生命游戏 可自己输入图像

    制作了一个生命游戏的小游戏 上下左右移动红点 空格绘制生命 按键B开始运动 按键C暂停 按键A清空 按键backspace删除生命 游戏截图 动图 代码部分 function lifecontrol ...

  2. 【元胞自动机】元胞自动机生命游戏【含Matlab源码 655期】

    ⛄一.元胞自动机简介 1 元胞自动机发展历程 最初的元胞自动机是由冯 · 诺依曼在 1950 年代为模拟生物 细胞的自我复制而提出的. 但是并未受到学术界重视. 1970 年, 剑桥大学的约翰 · 何 ...

  3. 【老生谈算法】matlab实现生命游戏源码——生命游戏

    MATLAB实现生命游戏源码 1.文档下载: 本算法已经整理成文档如下,有需要的朋友可以点击进行下载 序号 文档(点击下载) 本项目文档 [老生谈算法]MATLAB实现生命游戏源码.docx 2.算法 ...

  4. 元胞自动机 | Matlab实现基于CA元胞自动机的生命游戏模拟

    文章目录 效果一览 文章概述 程序设计 参考资料 效果一览 文章概述 元胞自动机 | 基于CA元胞自动机的生命游戏模拟."生命游戏"是一个"元胞自动机",它是一 ...

  5. 元胞自动机CA+生命游戏代码

    1.元胞自动机 元胞自动机(Cellular Automaton,复数为Cellular Automata,简称CA,也有人译为细胞自动机.点格自动机.分子自动机或单元自动机).是一时间和空间都离散的 ...

  6. Python实现生命游戏(Game of Life)

    生命游戏的算法就不多解释了,百度一下介绍随处可见. 因为网上大多数版本都是基于pygame,matlab等外部库实现的,二维数组大多是用numpy,使用起来学习成本比较高,所以闲暇之余写一个不用外部依 ...

  7. 康威生命游戏是如何搭建计算机的?

    2020年4月,数学家约翰·康威(John H. Conway)因新冠肺炎去世.大家回顾康威教授平生贡献时,不可避免要提到伟大.深刻的"康威生命游戏"(Conway's Game ...

  8. 复杂性思维中文第二版 六、生命游戏

    六.生命游戏 原文:Chapter 6 Game of Life 译者:飞龙 协议:CC BY-NC-SA 4.0 自豪地采用谷歌翻译 在本章中,我们考虑二维细胞自动机,特别是 John Conway ...

  9. Python | 实现简单的康威生命游戏

    康威生命游戏是由英国教授John Horton Conway提出的一种生命仿真游戏.其具体的形式是,所有的细胞生活在一个二维平面上,一个细胞在下一个时刻的生存状态取决于当前时刻周围的各个细胞的生存状态 ...

最新文章

  1. SystemCenter2012SP1实践(31)P2V迁移故障一则
  2. python pip国内镜像
  3. 深入分析 Java 中的中文编码问题--转
  4. shell中三种引号的区别
  5. chmod 777命令_linux常用命令
  6. shell mysql eof_shell EOF
  7. Java - 网络与多线程 - Java Socket 应用
  8. 数据结构 之 图的存储和遍历
  9. 程序员面试金典——18.4 2的个数
  10. 面试题大全-操作系统面试题
  11. activiti6创建28张表
  12. 腾讯云服务器价格明细表(CPU内存/带宽/磁盘)
  13. 云服务器安全组设置后,依然无法访问端口(已解决)
  14. 申论(写作篇)之花木体申论写作法
  15. Centos7安装Docker,阿里源,网易镜像
  16. 浏览器对HTML5中track标签中src属性路径vtt文件错误,Unsafe attempt to load URL vtt
  17. 算法竞赛---day1(等差素数列)
  18. 关于18位身份证号码最后一位计算(Java实现)
  19. 数据预处理之scaling
  20. linux开发板设置时间,开发板上修改时间方法

热门文章

  1. 足球大数据预测中欧亚大小球随时间变化的处理
  2. 红米2电信 android go,超能玩手机:如何让红米2电信版正确支持“全网通”?
  3. t420i升级固态硬盘提升_固态硬盘优化神器,一键提升固态硬盘性能和使用寿命...
  4. 以太坊智能合约部署——一个简单的投票系统
  5. SQL数学函数SIGN
  6. 网络对抗 Exp6 信息收集与漏洞扫描 20154311 王卓然
  7. php eos,EOS区块链PHP开发包
  8. 查看显卡单精度 linux,部分显卡及CPU单、双精度算力实则(3月9日更新数据)
  9. 一文带您读懂FCC、CE、CCC认证的区别
  10. Oracle作业第四章