Given a matrix A that represents the state of Conway's game of Life at one instant (time t=n), return the matrix B that represents the state of the game at the next instant (time t=n+1).
Assume a toroidal game board, so the edges of the matrix wrap left to right and top to bottom.
Examples:
Input A = [ 1 1 0 0 0 1 0 0 1 1 0 0 0 0 0 0 ]
Output B = [ 1 1 0 0 0 0 1 0 1 1 0 0 0 0 0 0 ]
Input A = [ 0 1 1 0 1 1 1 0 0 0 1 0 0 0 0 0 ]
Output B = [ 1 0 1 1 1 0 0 0 0 0 1 1 0 1 1 0 ]
I think test sets should be designed to encourage general solutions. At present, the test set only has square boards - some more varied examples would make solvers seek code that is general.
Is there something wrong with the test suite? function B = life(A) B = A; end produces "Error using life Too many output arguments." See comment on solution 1019804.
I suppose the error "Error using life Too many output arguments." is due to shadowing. It seems thet test suite calls \toolbox\matlab\demos\life.m
I had so much fun with this problem. Thank you.
Is there a syntax flaw in the Test Suite? Currently the first test displays with a first line of "%%A = [ ...", and in my solution (S 1435695) only the second test is displayed. Although apparently the first test is still being called (S 1435709)?
function B = gameOfLife(A)
B=A;
C=[A,A,A;A,A,A;A,A,A];
for i=5:8
for j=5:8
cnt=0;
for m=i-1:i+1
for n=j-1:j+1
if m==i&&n==j
cnt=cnt;
else
if C(m,n)==1
cnt=cnt+1;
end
end
end
end
if C(i,j)==1 %该细胞现在存活
if cnt>3||cnt<2
B(i-4,j-4)=0;
end
else %该细胞现在死亡
if cnt==3
B(i-4,j-4)=1;
end
end
end
end
end
Is there something wrong with the test suite?
function B = life(A)
B = A;
end
produces "Error using life Too many output arguments."
This solution is for only a 4x4 A. See Solution 598430 for general solution.
This is the solution for A of any size.
Ties the current leader in size, but works on all board sizes.
This stops working if the input is not 4x4.
Nice way of expanding A for circular convolution
Humph! Solution 2234 uses imfilter, but this fails with imfilter not found!
Nice - but only works for 4x4 boards.
Puzzling - solution 2231 uses imfilter but this solution fails because padarray isn't available. Both are in the IPT.
Nice usage of a simpler matrix for convolution (ones(3)) and readjustment of the logical conditions.
Good job!
I was looking for a command to do a circular convolution, but couldn't figure it out. This is useful to learn and, I guess, the whole point of Cody
However, learning is severely hindered by not being able to view solutions of smaller size. At least, as far as i know, this is not possible.
_All_ solutions in this Problem will become viewable after you solve _another_ Problem in the same category (which here is "Cody Challenge").