%% PLAY & SHOW width=100; dot_rate=0.5; board=round(dot_rate*rand(width+2,width+2)); t=1; times=1000; while t<=times board=generate_step(board); imshow(zoom_out(board)); t=t+1; end %% RULES function state=life_rules(fore_state,neighbor_state) state((fore_state==1&&(neighbor_state==2||neighbor_state==3))||(fore_state==0&&neighbor_state==3))=1; state((fore_state==1&&(neighbor_state<=1||neighbor_state>=4))||(fore_state==0&&neighbor_state~=3))=0; end function board=generate_step(fore_board) expand_width=size(fore_board,1); board(1,1)=fore_board(expand_width-1,expand_width-1);% Cross assignment of four boundary points board(expand_width,expand_width)=fore_board(2,2); board(1,expand_width)=fore_board(expand_width-1,2); board(expand_width,1)=fore_board(2,expand_width-1); board(1,2:expand_width-1)=fore_board(expand_width-1,2:expand_width-1);% Cross assignment of four boundary bars board(expand_width,2:expand_width-1)=fore_board(2,2:expand_width-1); board(2:expand_width-1,1)=fore_board(2:expand_width-1,expand_width-1); board(2:expand_width-1,expand_width)=fore_board(2:expand_width-1,2); for i=2:expand_width-1 for j=2:expand_width-1 neighbor_state=sum(sum(fore_board(i-1:i+1,j-1:j+1)))-fore_board(i,j); board(i,j)=life_rules(fore_board(i,j),neighbor_state); end end end function expand_board=zoom_out(board) width=size(board,1); expand_rate=7;% 7×7 pixels displayed as a cell. expand_board=zeros(expand_rate*width); for i=1:width for j=1:width expand_board(expand_rate*(i-1)+1:expand_rate*i,expand_rate*(j-1)+1:expand_rate*j)=board(i,j); end end end