Home > Back-end >  Conway's game of life in Matlab - function returning unexpected numbers when in nested loop
Conway's game of life in Matlab - function returning unexpected numbers when in nested loop

Time:04-24

Hey I am working on making Conway's game of life in Matlab for a project and so far I have created a function that finds the number of alive cells around the original cell, which I believe works as I have tested it and played around with it a fair amount, but when I implement it into my main script that contains the conditional rules for the game of life it seems to stop working.

function [alive] = cellStat(grid,row,col)
%this function finds the number of alive cells surrounding it
alive = 0;

    for i = row-1:row 1
        %making sure the cell is on the board
        if i <= 0 || i > length(grid)
            continue
        end

        for j = col-1:col 1
            %making sure the cell is on the board
            if j <= 0 || j > length(grid)
                continue
            end
            %making sure the cell is not counted as its own neighbour
            if i == row && j == col
                continue
            end
            %disp(i   " "   j   " = "   grid(i,j));
            if grid(i,j) == 1
                alive = alive   1;
            end
        end
    end
end

main script

r = [0 0 1 0 0;1 0 1 0 0;0 1 1 0 0;0 0 0 0 0;0 0 0 0 0];
years = 1;
alive = 0;

for n = 1:years
    for i = 1:length(r)
        for j = 1:length(r)
            alive = cellStat(r,i,j);
            if alive <= 1 && r(i,j) == 1
                r(i,j) = 0;
            elseif alive > 3 && r(i,j) == 1
                r(i,j) = 0;
            elseif alive == 3 && r(i,j) == 0
                r(i,j) = 1;
            end
        end
    end
    disp(r);
end

For example, I have been trying to test this for a glider pattern in Conway's game of life which is the array r in the previous code. But when I run the code the output is not as expected. Here is the command window, the first array is the initial array r

I also tried to debug it by putting in a disp function to find out what the cellStat function is returning throughout the for loops in the main script (disp(i " " j " = " alive);) right underneath the line that finds the number of alive cells surrounding the current cell, and it comes back with interesting results. For example it says the cell at row 2 column 2 has 6 alive neighbours, but there are not even 6 alive cells on the grid.

I assume this is an error with the for loops in the main script because when I use the function on that cell only it gives a correct result (5).

I'd appreciate some help as I'm not really sure where to start fixing this.

CodePudding user response:

You perform only one loop over the cells, in which you both count the neighbors and modify them. This does not work, because the modifications you do early in the loop change the number of neighbors that you count later on.

You need two loops: in the first you count the neighbors for all cells, in the second you modify each cell according to its number of neighbors.

  • Related