home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / e / e007 / 3.img / FILES / EXAMPLES.PAK / LIFE.M < prev    next >
Encoding:
Text File  |  1992-07-29  |  892 b   |  32 lines

  1.  
  2. (*: Summary: This package gives an example of an impementation of a
  3. two-dimensional Game of Life.
  4. *)
  5.  
  6. LifeRule[list_] := 
  7.         Module[{ t },
  8.                 t = Count[list, 1, {2}] ;
  9.                 If[(list[[2, 2]] == 1 && t == 4) || t == 3, 1, 0]
  10.         ]
  11.  
  12. LifeUpdate[list_] := 
  13.         Map[LifeRule, Partition[
  14.                 Wrap[ Map[Wrap, list]], {3, 3}, {1, 1}], {2}]
  15.  
  16. Wrap = Join[{Last[#]}, #, {First[#]}]&
  17.  
  18. AddObject[list_, {i_, j_}, template_] :=
  19.         ReplacePart[list, 1, Table[{i, j}, {Length[template]}] + template]
  20.  
  21. Glider = {{-1, -1}, {0, -1}, {1, -1}, {1, 0}, {0, 1}}
  22.  
  23. LifeBlock = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}
  24.  
  25.  
  26. LifeEvolve[init_, t_] := NestList[LifeUpdate, init, t]
  27.  
  28. ShowLife[tarray_] := Show[Graphics3D[ Cuboid[-Reverse[#], -Reverse[#] + 0.5]& 
  29.                         /@ Position[tarray, 1] ], ViewPoint -> {1, 2, 10}]
  30.  
  31.         
  32.