I'm new to matlab and trying to code game of life. But i have some difficulties making the sum of the neigbors. Every cell can have a value of 0 or 1. I'm trying to use a counter (like in Python, the only program i'm somewhat familiar with) but that doesn't seem to work. The problem is that is has to work for all cells, so also the border cells. If i have the value of each cell (so that will be somewhere between 0 and 8) i have to implement the rules. But I also don't know if that's correct. Please help! I'm getting desperate!
TIME = 50;
pulsar;
life = {}; % create list 'life'
life{1} = X; % add seed to life
numrows = size(X,1); % calculate number of rows
numcolumns = size (X,2); % calculate number of columns
current = X; % make seed the first current(matrix you're starting off with in each step)
for i = 0:TIME; % determine amount of times the loop will run
for row = 1:numrows; % for each row
for column = 1:numcolumns; % for each column
if column + 1 ~= numcolumns + 1
east_of_row = column + 1; % define how to count the cell right of target cell
end
if column - 1 ~= 0
west_of_row = column - 1; % define how to count the cell left of target cell
end
if row - 1 ~= 0
north_of_column = row - 1; % define how to count the cell north of target cell
end
if row + 1 ~= numrows + 1
south_of_column = row + 1;
end
neighbors = 0 % start counter 'neighbors' with 0
if current(row,east_of_row) == 1; % if neighboring cell has a value of 1
neighbors = neighbors + 1; % add 1 to neighbors
end
if current(row,west_of_row) == 1; % if neighboring cell has a value of 1
neighbors = neighbors + 1; % add 1 to neighbors
end
if current(north_of_column,column) == 1; % if neighboring cell has a value of 1
neighbors = neighbors + 1; % add 1 to neighbors
end
if current(south_of_column,column) == 1; % if neighboring cell has a value of 1
neighbors = neighbors + 1; % add 1 to neighbors
end
if current(south_of_column,east_of_row) == 1; % if neighboring cell has a value of 1
neighbors = neighbors + 1; % add 1 to neighbors
end
if current(north_of_column,east_of_row) == 1; % if neighboring cell has a value of 1
neighbors = neighbors + 1; % add 1 to neighbors
end
if current(north_of_column,west_of_row) == 1; % if neighboring cell has a value of 1
neighbors = neighbors + 1; % add 1 to neighbors
end
if current(south_of_column,west_of_row) == 1; % if neighboring cell has a value of 1
neighbors = neighbors + 1; % add 1 to neighbors
end
% rules of the game:
if current(row,column) == 1; % in case a target cell has a value of 1:
if neighbors < 2 % if the number of neighbors is smaller than 2
nextnext(row,column) = 0; % value of target cell gets 0 in nextnext
end
if neighbors == 2 % if the number of neighbors is 2 or 3
nextnext(row,column) = 1; % value of target cell stays 1 in nextnext
end
if neighbors == 3
nextnext(row,column) = 1;
end
if neighbors > 3 % if the number of neigbors is higher than 3
nextnext(row,column) = 0; % value of target cell gets 0 in nextnext
end
end
if current (row,column) == 0 % in case a target cell has a value of 0:
if neighbors == 3 % if the number of neighbors is 3
nextnext(row,column) = 1; % value of target cell gets 1 in nextnext
end
if neighbors ~= 3 % if the number of neigbors isn't 3
nextnext(row,column) = 0; % value of target cell stays 0 in nextnext
end
end
end
end
current = nextnext; % make nextnext matrix the current matrix for the next step
%life{TIME} = nextnext; % add matrix to list 'life
end
show(life);