Create a gist now

Instantly share code, notes, and snippets.

anonymous /Langton's ant Secret
Created Dec 8, 2015

What would you like to do?
# include <Siv3D.hpp>
struct Langton{
int dir;
/*
0 上
1 左
2 下
3 右
*/
Point p;//アリの位置
Color color;
};
void Main()
{
const Font font(10);
Image image(Window::Size(), Palette::Black);
DynamicTexture texture;
Array<struct Langton> ants;
while (System::Update())
{
//ウィンドウ内をクリックしてアリを生成
if (Input::MouseL.clicked && 0 < Mouse::Pos().x && Mouse::Pos().x < Window::Width() && 0 < Mouse::Pos().y && Mouse::Pos().y < Window::Height())
{
ants.push_back({ Random(0, 3), Mouse::Pos(), HSV(System::FrameCount() % 360, 1.0, 1.0) });
}
for (auto &ant : ants){
if (image[ant.p.y][ant.p.x] != Palette::Black){//右に方向転換して着色
ant.dir = (ant.dir + 1) % 4;
Point(ant.p).overwrite(image, Palette::Black);
}
else {//左に方向転換して着色
ant.dir = (ant.dir + 3) % 4;
Point(ant.p).overwrite(image, ant.color);
}
switch (ant.dir){//アリの移動
case 0:
--ant.p.y;
break;
case 1:
--ant.p.x;
break;
case 2:
++ant.p.y;
break;
default:
++ant.p.x;
}
//ウィンドウ限界処理
ant.p.x = (ant.p.x + Window::Width()) % Window::Width();
ant.p.y = (ant.p.y + Window::Height()) % Window::Height();
}
texture.fill(image);
texture.draw();
font(System::FrameCount()).draw();
for (auto &ant : ants)//アリを白色で描画
Point(ant.p).draw(Palette::White);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment