2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Kinx アルゴリズム - ライフゲーム

Posted at

Kinx アルゴリズム - ライフゲーム

はじめに

「見た目は JavaScript、頭脳(中身)は Ruby、(安定感は AC/DC)」 でお届けしているスクリプト言語 Kinx。「プログラム=アルゴリズム+データ構造」。アルゴリズムの実装例をご紹介。

元ネタは「C言語による(30年経っても)最新アルゴリズム事典」。今回はライフゲームです。

最新アルゴリズム事典にはこういうのも結構載ってる。パズル的な。

Kinx では最初のころにこのサンプルコードを書いて、二重配列が正しく動作することと、二重配列の結果の左辺値が正しく動作するか(インクリメントする部分)の確認に使った。良いテストになった。

ライフゲーム

Wikipedia より

ライフゲーム (Conway's Game of Life[1]) は1970年にイギリスの数学者ジョン・ホートン・コンウェイ (John Horton Conway) が考案した生命の誕生、進化、淘汰などのプロセスを簡易的なモデルで再現したシミュレーションゲームである。単純なルールでその模様の変化を楽しめるため、パズルの要素を持っている。
生物集団においては、過疎でも過密でも個体の生存に適さないという個体群生態学的な側面を背景に持つ。セル・オートマトンのもっともよく知られた例でもある。

ソースコード

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
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
.....................................***......................................
.....................................*........................................
.....................................**.......................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................
..............................................................................

...(省略)

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

おわりに

こちらも C 言語版ほぼそのままです。Good Job!

アルゴリズム事典の中にはこういうパズルゲーム的な要素のものも結構あります。割と面白いので、折に触れて紹介していきたいと思います。

ではまた、次回。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?