-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlife.m
148 lines (109 loc) · 3.78 KB
/
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
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
function present = life( varargin )
%Conway's Game of Life
% Various input types:
%
% life(); select size and points of life
% life(M,N, 'select'); %predefined size, still select points of life
% note: literally type 'select'
%
% life(pre_defined_binary_matrix)
%
% life(M,N); generates world of size MxN with random points of life
% life(M,N, sparsity_level) generates world of size MxN with a level of
% sparsity between 0 (all points filled)
% to 1 (no points filled)
%
%
%
PAUSE_TIME = 0.3; %number of seconds each time step is presented
%Handle inputs ------------------------------
if size(varargin, 2) == 0
[present fh] = select_input();
set(fh, 'numbertitle', 'off', 'name', 'Conway''s Game of Life');
elseif size(varargin, 2) == 1 %predefined initial conditions matrix
present = varargin{1};
fh = figure('numbertitle', 'off', 'name', 'Conway''s Game of Life');
elseif size(varargin, 2) == 2 %predefined size MxN
M = varargin{1};
N = varargin{2};
present = rand(M, N) > 0.5;
fh = figure('numbertitle', 'off', 'name', 'Conway''s Game of Life');
elseif size(varargin, 2) == 3 %predefined size and sparsity (or size and selection option)
M = varargin{1};
N = varargin{2};
if isnumeric(varargin{3})
sparsity = varargin{3};
present = rand(M,N) > sparsity;
fh = figure('numbertitle', 'off', 'name', 'Conway''s Game of Life');
elseif strcmp(varargin{3}, 'select')
[present fh] = select_input(M, N);
set(fh, 'numbertitle', 'off', 'name', 'Conway''s Game of Life');
end
else
sprintf('%s', 'Incorrect number of inputs!')
end
%--------------------------------------------
while ishandle(fh)
imagesc(present);
pause(PAUSE_TIME);
present = update(present);
end
end
function future = update(present)
[M N] = size(present); %assumes m, n > 1
future = zeros(M, N); %dead by default (life is shit)
for i = 1:M
for j = 1:N
if i == 1, up = i; down = i+1; %top row
elseif i == M, up = i-1; down = i; %bottom row
else up = i-1; down = i+1;
end
if j == 1, left = j; right = j+1; %left column
elseif j == N, left = j-1; right = j; %right column
else left = j-1; right = j+1;
end
neighbors = sum(sum(present(up:down, left:right)));
%Check living rules
if present(i, j)
neighbors = neighbors - 1; %no self counting
%live only if you have exactly 2 or 3 neighbors
if neighbors == 2 || neighbors == 3
future(i,j) = 1;
end
%future is initialized to death by default
else
%check dead rules
if neighbors == 3
future(i,j) = 1;
end
end
end
end
end
function [present, fh] = select_input(M, N)
if nargin == 0
prompt = {'Enter matrix height:', 'Enter matrix width:'};
dlg_title = 'World Size';
numLines = 1;
default = {'20', '20'};
answer = inputdlg(prompt,dlg_title,numLines,default);
M = str2double(answer{1});
N = str2double(answer{2});
end
present = zeros(M, N);
finishedSelection = false;
fh = figure('numbertitle', 'off', 'name', 'Press Enter to Start...');
while ~finishedSelection
imagesc(present);
[j i] = myginput(1, 'crosshair');
if size(j,1) > 0 %if a value was selected
if present(round(i), round(j)) == 0
present(round(i),round(j)) = 1;
else
present(round(i), round(j)) = 0;
end
else
finishedSelection = true;
end
end
end