Skip to content

Instantly share code, notes, and snippets.

/CellAutomaton Secret

Created November 25, 2015 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/cd0c161daa0909b860fb to your computer and use it in GitHub Desktop.
Save anonymous/cd0c161daa0909b860fb to your computer and use it in GitHub Desktop.
# include <Siv3D.hpp>
void Main()
{
unsigned char rule = 30;//ルール
int width = 100;//横幅
int height = 100;//表示する世代の数
Grid<int> cells(width,height);//1か0を格納する
Graphics::SetBackground(Palette::Gray);
while (System::Update())
{
//オートマトンの更新
//下側から更新することによって下の段に影響を波及させずに1フレームで1世代ずつ更新させる
for (int y = height - 1; y >= 1; y--)
{
for (int x = 0; x < width; x++)
{
int state = cells[y - 1][(x + width - 1) % width] * 4 +
cells[y - 1][x] * 2 +
cells[y - 1][(x + 1) % width];
cells[y][x] = (rule >> state) & 1;
}
}
int dotSize = Window::Width() / width;
for (auto p : step({width, height }))
{
Rect rect(p * dotSize, dotSize , dotSize );
if (p.y == 0)
{
rect.w--;
rect.h--;
}
if (rect.leftClicked & p.y == 0)
{
cells[p.y][p.x] = 1 - cells[p.y][p.x];
}
if (cells[p.y][p.x])rect.draw(Palette::Black);
else rect.draw(Palette::White);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment