这个Matlab生活游戏出了什么问题?

时间:2013-03-12 19:53:27

标签: matlab conways-game-of-life

我试图在Matlab中编写Conway的生活游戏,但有些事情一直存在问题。我没有收到错误,所以我不知道自己做错了什么,它什么也做不了。我认为这个问题可能与细胞的计数有关(检查邻居的数量)或增加矩阵边界处细胞的规则。

这是我的代码:

TIME = 10;
pulsar;     % creates begin matrix X
life{1} = X;
life = {};           % create list '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 are starting off with in each step)
for i = 0:TIME;    % determine amount of times the loop will run
    nextnext = X;                     % create "nextnext" matrix to implement the rules of the game on (copy of X)
    for row = 2:numrows-1;              % for each row
        for column = 2:numcolumns-1;    % for each column
            east_of_row = column + 1;      % define how to count the cell right of target cell
            west_of_row = column - 1;      % define how to count the cell left of target cell             
            north_of_column = row - 1;     % define how to count the cell north of target cell              
            south_of_column = row + 1;     % define how to count the cell south of target cell               

            % count neighboring cells:
            neighbors = 0;                               % start counter 'neighbors' with 0

            while east_of_row <= size(X),
                 west_of_row <= size(X);,
                 north_of_column <= size(X);,
                 south_of_column <= size(X); 
                 if current(row,east_of_row) == 1              % if neighboring cell has a value of 1
                     neighbors + 1;                                 % add 1 to neighbors
                 end
                 if current(row,west_of_row) == 1              % if neighboring cell has a value of 1
                     neighbors + 1;                                  % add 1 to neighbors
                 end
                 if current(north_of_column,column) == 1       % if neighboring cell has a value of 1    
                     neighbors + 1;                                  % add 1 to neighbors
                 end
                 if current(south_of_column,column) == 1       % if neighboring cell has a value of 1    
                     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 + 1;                                   % add 1 to neighbors
                 end
                 if current(north_of_column,east_of_row) == 1  % if neighboring cell has a value of 1
                     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 + 1;                                  % add 1 to neighbors
                 end
                 if current(south_of_column,west_of_row) == 1  % if neighboring cell has a value of 1
                     neighbors + 1;                                   % add 1 to neighbors    
                 end
            end

    while east_of_row == 0,west_of_row == 0;,north_of_column == 0;,south_of_column == 0; 
    if current row,east_of_row ~= 0;
    if current(row,east_of_row) == 1              % if neighboring cell has a value of 1
        neighbors + 1;                                 % add 1 to neighbors
    end
    end
    if current row,west_of_row ~= 0; 
    if current(row,west_of_row) == 1              % if neighboring cell has a value of 1
        neighbors + 1;                                  % add 1 to neighbors
    end
    end
    if current north_of_column,column ~= 0;
    if current(north_of_column,column) == 1       % if neighboring cell has a value of 1
        neighbors + 1;                                  % add 1 to neighbors
    end
    end
    if current south_of_column,column ~= 0;
    if current(south_of_column,column) == 1       % if neighboring cell has a value of 1
        neighbors + 1;                                   % add 1 to neighbors
    end
    end
    if current south_of_column,east_of_row ~= 0;
    if current(south_of_column,east_of_row) == 1  % if neighboring cell has a value of 1
        neighbors + 1;                                   % add 1 to neighbors
    end
    end
    if current north_of_column,east_of_row  ~= 0;
    if current(north_of_column,east_of_row) == 1  % if neighboring cell has a value of 1
        neighbors + 1;                                  % add 1 to neighbors
    end
    end
    if current north_of_column,west_of_row ~= 0;
    if current(north_of_column,west_of_row) == 1  % if neighboring cell has a value of 1
        neighbors + 1;                                  % add 1 to neighbors
    end
    end
    if current south_of_column,west_of_row ~= 0;
    if current(south_of_column,west_of_row) == 1  % if neighboring cell has a value of 1
        neighbors + 1;                                   % add 1 to neighbors    
    end
    end
    end
    neigbors

        % 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 , neighbors == 3      % 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                        % 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+1} = nextnext;    % add matrix to list 'life
end


show(life);

1 个答案:

答案 0 :(得分:1)

哇。你的代码在很多方面都很糟糕......你是否听说过matlab中的矢量化?

要计算每个单元格的邻居数量,您可以通过以下方式完成:

neighbors = conv2( current, [1 1 1;1 0 1; 1 1 1], 'same' );

一旦你有了邻居的数量,你就可以简单地创建下一个时间步骤

nextnext = current.*( neighbors == 2 | neighbors == 3 ) + ... % all cells for which current == 1
           ( 1 - current ).*( neighbors == 3 ); 

您拥有的代码存在一些问题:

  1. neighbors + 1; % add 1 to neighbors实际上向邻居添加一行。 Matlab没有等效的neighbors++。所以,如果你真的想要增加neighbors,你必须明确地做到:neighbors = neighbors + 1;

  2. 我不知道为什么你使用while循环,但语句while east_of_row == 0,west_of_row == 0;,north_of_column == 0;,south_of_column == 0;只检查第一个条件east_of_row == 0,其余的被评估为表达式而不算作循环条件。

  3. 下次,如果您没有达到预期效果而且没有看到任何错误,请逐步尝试debugging您的代码。

  4. 是的,你试过吗

    >> life