challenge ライフゲーム

セルオートマトンに関するお題です. 
2次元タイプの'ライフゲーム'を実装して下さい. 
初期値としては10行10列程度の格子上の平面に0.3程度の人口(?)密度を考え, 
末端はループするようにして下さい. (例: 座標[-1, -1] = [10, 10])

それだけだと簡単すぎると思われる方は, 
過密状態で間引きが発生するような機能を組み込んで下さい. 
間引きは, 少なくともその後の1時間ステップにおける死亡率が, 
それをしなかった場合よりも小さくなれば結構です. 
(死亡率の最小化は複雑性が高すぎる感がありますし. )
サンプル:
t = 0
[ ][*][ ][ ][ ][ ][*][*][*][ ]
[ ][ ][ ][ ][*][ ][ ][*][*][ ]
[ ][ ][ ][*][ ][ ][*][ ][*][ ]
[*][ ][*][*][ ][ ][*][ ][ ][ ]
[ ][*][ ][ ][ ][ ][ ][ ][*][ ]
[*][ ][ ][ ][*][ ][*][*][ ][*]
[ ][*][ ][ ][ ][ ][*][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][*]
[*][ ][ ][ ][ ][ ][*][ ][ ][*]
[ ][ ][ ][ ][*][*][ ][ ][*][ ]
t = 1
[ ][ ][ ][ ][*][ ][ ][ ][ ][*]
[ ][ ][ ][ ][ ][*][ ][ ][ ][*]
[ ][ ][*][ ][*][*][*][ ][*][*]
[ ][*][ ][*][ ][ ][ ][ ][ ][*]
[ ][ ][*][*][ ][*][*][ ][*][ ]
[ ][*][ ][ ][ ][*][*][ ][*][*]
[ ][ ][ ][ ][ ][*][*][*][*][*]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][*]
[*][ ][ ][ ][ ][*][ ][ ][*][ ]
[*][ ][ ][ ][ ][ ][ ][ ][ ][ ]

Posted feedbacks - JavaScript

JavaScriptで書きました。

間引きは特に考慮していません。パターンはグライダーで、 0.5秒ごとに世代交代します。

Firefox 2.0.0.8, Internet Explorer 6, Opera 9.23で動作を確認。

  e.g.
    .*........
    ..*.......
    ***.......
    ..........
    ..........
    ..........
    ..........
    ..........
    ..........
    ..........
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:xlink="http://www.w3.org/1999/xlink" xml:lang="ja" lang="ja">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <title>Conway's game of life</title>
        <script type="text/javascript">
            $ = function (i) { return document.getElementById(i); }

            Function.prototype._setTimeout = 
            function (t, o, v) {
                var _ = this;
                return setTimeout(function () { _.apply(o, v); }, t);
            };

            var CellularAutomaton = 
            function (p, w, h) {
                this.p  = p;   // パターン
                this.w  = w;   // フィールドの横幅
                this.h  = h;   // フィールドの高さ
                this.c  = [];  // セル
                this._c = [];
                this.TI = 500; // 時間間隔
            };
            CellularAutomaton.prototype.generate = 
            function () {
                var i, j, n = 0, s = '', x, y;
                // フィールドを初期化
                for (y = 0; y < this.h; y++) {
                    for (x = 0; x < this.w; x++, n++) s += '<tt id="cell_' + n + '"></tt>';
                    s += '<br />';
                }
                document.body.innerHTML = s;
                this.clear();
                // パターンを描画
                for (i = 0; i < this.p.length; i++) {
                    for (j = 0; j < this.p[i].length; j++) {
                        if (this.p[i].charAt(j) == '.') continue;
                        n = this.h * i + j;
                        this.setCell(n, this.c[n] = 1);
                    }
                }
            };
            CellularAutomaton.prototype.clear = 
            function () {
                var n;
                for (n = this.w * this.h; n--; ) this.setCell(n, this.c[n] = 0);
            };
            CellularAutomaton.prototype.start = 
            function () {
                var n;
                // 次世代へ移行
                this.shift();
                for (n = 0; n < this.h * this.w; n++) this.c[n] = this._c[n];
                // 再帰
                this.start._setTimeout(this.TI, this, []);
            };
            CellularAutomaton.prototype.shift = 
            function () {
                var n = 0, x, y;

                for (y = 0; y < this.h; y++) {
                    for (x = 0; x < this.w; x++, n++) {
                        this._c[n] = this.judge(x, y, n);
                        if (this.c[n] != this._c[n]) this.setCell(n, this._c[n]);
                    }
                }
            };
            CellularAutomaton.prototype.judge = 
            function (x, y, n) {
                var i, j, l = 0, _x, _y;

                if ((x > 0 && x < this.w - 1) && (y > 0 && y < this.h - 1)) {
                    l = this.c[n - this.w - 1] + this.c[n - this.w] + this.c[n - this.w + 1]
                      + this.c[n          - 1]                      + this.c[n          + 1]
                      + this.c[n + this.w - 1] + this.c[n + this.w] + this.c[n + this.w + 1]
                      ;
                } else { // トーラス
                    for (i = -1; i < 2; i++) {
                        for (j = -1; j < 2; j++) {
                            if (i == 0 && j == 0) continue;

                            if (x + j == -1) _x = this.w - 1;
                            if (x + j == this.w) _x = 0;
                            if (x + j != -1 && x + j != this.w) _x = x + j;
                            if (y + i == -1) _y = this.h - 1;
                            if (y + i == this.h) _y = 0;
                            if (y + i != -1 && y + i != this.h) _y = y + i;

                            l += this.c[this.w * _y + _x];
                        }
                    }
                }

                if (this.c[n] == 0 && l == 3)             return 1; // 誕生
                if (this.c[n] == 1 && (l == 2 || l == 3)) return 1; // 維持
                return 0;                                           // 死亡
            };
            CellularAutomaton.prototype.setCell = 
            function (n, p) {
                $('cell_' + n).innerHTML = (p == 0) ? '.' : '*';
            };

            window.onload = 
            function () {
                var c = new CellularAutomaton([ '.*.' , '..*' , '***' ], 10, 10); c.generate(); c.start();
            };
        </script>
    </head>
    <body>
    </body>
</html>

Index

Feed

Other

Link

Pathtraq

loading...