概述
以前写的,主要是受到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==1
coloris=[1 1 1];
end
if generation~=1
co=[0;sort((1./(1:(generation-1)))')];
coloris=[ones(generation,1),co,zeros(generation,1)];
if nargin < 1
else if strcmp(arg1,'hot')
coloris=[ones(generation,1),co,zeros(generation,1)];
else strcmp(arg1,'cold')
coloris=[zeros(generation,1),co,-sort(-co)];
end
end
end
arg1='start';
end
take=3;
if strcmp(arg1,'life')
size=50;live=[2 3];born=3;generation=1;coloris=[1 1 1];background=[0 0 0];take=1;
end
if 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;
end
if 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;
end
if 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;
end
if 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 equal
axis([-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,:)];
else
B=[1 1 1 1 1];
B(1,:)=[];
end
postion=[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'))
clf
close
end
function 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==2
save=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=born
progressA(NewA==sborn)=1;
end
for slive=live
t3=find(NewA==slive);
t2=find(A==1);
[lalala,~,~]=intersect(t3,t2);
progressA(lalala)=1;
livingA(lalala)=1;
end
A=progressA;
save(save~=0)=save(save~=0)+1;
save(save>generation)=0;
if take==1
save(save==0&A==1)=A(save==0&A==1);
A=save;
end
if take==2
A(save~=0&A~=1)=save(save~=0&A~=1);
end
if take==3
A(save~=0&livingA~=1)=save(save~=0&livingA~=1);
end
for 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,:)];
else
BI=[1 1 1 1 1];
BI(1,:)=[];
end
B=[B;BI];
end
end
if control==1
for 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,:)];
else
BI=[1 1 1 1 1];
BI(1,:)=[];
end
B=[B;BI];
end
end
set(plotl,'XData',B(:,1),'YData',B(:,2),'CData',B(:,3:5))
set(plotpostion,'XData',postion(1,1),'YData',postion(1,2))
end
function key(~,event)
switch event.Key
case '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;
end
end
end
最后
以上就是野性唇膏为你收集整理的matlab 生命游戏(可调节代数,存活条件,繁殖条件)的全部内容,希望文章能够帮你解决matlab 生命游戏(可调节代数,存活条件,繁殖条件)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复