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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值