元胞自动机——生命游戏
<p>%元胞自动机——生命游戏%时间:2015-8-17-一
%作者:张卫东
clc
clear
close all
%=================================
%先建立GUI
%定义:开始按钮
plotbutton=uicontrol('style','Pushbutton',...
'string','Run',...
'fontsize',12,...
'position',,...
'callback','run=1;');
%定义:停止按钮
erasebutton=uicontrol('style','Pushbutton',...
'string','Stop',...
'fontsize',12,...
'position',,...
'callback','freeze=1;');
%定义:退出按钮
quitbutton=uicontrol('style','Pushbutton',...
'string','Quit',...
'fontsize',12,...
'position',,...
'callback','stop=1;close;');
%定义:进化步数
number=uicontrol('style','text',...
'string','1',...
'fontsize',12,...
'position',) ;
%=================================</p><p>%CA开始
n=128;
%初始化向量
z=zeros(n,n);
cells=z;
sum=z;
%设置少量元素为一;
%cells(n/2,0.25*n:0.75*n)=1;
%cells(0.25*n:0.75*n,n/2)=1;
%cells(0.5*n-1,0.5*n-1)=1;
%cells(0.5*n-2,0.5*n-2)=1;
%cells(0.5*n-3,0.5*n-3)=1;
cells=(rand(n,n))<0.5;</p><p>%建立一个图片,并画出它!
imh=image( cat(3,cells,z,z) );
set( imh,'erasemode','none' )
axis equal
axis tight
%index defination for cell updata
x=2:n-1;
y=2:n-1;</p><p>%Main event loop
stop=0; %wait for a quit button push
run=0; %wait for a draw
freeze=0; %wait for a freeze</p><p>while stop==0
if run==1
sum(x,y)=cells(x,y-1)+cells(x,y+1)+...
cells(x-1,y)+cells(x+1,y)+...
cells(x-1,y-1)+cells(x-1,y+1)+...
cells(x+1,y-1)+cells(x+1,y+1);
%The CA rule
cells=(sum==3)|(sum==2&cells);
%draw the new imag
set( imh,'cdata',cat(3,cells,z,z) )
%updata the step number diaplay
stepnumber=1+str2double( get(number,'string') );
set( number,'string',num2str( stepnumber ) )
end
if freeze==1
run=0;
freeze=0;
end
drawnow %need this in the loop for cintrol to work
%pause(0.5)
end
</p><p><p>
</p>
页:
[1]