-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDraft_Game_Life.m
121 lines (110 loc) · 2.99 KB
/
Draft_Game_Life.m
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
% Draft
% Game of life
%n matrix dimension
%spsize matrix sparse size
%tf final time
function Draft_Game_Life(n,spsize,tf)
if nargin<3
n=20;
spsize=0.3;
tf=100;
end
A=sprand(n,n,spsize);
B=spones(A);
%B=full(B);
% spy(B)
[row,col]=find(B);
pop=zeros(1,tf);
for t=1:tf
pop(t)=sum(sum(B));
%Matriz B modification
for i=1:length(row)
%% Treatment of edges
%First line
if row(i)==1
%First corner
if col(i)==1
neighbors = B(row(i)+1,col(i))+B(row(i),col(i)+1)+B(row(i)+1,col(i)+1);
if neighbors ==3 || neighbors ==1
B(row(i),col(i))=0;
else
B(row(i),col(i))=1;
end
%Second corner
elseif col(i)==n
neighbors = B(row(i)+1,col(i))+B(row(i),col(i)-1)+B(row(i)+1,col(i)-1);
if neighbors ==3 || neighbors ==1
B(row(i),col(i))=0;
else
B(row(i),col(i))=1;
end
else
neighbors = B(row(i)+1,col(i))+B(row(i),col(i)-1)+B(row(i)+1,col(i)-1)+B(row(i)+1,col(i)+1)+B(row(i),col(i)+1);
if neighbors >=3 || neighbors ==1
B(row(i),col(i))=0;
else
B(row(i),col(i))=1;
end
end
%End line
elseif row(i)==n
%Third corner
if col(i)==1
neighbors = B(row(i)-1,col(i))+B(row(i),col(i)+1)+B(row(i)-1,col(i)+1);
if neighbors ==3 || neighbors ==1
B(row(i),col(i))=0;
else
B(row(i),col(i))=1;
end
%Fourth corner
elseif col(i)==n
neighbors = B(row(i)-1,col(i))+B(row(i),col(i)-1)+B(row(i)-1,col(i)-1);
if neighbors ==3 || neighbors ==1
B(row(i),col(i))=0;
else
B(row(i),col(i))=1;
end
else
neighbors = B(row(i)-1,col(i))+B(row(i),col(i)-1)+B(row(i)-1,col(i)-1)+B(row(i)-1,col(i)+1)+B(row(i),col(i)+1);
if neighbors >=3 || neighbors ==1
B(row(i),col(i))=0;
else
B(row(i),col(i))=1;
end
end
%First column
elseif col(i)==1
if row(i)~=1 && row(i)~=n
neighbors = B(row(i)-1,col(i))+B(row(i),col(i)+1)+B(row(i)+1,col(i)+1)+B(row(i)-1,col(i)+1)+B(row(i)+1,col(i));
if neighbors >=3 || neighbors ==1
B(row(i),col(i))=0;
else
B(row(i),col(i))=1;
end
end
%Last column
elseif col(i)==n
if row(i)~=1 && row(i)~=n
neighbors = B(row(i)-1,col(i))+B(row(i),col(i)-1)+B(row(i)+1,col(i)-1)+B(row(i)-1,col(i)-1)+B(row(i)+1,col(i));
if neighbors >=3 || neighbors ==1
B(row(i),col(i))=0;
else
B(row(i),col(i))=1;
end
end
else
neighbors = B(row(i)-1,col(i)+1)+B(row(i)+1,col(i)+1)+B(row(i),col(i)+1)+B(row(i)-1,col(i))+B(row(i),col(i)-1)+B(row(i)+1,col(i)-1)+B(row(i)-1,col(i)-1)+B(row(i)+1,col(i));
if neighbors >=3 || neighbors ==1
B(row(i),col(i))=0;
else
B(row(i),col(i))=1;
end
end
end
spy(B)
title(['Tempo=', num2str(t), ' de ', num2str(tf)])
pause(0.2)
end
figure
plot(pop);
end