The Game of Life is a   cellular automaton   devised by the British mathematician   John Horton Conway   in 1970.   It is the best-known example of a cellular automaton.

Conway's game of life is described   here:

A cell   C   is represented by a   1   when alive,   or   0   when dead,   in an   m-by-m   (or m×m)   square array of cells.

We calculate   N   - the sum of live cells in C's   eight-location neighbourhood,   then cell   C   is alive or dead in the next generation based on the following table:

   C   N                 new C
   1   0,1             ->  0  # Lonely
   1   4,5,6,7,8       ->  0  # Overcrowded
   1   2,3             ->  1  # Lives
   0   3               ->  1  # It takes three to give birth!
   0   0,1,2,4,5,6,7,8 ->  0  # Barren

Assume cells beyond the boundary are always dead.

The "game" is actually a zero-player game, meaning that its evolution is determined by its initial state, needing no input from human players.   One interacts with the Game of Life by creating an initial configuration and observing how it evolves.



Task

Although you should test your implementation on more complex examples such as the   glider   in a larger universe,   show the action of the blinker   (three adjoining cells in a row all alive),   over three generations, in a 3 by 3 grid.



References

   Its creator John Conway, explains   the game of life.   Video from numberphile on youtube.
   John Conway   Inventing Game of Life   - Numberphile video.


Related task

   Langton's ant   - another well known cellular automaton.


 
Module Life {
	Font "courier new"
	Form 40, 18
	Cls 3,0
	Double
	Report 2,  "Game of Life"
	Normal
	Cls 5, 2
	Const Mx=20, My=10
	Dim A(0 to Mx+1, 0 to My+1)=0
	Flush
REM	Data (2,2),(2,3),(3,3),(4,3),(5,4),(,3,4),(5,3),(6,2),(8,5),(5,8)
	Data (5,3)
	Data (5,4)
	Data (5,5)
	generation=1
	While not empty
		(A, B)=Array
		A(A,B)=1
	End While
	Display()
	Do
		k$=Key$
		If k$=chr$(13) Then exit
		A()=@GetNext()
		refresh 500
		Display()
	Until A()#Sum()=0
	K$=Key$
	Cls, 0
	End
	Function GetNext()
		Local B()
		B()=A()  ' copy array
		Local B=B()  ' get a pointer
		B*=0  ' make all element zero
		Local i, j, tot
		For j=1 to My
			For i=1 to Mx
				tot=-A(i,j)
				For k=j-1 to j+1
					For m=i-1 to i+1
						tot+=A(m, k)
					Next
				Next
				If A(i,j)=1 Then
					If tot=2 or tot=3 Then B(i,j)=1
				Else.if tot=3 Then
					B(i,j)=1
				End If
			Next
		Next
		=B()
	End Function
	Sub Display()
		Cursor 0,2
		move !    ' move graphic to character cursor
		Fill scale.x, scale.y-pos.Y, 1, 5
		Print "Generation:";Generation
		Generation++
		Local i, j
		For j=1 to My
			Print @(width div 2-Mx div 2);
			For i=1 to Mx
				Print If$(A(I,J)=1->"■", "□");
			Next
			Print
		Next
		Print 
		Report 2, "Press enter to exit or any other key for Next generation"
	End Sub
 
}
Life
 