** "Looks like JavaScript, brain (contents) is Ruby, (stability is AC / DC)" ** Scripting language Kinx ). "Program = algorithm + data structure". Introducing an implementation example of the algorithm.
The original story is "The latest algorithm encyclopedia in C language (even after 30 years)". This time is a life game.
There are quite a few such things in the latest algorithm encyclopedia. It's like a puzzle.
Kinx wrote this sample code early on to see if the double array works correctly and if the rvalues in the result of the double array work correctly (incrementing part). It was a good test.
From Wikipedia
Conway's Game of Life [1] reproduces the process of birth, evolution, and selection of life devised by British mathematician John Horton Conway in 1970 with a simple model. It is a simulation game. It has a puzzle element because you can enjoy the change of the pattern with simple rules. In a biological population, it has a population ecological aspect that it is not suitable for individual survival whether it is depopulated or overcrowded. It is also the most well-known example of cellular automata.
const N = 22; // Vertical
const M = 78; // Horizontal
var a = [], b = [];
a[N/2][M/2] = a[N/2-1][M/2] = a[N/2+1][M/2] = a[N/2][M/2-1] = a[N/2-1][M/2+1] = 1;
function life() {
for (var g = 1; g <= 1000; g++) {
System.print("Generation %4d\n" % g);
for (var i = 1; i <= N; i++) {
for (var j = 1; j <= M; j++)
if (a[i][j]) {
System.print("*");
b[i-1][j-1]++; b[i-1][j]++; b[i-1][j+1]++;
b[i ][j-1]++; b[i ][j+1]++;
b[i+1][j-1]++; b[i+1][j]++; b[i+1][j+1]++;
} else System.print(".");
System.print("\n");
}
for (var i = 0; i <= N + 1; i++)
for (var j = 0; j <= M + 1; j++) {
if (b[i][j] != 2) a[i][j] = (b[i][j] == 3);
b[i][j] = 0;
}
}
}
life();
Generation 1
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
......................................**......................................
.....................................**......................................*
......................................*.......................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
Generation 2
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
.....................................***......................................
.....................................*........................................
.....................................**.......................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
...(abridgement)
Generation 1000
..............................................................................
.......**.....................................................................
.......**.....................................**..............................
.............................................*..*.............................
............................**................*.*.............................
............................**.................*........**....................
....................................*...................**....................
...........**.......................*.........................................
...........**.......................*.........................................
..............................................................................
...............................**.............................................
..............................*..*............................................
...............................*.*............................................
................................*.............................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
This is also almost the same as the C language version. Good Job!
There are quite a few puzzle game-like elements in the algorithm encyclopedia. It's rather interesting, so I'd like to introduce it from time to time.
See you next time.