Game Of Life

       The Game of Life,also known simply as Life,is a cellular automaton devised by the British mathematician John Horton Conway in 1970.The universe of the Game of Life is   an infinite two-dimensional orthogonal grid of square cells,each of which is in one   of two possible states,alive or dead.Every cell interacts with its eight  neighbors,which are the cells that are horizontally,vertically,or diagonally  adjacent.At each step in time,the following transitions occur:
     (1)Any live cell with fewer than two live neighbors dies,as if caused by under-
population.
     (2)Any live cell with two or three live neighbors lives on to the next generation.

     (3)Any live cell with more than three live neighbors dies,as if by overcrowding.

     (4)Any dead cell with exactly three live neighbors becomes a live cell,as if by  reproduction.

     The next picture gives an example.The live cell is the block with a black point in the figure,and the dead one is nothing in the block.

    

       so,we can implement the Game Of Life idea in matlab code.

       function [ m_evo ] = Game_Of_Life( matrix )
          [d1 d2] = size(matrix);
          m_evo = zeros(d1, d2);
          for i=1:d1
              for j=1:d2
                  num = count_neighbour(matrix, i, j);
                  if matrix(i,j) == 1
                      if num==2 || num==3
                         m_evo(i,j) = 1;
                      else
                         m_evo(i,j) = 0;
                     end
               else
                    if num == 3
                        m_evo(i,j) = 1;
                   end
              end
          end
      end
  end



function [ num ] = count_neighbour( matrix, i, j )
nb = zeros(1, 8);
[d1 d2] = size(matrix);
if i==1 && j==1
    nb(1) = matrix(1,2);
    nb(2) = matrix(2,1);
    nb(3) = matrix(2,2);
    nb(4) = matrix(1,d2);
    nb(5) = matrix(d1,1);
    nb(6) = matrix(d1,d2);
    nb(7) = matrix(2,d2);
    nb(8) = matrix(d1,2);
elseif i==1 && j==d2
    nb(1) = matrix(1,d2-1);
    nb(2) = matrix(2,d2);
    nb(3) = matrix(2,d2-1);
    nb(4) = matrix(1,1);
    nb(5) = matrix(d1,1);
    nb(6) = matrix(d1,d2);
    nb(7) = matrix(2,1);
    nb(8) = matrix(d1,d2-1);
elseif i==d1 && j==d2
    nb(1) = matrix(d1,d2-1);
    nb(2) = matrix(d1-1,d2);
    nb(3) = matrix(d1-1,d2-1);
    nb(4) = matrix(1,1);
    nb(5) = matrix(d1,1);
    nb(6) = matrix(1,d2);
    nb(7) = matrix(1,d2-1);
    nb(8) = matrix(d1-1,1);
elseif i==d1 && j==1
    nb(1) = matrix(d1,2);
    nb(2) = matrix(d1-1,1);
    nb(3) = matrix(d1-1,2);
    nb(4) = matrix(1,1);
    nb(5) = matrix(d1,d2);
    nb(6) = matrix(1,d2);
    nb(7) = matrix(1,2);
    nb(8) = matrix(d1-1,d2);
elseif i==1
    nb(1) = matrix(d1,j-1);
    nb(2) = matrix(d1,j);
    nb(3) = matrix(d1,j+1);
    nb(4) = matrix(i,j-1);
    nb(5) = matrix(i,j+1);
    nb(6) = matrix(i+1,j-1);
    nb(7) = matrix(i+1,j);
    nb(8) = matrix(i+1,j+1);
elseif j==1
    nb(1) = matrix(i-1,d2);
    nb(2) = matrix(i-1,j);
    nb(3) = matrix(i-1,j+1);
    nb(4) = matrix(i,d2);
    nb(5) = matrix(i,j+1);
    nb(6) = matrix(i+1,d2);
    nb(7) = matrix(i+1,j);
    nb(8) = matrix(i+1,j+1);
elseif i==d1
    nb(1) = matrix(i-1,j-1);
    nb(2) = matrix(i-1,j);
    nb(3) = matrix(i-1,j+1);
    nb(4) = matrix(i,j-1);
    nb(5) = matrix(i,j+1);
    nb(6) = matrix(1,j-1);
    nb(7) = matrix(1,j);
    nb(8) = matrix(1,j+1);
elseif j==d2
    nb(1) = matrix(i-1,j-1);
    nb(2) = matrix(i-1,j);
    nb(3) = matrix(i-1,1);
    nb(4) = matrix(i,j-1);
    nb(5) = matrix(i,1);
    nb(6) = matrix(i+1,j-1);
    nb(7) = matrix(i+1,j);
    nb(8) = matrix(i+1,1);
else
    nb(1) = matrix(i-1,j-1);
    nb(2) = matrix(i-1,j);
    nb(3) = matrix(i-1,j+1);
    nb(4) = matrix(i,j-1);
    nb(5) = matrix(i,j+1);
    nb(6) = matrix(i+1,j-1);
    nb(7) = matrix(i+1,j);
    nb(8) = matrix(i+1,j+1);
end
num = length(find(nb==1));
end



你可能感兴趣的:(Game,Of,Life)