Game of Life - my elements don't die, only grow!

2 views (last 30 days)
My Game of life program creates some cool patterns, but isnt working as i had hoped.
When i put in a glider pattern as the starting population, the patterns grow infinitely, not transitioning across screen as they should. Something must be wrong with my if statements that should control the elements 'dying.' My code is as follows:
matrix = zeros(500,500);
pop =[1,1,1;1,0,0;0,1,0];
matrix(250:252,250:252) = pop; %puts the initial population in the center of the matrix
while true
imshow(matrix);
k = [1 1 1;1 0 1;1 1 1];
neighbours = conv2(matrix, k,'same');
for row = 1:size(matrix, 1)
for col = 1:size(matrix,2)
if((matrix(row,col) == 1) && (neighbours(row,col)==2||3)) ||((matrix(row,col) == 0) && (neighbours(row,col)==3))
matrix(row,col) = 1;
else
matrix(row,col)=0;
end
end
end
end
thanks for your help
  1 Comment
John D'Errico
John D'Errico on 26 Sep 2017
Silly question here, I know. But why, if you recognize that conv2 gives you a neighbor count (well done in that respect by the way, if you found that trick yourself) why not do the propagation step in one simple array test?
nextgen = (matrix & ((neighbors == 2) | (neighbors == 3))) | (~matrix & (neighbors == 3));
No explicit loops needed at all. The trick to using a tool like MATLAB is to understand matrix operations. Let the computer write the loops.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 26 Sep 2017
Edited: Stephen23 on 26 Sep 2017
Have a closer look at your logic operations:
neighbours(row,col)==2||3
which is not an error, but it does not do what you think it does!. That line is actually equivalent to this (note the grouping parentheses that I added for clarity):
(neighbours(row,col)==2) || 3
which because all non-zero values are considered true is equivalent to
(neighbours(row,col)==2) || true
which is clearly equivalent to
X || true
which is equivalent to
true
Your mistake was to ignore the clearly documented operator precedence, which states clearly the priorities of the two operators that you are using:
"8. Less than (<), less than or equal to (<=), greater than (>), greater than or equal to (>=), equal to (==), not equal to (~=)"
...
"12. Short-circuit OR ()"
TIP: check your code as you write it. Read the documentation for each operation that you are using. Do not move on to the next line until you have tested and confirmed that the line does what you need it to do.
  2 Comments
Benjamin Barnett
Benjamin Barnett on 26 Sep 2017
Thanks so much!
I have rewritten the line as this:
(neighbours(row,col)==2||(neighbours(row,col)==3))
which gives me the correct output. Is there any way I can write this more concisely?
Thanks for your help, I knew it would be a stupid mistake!
Guillaume
Guillaume on 26 Sep 2017
Note that I don't know any language where X==a|b| would be equivalent to X==a|X==b|.
Another way to write the expression would be
ismember(X, [a, b])

Sign in to comment.

More Answers (0)

Categories

Find more on Conway's Game of Life in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!