How to resolve the algorithm Conway's Game of Life step by step in the Chapel programming language
How to resolve the algorithm Conway's Game of Life step by step in the Chapel 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 Chapel programming language
Source code in the chapel programming language
config const gridHeight: int = 3;
config const gridWidth: int = 3;
enum State { dead = 0, alive = 1 };
class ConwaysGameofLife {
var gridDomain: domain(2);
var computeDomain: subdomain( gridDomain );
var grid: [gridDomain] int;
proc ConwaysGameofLife( height: int, width: int ) {
this.gridDomain = {0..#height+2, 0..#width+2};
this.computeDomain = this.gridDomain.expand( -1 );
}
proc step(){
var tempGrid: [this.computeDomain] State;
forall (i,j) in this.computeDomain {
var isAlive = this.grid[i,j] == State.alive;
var numAlive = (+ reduce this.grid[ i-1..i+1, j-1..j+1 ]) - if isAlive then 1 else 0;
tempGrid[i,j] = if ( (2 == numAlive && isAlive) || numAlive == 3 ) then State.alive else State.dead ;
}
this.grid[this.computeDomain] = tempGrid;
}
proc this( i: int, j: int ) ref : State {
return this.grid[i,j];
}
proc prettyPrint(): string {
var str: string;
for i in this.gridDomain.dim(1) {
if i == 0 || i == gridDomain.dim(1).last {
for j in this.gridDomain.dim(2) {
str += "-";
}
} else {
for j in this.gridDomain.dim(2) {
if j == 0 || j == this.gridDomain.dim(2).last {
str += "|";
} else {
str += if this.grid[i,j] == State.alive then "#" else " ";
}
}
}
str += "\n";
}
return str;
}
}
proc main{
var game = new ConwaysGameofLife( gridHeight, gridWidth );
game[gridHeight/2 + 1, gridWidth/2 ] = State.alive;
game[gridHeight/2 + 1, gridWidth/2 + 1 ] = State.alive;
game[gridHeight/2 + 1, gridWidth/2 + 2 ] = State.alive;
for i in 1..3 {
writeln( game.prettyPrint() );
game.step();
}
}
You may also check:How to resolve the algorithm Sorting algorithms/Heapsort step by step in the Python programming language
You may also check:How to resolve the algorithm Quaternion type step by step in the Eero programming language
You may also check:How to resolve the algorithm Möbius function step by step in the AWK programming language
You may also check:How to resolve the algorithm Dijkstra's algorithm step by step in the Ring programming language
You may also check:How to resolve the algorithm Pan base non-primes step by step in the J programming language