How to resolve the algorithm Conway's Game of Life step by step in the Egel programming language
How to resolve the algorithm Conway's Game of Life step by step in the Egel programming language
Table of Contents
Problem Statement
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: 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.
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.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Conway's Game of Life step by step in the Egel programming language
Source code in the egel programming language
import "prelude.eg"
import "io.ego"
using System
using List
using IO
def boardsize = 5
def empty = [ X Y -> 0 ]
def insert =
[ X Y BOARD ->
[ X0 Y0 -> if and (X0 == X) (Y0 == Y) then 1
else BOARD X0 Y0 ] ]
def coords =
let R = fromto 0 (boardsize - 1) in
[ XX YY -> map (\X -> map (\Y -> X Y) YY) XX ] R R
def printcell =
[ 0 -> print ". "
| _ -> print "* " ]
def printboard =
[ BOARD ->
let M = map [XX -> let _ = map [(X Y) -> printcell (BOARD X Y)] XX in print "\n" ] coords in
nop ]
def count =
[ BOARD, X, Y ->
(BOARD (X - 1) (Y - 1)) + (BOARD (X) (Y - 1)) + (BOARD (X+1) (Y - 1)) +
(BOARD (X - 1) Y) + (BOARD (X+1) Y) +
(BOARD (X - 1) (Y+1)) + (BOARD (X) (Y+1)) + (BOARD (X+1) (Y+1)) ]
def next =
[ 0 N -> if N == 3 then 1 else 0
| _ N -> if or (N == 2) (N == 3) then 1 else 0 ]
def updateboard =
[ BOARD ->
let XX = map (\(X Y) -> X Y (BOARD X Y) (count BOARD X Y)) (flatten coords) in
let YY = map (\(X Y C N) -> X Y (next C N)) XX in
foldr [(X Y 0) BOARD -> BOARD | (X Y _) BOARD -> insert X Y BOARD ] empty YY ]
def blinker =
(insert 1 2) @ (insert 2 2) @ (insert 3 2)
def main =
let GEN0 = blinker empty in
let GEN1 = updateboard GEN0 in
let GEN2 = updateboard GEN1 in
let _ = map [ G -> let _ = print "generation:\n" in printboard G ] {GEN0, GEN1, GEN2} in
nop
You may also check:How to resolve the algorithm Write entire file step by step in the VBA programming language
You may also check:How to resolve the algorithm N-queens problem step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Fibonacci word step by step in the Sidef programming language
You may also check:How to resolve the algorithm Reflection/List methods step by step in the Java programming language
You may also check:How to resolve the algorithm Benford's law step by step in the R programming language