Skip to content

Instantly share code, notes, and snippets.

/Langton's ant Secret

Created December 8, 2015 17:21
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/4706ea1ef83b761070ed to your computer and use it in GitHub Desktop.
Save anonymous/4706ea1ef83b761070ed to your computer and use it in GitHub Desktop.
# 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