# Need help with Game of life

1 view (last 30 days)
Daniel Hodgson on 27 Aug 2020
Answered: Vimal Rathod on 31 Aug 2020
%I have school project which requires to programme Game of life. What im looking for is tips and trick to how to imporve my current code, im quite new to matlab and this is only the begining of my project but im getting stuck at some place and would really appriciate some help. I have tried to be as clear as possible.
clc, clear all
%figure below is mandatory starting condition
b=zeros(16) ;%matrix all zeros
b(5:9,5)=1 ;% left ones
b(7:11,11)=1 ;%right ones
b(5,7:11)=1 ;%top ones
b(11,5:9)=1; %bottom ones
spy(b,40,'g'); % plot picture
a=b;
[y,x]=find(a)%helps find all ones in matrix
for i=1:length(x) %trying to calcule neighbours around each cell containing one
c=a(y(i)+1,x(i))+a(y(i)-1,x(i))+a(y(i),x(i)+1)+a(y(i),x(i)-1)...
+a(y(i)+1,x(i)+1)+a(y(i)+1,x(i)-1)+a(y(i)-1,x(i)+1)+a(y(i)-1,x(i)-1)
%WANT TO DO: plug c values into vector then change vector(vales) with conditions below
%then plug back into matrix a replacing the numbers that were there before
if c<2
a(y(i),x(i))=0
elseif c or(2, 3)
a(y(i),x(i))=1
elseif c>3
a(y(i),x(i))=0
end
end

Vimal Rathod on 31 Aug 2020
You could use the conv2 function to find out the elements in the neighbours and then calculate the sum directly.
Below is a link for an answer which could be of use to you.
hope this helps!