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

Source code in the sidef programming language

var w = Num(`tput cols`)
var h = Num(`tput lines`)
var r = "\033[H"

var dirs = [[-1,-1], [-1, 0], [-1, 1], [ 0,-1],
            [ 0, 1], [ 1,-1], [ 1, 0], [ 1, 1]]

var universe = h.of { w.of {1.rand < 0.1} }

func iterate {
    var new = h.of { w.of(false) }
    static rx = (^h ~X ^w)
    for i,j in rx {
        var neighbor = 0
        for y,x in (dirs.map {|dir| dir »+« [i, j] }) {
            universe[y % h][x % w] && ++neighbor
            neighbor > 3 && break
        }
        new[i][j] = (universe[i][j]
                        ? (neighbor==2 || neighbor==3)
                        : (neighbor==3))
    }
    universe = new
}

STDOUT.autoflush(true)

loop {
    print r
    say universe.map{|row| row.map{|cell| cell ? '#' : ' '}.join }.join("\n")
    iterate()
}

  

You may also check:How to resolve the algorithm Sorting algorithms/Quicksort step by step in the Dart programming language
You may also check:How to resolve the algorithm Executable library step by step in the Phix programming language
You may also check:How to resolve the algorithm Sorting algorithms/Gnome sort step by step in the EDSAC order code programming language
You may also check:How to resolve the algorithm Operator precedence step by step in the Factor programming language
You may also check:How to resolve the algorithm Pentomino tiling step by step in the Perl programming language