How to resolve the algorithm Conway's Game of Life step by step in the Futhark programming language
How to resolve the algorithm Conway's Game of Life step by step in the Futhark 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 Futhark programming language
Source code in the futhark programming language
fun bint(b: bool): int = if b then 1 else 0
fun intb(x: int): bool = if x == 0 then False else True
fun to_bool_board(board: [][]int): [][]bool =
map (fn (r: []int): []bool => map intb r) board
fun to_int_board(board: [][]bool): [][]int =
map (fn (r: []bool): []int => map bint r) board
fun cell_neighbors(i: int, j: int, board: [n][m]bool): int =
unsafe
let above = (i - 1) % n
let below = (i + 1) % n
let right = (j + 1) % m
let left = (j - 1) % m in
bint board[above,left] + bint board[above,j] + bint board[above,right] +
bint board[i,left] + bint board[i,right] +
bint board[below,left] + bint board[below,j] + bint board[below,right]
fun all_neighbours(board: [n][m]bool): [n][m]int =
map (fn (i: int): []int =>
map (fn (j: int): int => cell_neighbors(i,j,board)) (iota m))
(iota n)
fun iteration(board: [n][m]bool): [n][m]bool =
let lives = all_neighbours(board) in
zipWith (fn (lives_r: []int) (board_r: []bool): []bool =>
zipWith (fn (neighbors: int) (alive: bool): bool =>
if neighbors < 2
then False
else if neighbors == 3 then True
else if alive && neighbors < 4 then True
else False)
lives_r board_r)
lives board
fun main(int_board: [][]int, iterations: int): [][]int =
-- We accept the board as integers for convenience, and then we
-- convert to booleans here.
let board = to_bool_board int_board in
loop (board) = for i < iterations do
iteration board in
to_int_board board
You may also check:How to resolve the algorithm Terminal control/Coloured text step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Cholesky decomposition step by step in the Julia programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the TI-83 BASIC programming language
You may also check:How to resolve the algorithm Esthetic numbers step by step in the Rust programming language
You may also check:How to resolve the algorithm Largest number divisible by its digits step by step in the Arturo programming language