How to resolve the algorithm Conway's Game of Life step by step in the Jsish programming language
How to resolve the algorithm Conway's Game of Life step by step in the Jsish 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 Jsish programming language
Source code in the jsish programming language
/* Conway's game of life, in Jsish */
function GameOfLife () {
this.title = "Conway's Game of Life";
this.cls = "\u001B[H\u001B[2J";
this.init = function (turns, width, height) {
this.board = new Array(height);
for (var x = 0; x < height; x++) {
this.board[x] = new Array(width);
for (var y = 0; y < width; y++) {
this.board[x][y] = Math.round(Math.random());
}
}
this.turns = turns;
};
this.nextGen = function() {
this.boardNext = new Array(this.board.length);
for (var i = 0; i < this.board.length; i++) {
this.boardNext[i] = new Array(this.board[i].length);
}
for (var x = 0; x < this.board.length; x++) {
for (var y = 0; y < this.board[x].length; y++) {
var n = 0;
for (var dx = -1; dx <= 1; dx++) {
for (var dy = -1; dy <= 1; dy++) {
if ( dx == 0 && dy == 0){}
else if (typeof this.board[x+dx] !== 'undefined'
&& typeof this.board[x+dx][y+dy] !== 'undefined'
&& this.board[x+dx][y+dy]) {
n++;
}
}
}
var c = this.board[x][y];
switch (n) {
case 0:
case 1:
c = 0;
break;
case 2:
break;
case 3:
c = 1;
break;
default:
c = 0;
}
this.boardNext[x][y] = c;
}
}
this.board = this.boardNext.slice(0);
};
this.print = function() {
for (var x = 0; x < this.board.length; x++) {
var l = "";
for (var y = 0; y < this.board[x].length; y++) {
if (this.board[x][y])
l += "X";
else
l += " ";
}
puts(l);
}
};
this.start = function() {
for (var t = 0; t < this.turns; t++) {
sleep(500);
printf(this.cls);
puts(this.title + "\n---\nTurn "+(t+1));
this.print();
this.nextGen();
}
};
}
var game = new GameOfLife();
if (Interp.conf('unitTest')) {
game.init(3,3,3);
game.title="---\n3x3 Blinker over three turns.";
game.board = [
[0,0,0],
[1,1,1],
[0,0,0]];
game.cls="";
game.start();
} else {
game.init(3,3,3);
game.title="---\n3x3 Blinker over three turns.";
game.board = [
[0,0,0],
[1,1,1],
[0,0,0]];
game.start();
game.init(5,10,6);
game.title="---\n10x6 Glider over five turns.";
game.board = [
[0,0,0,0,0,0,0,0,0,0],
[0,0,1,0,0,0,0,0,0,0],
[0,0,0,1,0,0,0,0,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]];
game.start();
var steps = (console.args[0]) ? parseInt(console.args[0]) || 1 : 50;
game.init(steps, 32,16);
game.title="---\nRandom 32x16, " + steps + " step" + ((steps === 1) ? "" : "s");
game.start();
}
/*
=!EXPECTSTART!=
---
3x3 Blinker over three turns.
---
Turn 1
XXX
---
3x3 Blinker over three turns.
---
Turn 2
X
X
X
---
3x3 Blinker over three turns.
---
Turn 3
XXX
=!EXPECTEND!=
*/
You may also check:How to resolve the algorithm Babbage problem step by step in the Caché ObjectScript programming language
You may also check:How to resolve the algorithm Truncatable primes step by step in the Perl programming language
You may also check:How to resolve the algorithm XML/DOM serialization step by step in the PHP programming language
You may also check:How to resolve the algorithm Zebra puzzle step by step in the C# programming language
You may also check:How to resolve the algorithm Abelian sandpile model step by step in the ARM Assembly programming language