-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConway.m
48 lines (40 loc) · 1009 Bytes
/
Conway.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
% Any live cell with two or three live neighbours survives.
% Any dead cell with three live neighbours becomes a live cell.
% All other live cells die in the next generation. Similarly, all other dead cells stay dead.
clear
clf
N = 20; % number of rows
M = 25; % number of columns
rows = 1:N;
topRows = mod(rows-2, N) + 1;
bottomRows = mod(rows, N) + 1;
cols = 1:M;
leftCols = mod(cols-2, M) + 1;
rightCols = mod(cols, M) + 1;
# Random start
grid = randi([0,1], N, M);
imshow(grid)
axis('equal')
title('0')
for n = 1:100
# Get the 8 neighbours on a 3D array and sum them on the 3rd dimension
neighbours = sum(
cat(
3,
grid(topRows, leftCols),
grid(topRows, cols),
grid(topRows, rightCols),
grid(rows, leftCols),
grid(rows, rightCols),
grid(bottomRows, leftCols),
grid(bottomRows, cols),
grid(bottomRows, rightCols)
)
,3);
# Conways game of life
grid = ((grid & neighbours==2) | neighbours==3 );
# Display
imshow(grid)
title(n)
drawnow
end