home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 3.ddi / DEMOS.DI$ / LIFE.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  2.6 KB  |  96 lines

  1. %LIFE    MATLAB's version of Conway's Game of Life.
  2. %    Demonstrates sparse matrices and Handle Graphics.
  3.  
  4. %    C. Moler, 7-11-92, 8-7-92.
  5. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  6.  
  7. fig = clf;
  8. clc
  9. echo on
  10.  
  11. % "Life" is a cellular automaton invented by John Conway that involves
  12. % live and dead cells in a rectangular, two-dimensional universe.
  13. % In MATLAB, the universe is sparse matrix which is initially all zero.
  14.  
  15. m = 101;
  16. X = sparse(m,m);
  17.  
  18. % Press any key to continue after pauses.
  19. pause
  20.  
  21. clc
  22.  
  23. % The initial population for this demonstration consists
  24. % of a "blinker" and two "gliders".
  25.  
  26. p = -1:1;
  27. blinker = [0 1 0; 0 1 0; 0 1 0];
  28. glider =  [0 1 0; 1 1 0; 1 0 1];
  29. k = 40;  X(k+p,k+p) = blinker;
  30. k = 51;  X(k+p,k+p) = glider;
  31. k = 69;  X(k+p,k+p) = glider;
  32.  
  33. % The following statements plot the initial configuration.
  34. % The "find" function returns the indices of the nonzero elements.
  35.  
  36. [i,j] = find(X);
  37. figure(fig);
  38. plothandle = plot(i,j,'.','erasemode','background','markersize',12);
  39. axis([0 m+1 0 m+1]);
  40.  
  41. pause
  42.  
  43. clc
  44.  
  45. % Whether cells stay alive, die, or generate new cells depends
  46. % upon how many of their eight possible neighbors are alive.
  47. % Here we generate index vectors for four of the eight neighbors.
  48. % We use periodic (torus) boundary conditions at the edges of the universe.
  49.  
  50. n = [m 1:m-1];
  51. e = [2:m 1];
  52. s = [2:m 1];
  53. w = [m 1:m-1];
  54.  
  55. pause
  56.  
  57.  
  58. clc
  59. % The computation is concisely written in a second M-file, lifeloop.m.
  60. type lifeloop
  61. pause
  62. clc
  63.  
  64. % The progress of the simulation is controlled by t and tfinal.
  65. % Initially,
  66.  
  67. tfinal = 0;
  68. t = 0;
  69.  
  70. % The variable t is incremented in the inner loop.
  71. % The value of tfinal is changed by four pushbuttons.
  72. % See life.m for the details.
  73.  
  74. % Click "Step" to set tfinal to t+1 and get one step.
  75. % Click "Go" to set tfinal to inf and initiate an infinite loop.
  76. % Click "Stop" to set tfinal to t and terminate the loop.
  77. % Click "Done" to make tfinal negative and clear the figure.
  78.  
  79.  
  80. echo off
  81.  
  82. on = 'set(gohandle,''back'',''cyan''); ';
  83. off = 'set(gohandle,''back'',''default''); ';
  84. figure(fig);
  85. stephandle = uicontrol('position',[100 5 50 25],'string','step', ...
  86.     'callback',[on 'tfinal = t+1; lifeloop; ' off]);
  87. gohandle = uicontrol('position',[170 5 50 25],'string','go', ...
  88.     'callback',['if finite(tfinal),' on 'tfinal = inf; lifeloop; end'], ...
  89.     'interruptible','yes');
  90. stophandle = uicontrol('position',[240 5 50 25],'string','stop', ...
  91.     'callback',[off 'tfinal = t;']);
  92. donehandle = uicontrol('position',[310 5 50 25],'string','done', ...
  93.     'callback',[off 'tfinal = -1; clf']);
  94.  
  95. echo off
  96.