-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.m
87 lines (83 loc) · 2.32 KB
/
code.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
clear; clc; close all; format compact
load Downloads/StartingCells.mat
while(1)
figure(1);
set(gca,'Color',[0.2 0.50 1])
hold on
xlim([0 20])
ylim([0 20])
xticks([2:2:20])
yticks([2:2:20])
StartMarksPlotting = flipud(StartingCells);
for i = 1:size(StartMarksPlotting,1)
for j = 1:size(StartMarksPlotting,2)
if StartMarksPlotting(i,j)>0
plot(j,i,'ko','MarkerSize',12,'MarkerFaceColor','black')
end
end
end
m = input('Press s to step the simulation or q to quit: ','s');
if m == 's' || m == 'S'
r = StartingCells;
steps = 1;
alive = 0;
for n = 1:steps
for i = 1:length(r)
for j = 1:length(r)
alive = CellStat(r,i,j);
if alive <= 1 && r(i,j) == 1
r(i,j) = 0;
elseif alive > 3 && r(i,j) == 1
r(i,j) = 0;
elseif alive == 3 && r(i,j) == 0
r(i,j) = 1;
end
end
end
end
figure(2);
set(gca,'Color',[0.2 0.50 1])
hold on
xlim([0 20])
ylim([0 20])
xticks([2:2:20])
yticks([2:2:20])
newplot = flipud(r);
for i = 1:size(newplot,1)
for j = 1:size(newplot,2)
if newplot(i,j)>0
plot(j,i,'ko','MarkerSize',12,'MarkerFaceColor','black')
end
end
end
pause(0.5)
elseif m == 'q' || m == 'Q'
disp('Thanks for simulating life')
break
else
disp('Bad value entered. Please enter s or q')
end
end
%functions
function [alive] = CellStat(grid,row,col)
%this function finds the number of alive cells surrounding it
alive = 0;
for i = row-1:row+1
%making sure the cell is on the board
if i <= 0 || i > length(grid)
continue
end
for j = col-1:col+1
if j <= 0 || j > length(grid)
continue
end
%making sure the cell is not counted as its own neighbour
if i == row && j == col
continue
end
if grid(i,j) == 1
alive = alive + 1;
end
end
end
end