您当前的位置: 首页 >  游戏

slandarer

暂无认证

  • 1浏览

    0关注

    248博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

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

slandarer 发布时间:2019-07-21 13:41:14 ,浏览量:1

以前写的,主要是受到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(A2)=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            
关注
打赏
1664692598
查看更多评论
0.0483s