How to resolve the algorithm Conway's Game of Life step by step in the E programming language
How to resolve the algorithm Conway's Game of Life step by step in the E 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 E programming language
Source code in the e programming language
def gridWidth := 3
def gridHeight := 3
def X := 0..!gridWidth
def Y := 0..!gridHeight
def makeFlexList :=
def makeGrid() {
def storage := makeFlexList.fromType(, gridWidth * gridHeight)
storage.setSize(gridWidth * gridHeight)
def grid {
to __printOn(out) {
for y in Y {
out.print("[")
for x in X {
out.print(grid[x, y].pick("#", " "))
}
out.println("]")
}
}
to get(xb :int, yb :int) {
return if (xb =~ x :X && yb =~ y :Y) {
storage[y * gridWidth + x]
} else {
false
}
}
to put(x :X, y :Y, c :boolean) {
storage[y * gridWidth + x] := c
}
}
return grid
}
def mooreNeighborhood := [[-1,-1],[0,-1],[1,-1],[-1,0],[1,0],[-1,1],[0,1],[1,1]]
def computeNextLife(prevGrid, nextGrid) {
for y in Y {
for x in X {
var neighbors := 0
for [nx, ny] ? (prevGrid[x+nx, y+ny]) in mooreNeighborhood {
neighbors += 1
}
def self := prevGrid[x, y]
nextGrid[x, y] := (self && neighbors == 2 || neighbors == 3)
}
}
}
var currentFrame := makeGrid()
var nextFrame := makeGrid()
currentFrame[1, 0] := true
currentFrame[1, 1] := true
currentFrame[1, 2] := true
for _ in 1..3 {
def frame := nextFrame
computeNextLife(currentFrame, frame)
nextFrame := currentFrame
currentFrame := frame
println(currentFrame)
}
You may also check:How to resolve the algorithm Unbias a random generator step by step in the Rust programming language
You may also check:How to resolve the algorithm File size step by step in the Ruby programming language
You may also check:How to resolve the algorithm Law of cosines - triples step by step in the Wren programming language
You may also check:How to resolve the algorithm Copy stdin to stdout step by step in the Prolog programming language
You may also check:How to resolve the algorithm Exponentiation order step by step in the Quackery programming language