以前写的,主要是受到golly这个软件的启发(感觉是个很好玩的软件)
这个生命游戏可调节代数,存活条件,繁殖条件
like this
运行效果
代码:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189function 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内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复