How to resolve the algorithm Conway's Game of Life step by step in the Pointless programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Conway's Game of Life step by step in the Pointless 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 Pointless programming language

Source code in the pointless programming language

-----------------------------------------------------------
-- Print 100 simulated states of conway's game of life
-- for a glider starting pattern on a wrapping grid
-- Print generation number along with cells

output =
  initCells
  |> iterate(updateCells)
  |> take(130)
  |> enumerate
  |> map(showPair)
  |> printFrames

initCells =
 [[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 1 1 0 0 0 0 1 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 1 0 0 0 1 1 1 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]

width  = length(initCells[0])
height = length(initCells)

positions = 
  for y in range(0, height - 1)
  for x in range(0, width - 1)
  yield (x, y)

-----------------------------------------------------------
-- Update each cell in the grid according to its position,
-- and convert the resulting list back to a 2D array

updateCells(cells) =
  positions
  |> map(tick(cells))
  |> toNDArray((height, width))

-----------------------------------------------------------
-- Get the new value for a cell at a give position
-- based on the current cell values in the grid

tick(cells, pos) = toInt(survive or birth) where {
  survive = cells[y][x] == 1 and count in {2, 3}
  birth   = cells[y][x] == 0 and count == 3
  count   = getCount(x, y, cells)
  (x, y)  = pos
}

-----------------------------------------------------------
-- Get the number of live neighbors of a given position

getCount(x, y, cells) = sum(
  for dx in [-1, 0, 1] 
  for dy in [-1, 0, 1] 
  when (dx != 0 or dy != 0)
  yield getNeighbor(x + dx, y + dy, cells) 
)

getNeighbor(x, y, cells) = cells[y % height][x % width]   

-----------------------------------------------------------
-- Print the board and generation number given pairs
-- of (gen, cells) from the enumerate function

showPair(pair) =
  format("{}\ngeneration: {}", [showCells(cells), gen])
  where (gen, cells) = pair

showCells(cells) =
  toList(cells)
  |> map(showRow)
  |> join("\n")

showRow(row) =
  format("|{}|", [map(showCell, toList(row)) |> join("")])

showCell(cell) =
  if cell == 1 then "*" else " "


  

You may also check:How to resolve the algorithm Compound data type step by step in the OCaml programming language
You may also check:How to resolve the algorithm Walk a directory/Non-recursively step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Towers of Hanoi step by step in the Gambas programming language
You may also check:How to resolve the algorithm Sorting algorithms/Strand sort step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Earliest difference between prime gaps step by step in the Sidef programming language