"ui";

ui.layout(
    <vertical>
        <text id="text" textSize="10sp"/>
    </vertical>
);


/*

□□□  □□□
□■□  □□□   少于两个死亡
■□□  ■□□

□■□  □■□
□■■  □□■   多于三个死亡
■□■  ■□■

□■□  □■□
□□■  □■■   三生一
■□□  ■□□

□■□  □■□
□■■  □■■   保持不变
■□□  ■□□

*/




var Ary =[
"□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□",
"□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□",
"□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□",
"□□□□□□□□■■□□□□□□□□□□□□□□□□□□□□",
"□□□□□□□□■■□□□□□□□□□□□□□□□□□□□□",
"□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□",
"□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□",
"□□□□□□□■■■□□□□□□□□□□□□□□□□□□□□",
"□□□□□□□■■■□□□□□□□□□□□□□□□□□□□□",
"□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□",
"□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□",
"□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□",
"□□□□□■■□□□■■□□□□□□□□□□□□□□□□□□",
"□□□□□□■■■■■□□□□□□□□□□□□□□□□□□□",
"□□□□□□□■■■□□□□□□□□□□□□□□□□□□□□",
"□□□□□□□□■□□□□□□□□□□□□□□□□□□□□□",
"□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□",
"□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□",
"□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□",
"□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□",
"□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□",
"□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□",
"□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□",
"□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□",
"□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□",
"□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□",
"□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□",
"□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□",
"□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□",
"□□□■■□□□■■□□□□□□□□□□□□□□□□□□□□",
"□□□□□■■■□□□□□□□□□□□□□□□□□□□□□□",
"□□□□■□□□■□□□□□□□□□□□□□□□□□□□□□",
"□□□□□■□■□□□□□□□□□□□□□□□□□□□□□□",
"□□□□□□■□□□□□□□□□□□□□□□□□□□□□□□",
"□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□",
"□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□",
"□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□",
"□□□□□□■■□□□□□□□□□□□□□□□□□□□□□□",
"□□□□□□■■□□□□□□□□□□□□□□□□□□□□□□",
"□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□",
];

log(Ary.length,Ary[0].length);
var w = 30,
    h = 40;
/*
for (var i = 0; i < h; i++) {
    var T = "";
    for (var ii = 0; ii < w; ii++) {
        T += (random(0, 1) ? "□" : "■");

    };
    Ary.push(T);
};
*/


ui.text.setText(Ary.join("\n"));

threads.start(function() {
    sleep(500);
    while (true) {
        var ary = Ary.join("\n").split("\n");
        for (var y = 0; y < h; y++) {
            var ar=ary[y].split("");
            for (var x = 0; x < w; x++) {
                var k = 0;
                for (var iy = -1; iy <= 1; iy++) {
                    for (var ix = -1; ix <= 1; ix++) {
                        if (!(iy == 0 && ix == 0)) {
                            var A = Ary[y + iy];
                            if (A) {
                                var B = A[x + ix];
                                if (B) {
                                    k += (String(B) == "■" ? 1 : 0);
                                };
                            };
                        };

                    };
                };
                if (k < 2 || k > 3) {
                    ar[x] = "□";

                } else if (k == 3) {
                    ar[x] = "■";
                };

            };
            ary[y]=ar.join("");
        };
        Ary = ary;
        ui.run(() => {
            ui.text.setText(Ary.join("\n"));
        });
        //sleep(100);
    };
});
