(*:Title:  Life  *)

(*:Author: Wolfram Research, Inc. *)

(*:Copyright: Copyright 1992-1993, Wolfram Research, Inc. *)

(*:Package Version: 1.0 *)

(*:Mathematica Version: 2.0 *)

(*: Summary: This package gives an example of an implementation of a
two-dimensional Game of Life.
*)

(*:Keywords:  life
*)

(*:Discussion: *)

(*:Context: Examples`Life` *)

(*:Source: *)

(*:History:
	Header information added August 1992, Jeff Adams, Wolfram Research, Inc.
*)

(*:Limitations: *)


BeginPackage["Lez`Life2`"]

LifeRule::usage = "LifeRule[list] defines the rule to be applied to list during a LifeUpdate."

LifeUpdate::usage = "LifeUpdate[list] takes the elements in list one step forward by applying LifeRule."

AddObject::usage = "AddObject[list,{i,j}, template] adds the object represented
by template to list at location {i,j}."

Glider::usage = "Glider is a gliding life element."

LifeBlock::usage = "LifeBlock is a life block."

LifeEvolve::usage = "LifeEvolve[list, n] progresses the elements in list n steps forward."

ShowLife::usage = "ShowLife[list] displays a Graphics3D plot of the life steps contained in list."


(* Begin["`Private`"] *)

LifeRule[list_] := 
	Module[{ t },
		t = Count[list, 1, {2}] ;
		If[(list[[2, 2]] == 1 && t == 4) || t == 3, 1, 0]
	]

LifeUpdate[list_] := 
	Map[LifeRule, Partition[
		Wrap[ Map[Wrap, list]], {3, 3}, {1, 1}], {2}]

Wrap = Join[{Last[#]}, #, {First[#]}]&

AddObject[list_, {i_, j_}, template_] :=
	ReplacePart[list, 1, Table[{i, j}, {Length[template]}] + template]

Glider = {{-1, -1}, {0, -1}, {1, -1}, {1, 0}, {0, 1}}

LifeBlock = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}


LifeEvolve[init_, t_] := NestList[LifeUpdate, init, t]

ShowLife[tarray_] := Show[Graphics3D[ Cuboid[-Reverse[#], -Reverse[#] + 0.5]& 
			/@ Position[tarray, 1] ], ViewPoint -> {1, 2, 10}]

(* End[] *)

EndPackage[]


