function varargout = game_of_life(varargin)
% GAME_OF_LIFE M-file for game_of_life.fig
% GAME_OF_LIFE, by itself, creates a new GAME_OF_LIFE or raises the existing
% singleton*.
%
% H = GAME_OF_LIFE returns the handle to a new GAME_OF_LIFE or the handle to
% the existing singleton*.
%
% GAME_OF_LIFE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GAME_OF_LIFE.M with the given input arguments.
%
% GAME_OF_LIFE('Property','Value',...) creates a new GAME_OF_LIFE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before game_of_life_OpeningFunction gets called. An
% unrecognized property name or invalid value makes property application
% step. All inputs are passed to game_of_life_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help game_of_life
% Last Modified by GUIDE v2.5 04-Nov-2008 16:29:49
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @game_of_life_OpeningFcn, ...
'gui_OutputFcn', @game_of_life_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before game_of_life is made visible.
function game_of_life_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to game_of_life (see VARARGIN)
% Choose default command line output for game_of_life
handles.output = hObject;
global init_pushtool;
try
if ~isequal(init_pushtool,1)
error();
end
catch
init_pushtool=1;
load ICONH.mat;
uipushtool('cdata',fstart,'TooltipString','start','ClickedCallback',{@start_Callback,handles});
uipushtool('cdata',fstop,'TooltipString','stop','ClickedCallback',{@wait_Callback,handles});
uipushtool('cdata',ftraj,'TooltipString','step','ClickedCallback',{@step_Callback,handles});
uipushtool('cdata',fdraw,'TooltipString','new','ClickedCallback',{@new_Callback,handles});
end
global matrix;
global h;
global stop;
global inc;
global init;
global stop_step;
stop_step=0;
stop=0;
inc=1;
matrix=[];
h=[];
num=str2num(get(handles.N_number,'string'));
%h=zeros(num);
axes(handles.axes1);
axis([0 num 0 num]);
for i=0:num-1
line([i,i],[0,num],'color','k');
line([0,num],[i,i],'color','k');
end
for i=1:num
for j=1:num
h(i,j)=rectangle('position',[(i-inc) (j-inc) inc inc],'facecolor','r','visible','off','edgecolor','k','erasemode','xor');
%h(i,j)=rectangle('position',[(i-inc) (j-inc) inc inc],'facecolor','r','erasemode','xor');
end
end
set(handles.text3,'string','New');
matrix=zeros(num+2);
init=0;
set(handles.pushbutton8,'string','Add on');
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes game_of_life wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = game_of_life_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in start.
function start_Callback(hObject, eventdata, handles)
% hObject handle to start (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% f=handles{1};
% g=handles{2};
global stop;
global init;
global stop_step;
if stop_step==0
stop=0;
else
stop=1;
stop_step=0;
end
init=1;
global matrix;
global inc;
global h;
len=length(matrix);
matrix_2=zeros(len);
set(handles.text3,'string','Start')
while(1)
oldmatrix=matrix;
if length(oldmatrix)~=length(matrix)
break;
end
matrix(1,:)=matrix(len-1,:);
matrix(len,:)=matrix(2,:);
matrix(:,1)=matrix(:,len-1);
matrix(:,len)=matrix(:,2);
for i=2:len-1
for j=2:len-1
temp=sum(matrix(i-1,j-1:j+1))+sum(matrix(i+1,j-1:j+1))+matrix(i,j-1)+matrix(i,j+1);
if (temp>=2&&temp<=3)&&matrix(i,j)==1
matrix_2(i,j)=1;
elseif temp==3&&matrix(i,j)==0
matrix_2(i,j)=1;
else
matrix_2(i,j)=0;
end
end
end
matrix=matrix_2;
if isequal(matrix,oldmatrix)
break;
else
drawmat=matrix(2:len-1,2:len-1);
draw(drawmat);
end
drawnow;
T=str2num(get(handles.edit2,'string'));
pause(T);
if stop==1
break;
end
end
guidata(hObject,handles);
% --- Executes on mouse press over axes background.
function axes1_ButtonDownFcn(hObject, eventdata, handles)
% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function figure1_WindowButtonDownFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global inc;
global matrix;
global h;
global init;
global add;
%if (init==0|add==1)
if isequal(get(handles.pushbutton8,'string'),'Add on')
cp=get(gca,'currentpoint');
x=cp(1,1);
y=cp(1,2);
num_x=ceil(x/inc)*inc;
num_y=ceil(y/inc)*inc;
% matrix_i=ceil(x/inc)+1;
% matrix_j=ceil(y/inc)+1;
if num_x>0&&num_x<=length(matrix)-2&&num_y>0&&num_y<=length(matrix)-2
axes(handles.axes1);
if matrix(num_x+1,num_y+1)==0
matrix(num_x+1,num_y+1)=1;
set(h(num_x,num_y),'visible','on');
else
matrix(num_x+1,num_y+1)=0;
set(h(num_x,num_y),'visible','off');
end
;
end
end
% --- Executes on button press in wait.
function wait_Callback(hObject, eventdata, handles)
% hObject handle to wait (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.text3,'string','Wait');
guidata(handles.text3,handles);
uiwait;
;
function N_number_Callback(hObject, eventdata, handles)
% hObject handle to N_number (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of N_number as text
% str2double(get(hObject,'String')) returns contents of N_number as a double
% global matrix;
% if matrix==0
% axes(handles.axes1);
% global inc;
% global matrix;
% global h;
% num=str2num(get(handles.N_number,'string'));
% %num=25;
% matrix=zeros(num+2);
% inc=1;
% for
- 1
- 2
前往页