C++/OpenSiv3D で 10 行ライフゲーム


OpenSiv3Dでライフゲームを 10 LOC で書く方法

Main
# include <Siv3D.hpp> // OpenSiv3D v0.2.7

void Main()
{
    Image i(Window::Size(), Arg::generator = []() { return Color(0, 0, Random(0, 1)); });
    DynamicTexture t(i);
    Array<Point> n = { { { -1,-1 },{ 0, -1 },{ 1, -1 },{ -1, 0 },{ 1,0 },{ -1, 1 },{ 0, 1 },{ 1,1 } } };

    while (System::Update())
    {
        for (auto p : step(Point(1, 1), i.size() - Point(2, 2)))
            i[p].r = (6152 >> i[p].b * 9 + n.count_if([&](auto v) { return i[p + v].b; })) & 1;

        t.fill(i.forEach([](auto& c) { c = c.r ? Color(0, 255, 1) : Color(0); }));
        t.draw();
    }
}