[Java] Kinx Algorithm-Life Game

2 minute read

Kinx Algorithm-Life Game

Introduction

The script language Kinxdeliveredby”“ItlookslikeJavaScript,thebrain(contents)isRuby,(stabilityisAC/DC)”). “Program = algorithm + data structure”. Introducing an example implementation of the algorithm.

The original story is “The latest algorithm encyclopedia in C language (even after 30 years)”. This time is a life game.

The latest algorithm encyclopedia also has quite a few of these. Like a puzzle.

In Kinx, I wrote this sample code at the beginning, and used it to confirm that the double array works correctly and whether the lvalue of the result of the double array works (increment). It was a good test.

Life game

  • https://ja.wikipedia.org/wiki/%E3%83%A9%E3%82%A4%E3%83%95%E3%82%B2%E3%83%BC%E3%83%A0

From Wikipedia

Life Game (Conway’s Game of Life[1]) is a simple model that reproduces the process of birth, evolution, selection, etc., devised by the English mathematician John Horton Conway in 1970. It is a simulation game. It has a puzzle element because you can enjoy the change of the pattern with a simple rule. In the context of populations, the population ecological aspect is that they are not suitable for individual survival, whether they are depopulated or overcrowded. It is also the best known example of a cellular automaton.

Source code

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();

result

Generation 1
.................................................. ............................
.................................................. ............................
.................................................. ............................
.................................................. ............................
.................................................. ............................
.................................................. ............................
.................................................. ............................
.................................................. ............................
.................................................. ............................
......................................**......... ............................
..................................... ...........................*
.............................. ............................
.................................................. ............................
.................................................. ............................
.................................................. ............................
.................................................. ............................
.................................................. ............................
.................................................. ............................
.................................................. ............................
.................................................. ............................
.................................................. ............................
.................................................. ............................
Generation 2
.................................................. ............................
.................................................. ............................
.................................................. ............................
.................................................. ............................
.................................................. ............................
.................................................. ............................
.................................................. ............................
.................................................. ............................
.................................................. ............................
.......................***........... ............................
.......................*............ ............................
..................................... ............................
.................................................. ............................
.................................................. ............................
.................................................. ............................
.................................................. .............................................................................. ............................
.................................................. ............................
.................................................. ............................
.................................................. ............................
.................................................. ............................
.................................................. ............................

...(abridgement)

Generation 1000
.................................................. ............................
........................................................................ ............................
.......**........................................**.. ............................
.............................................*..*. ............................
.....................**................*.*. ............................
.....................**.................*.. ......**...................
....................................*............. ......**...................
...........**...................*............. ............................
...........**...................*............. ............................
.................................................. ............................
...............................**................. ............................
........................................*..*................ ............................
...............................*.*................ ............................
................................*................. ............................
.................................................. ............................
.................................................. ............................
.................................................. ............................
.................................................. ............................
.................................................. ............................
.................................................. ............................
.................................................. ............................
.................................................. ............................

in conclusion

This is 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 relatively interesting, so I would like to introduce it occasionally.

See you next time.