40行でライフゲーム
ニコニコ生放送「寝ながらライフゲームつくる」にて
Main
# include <Siv3D.hpp>
void Main()
{
const Point size = Window::Size();
Image image(size.x, size.y);
DynamicTexture tex;
std::array<std::vector<bool>, 2> worlds;
const int num = size.x*size.y;
worlds[0].resize(num);
worlds[1].resize(num);
std::array<Point, 8> neighbors = { { { -1, -1 }, { 0, -1 }, { 1, -1 }, { -1, 0 }, { 1, 0 }, { -1, 1 }, { 0, 1 }, { 1, 1 } } };
std::array<bool, 9> boneRule = { false, false, false, true, false, false, false, false, false };
std::array<bool, 9> surviveRule = { false, false, true, true, false, false, false, false, false };
std::array<std::array<bool, 9>, 2> rule = { boneRule, surviveRule };
while (System::Update())
{
if (Input::MouseL.clicked)
for (int y = 1; y < size.y - 1; ++y)
for (int x = 1; x < size.x - 1; ++x)
worlds[0][y*size.x + x] = RandomBool(0.5);
//update
for (int y = 1; y < size.y - 1; ++y)
for (int x = 1; x < size.x - 1; ++x)
worlds[1][y*size.x + x] = rule[worlds[0][y*size.x + x]][std::count_if(neighbors.begin(), neighbors.end(), [&](const Point& v){return worlds[0][(v.y + y)*size.x + (v.x + x)]; })];
worlds[0].swap(worlds[1]);
//draw
for (int y = 1; y < size.y - 1; ++y)
for (int x = 1; x < size.x - 1; ++x)
image[y][x] = ColorF(0.0, worlds[0][y*size.x + x], 0.0);
tex.tryFill(image);
tex.draw();
}
}
Author And Source
この問題について(40行でライフゲーム), 我々は、より多くの情報をここで見つけました https://qiita.com/hamukun8686/items/f82143af77fa23673c8b著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .