Skip to content

Instantly share code, notes, and snippets.

@para7
Last active September 12, 2017 16:02
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 para7/3dbac743464d8d7366c2985c763bae00 to your computer and use it in GitHub Desktop.
Save para7/3dbac743464d8d7366c2985c763bae00 to your computer and use it in GitHub Desktop.
#include <Siv3D.hpp>
#include <HamFramework.hpp>
#include <queue>
constexpr int PanelSize = 6;
constexpr int Wall = std::numeric_limits<int>::max() / 2;
constexpr int Shortest = Wall / 2;
constexpr Point FieldSize(100, 100);
Grid<int> map;
Array<Point> breakwall;
constexpr std::array<Point, 4u> Surround = { Point::Up(), Point::Left(), Point::Down(), Point::Right() };
void Main()
{
map.resize(FieldSize);
int number = 1;
map.fill(Wall);
Point rb;
for (int i = 0; i < map.width(); ++i)
{
for (int k = 0; k < map.height(); ++k)
{
map[k][i] = Wall;
if ((1 <= i&&i < map.width() - 1) && (1 <= k && k < map.height() - 1))
{
if ((i % 2) && (k % 2))
{
map[k][i] = number++;
rb = { i, k };
}
else if ((i % 2) ^ (k % 2) && (i < map.width() - 2) && (k < map.height() - 2))
{
if (i % 2)
{
map[k][i] = -1;
}
else
{
map[k][i] = -2;
}
breakwall.emplace_back(Point(i, k));
}
}
}
}
breakwall.shuffle();
for (auto& pos : breakwall)
{
int info1, info2;
if (map[pos] == -1)
{
info1 = map[pos.movedBy(0, 1)];
info2 = map[pos.movedBy(0, -1)];
}
else if (map[pos] == -2)//よこ
{
info1 = map[pos.movedBy(1, 0)];
info2 = map[pos.movedBy(-1, 0)];
}
if (info1 != info2 || RandomBool(0.015))
{
std::queue<Point> qu;
map[pos] = info1;
qu.push(pos);
while (qu.size())
{
const auto basepos = qu.front();
for (const auto& surrond : Surround)
{
const auto pos = basepos.movedBy(surrond);
auto& hoge = map[pos];
if (hoge != info1 &&
hoge != Wall &&
0 < hoge)
{
hoge = info1;
qu.push(pos);
}
}
qu.pop();
}
}
else
{
map[pos] = Wall;
}
}
{
Grid<Point> parent(map.size());
parent.fill(Point::Zero());
std::queue<Point> search;
search.push({ 1,1 });
map[search.front()] = 0;
while (true)
{
if (search.front() == rb)
{
break;
}
for (const auto& surround : Surround)
{
const auto pos = search.front().movedBy(surround);
if (map[pos] != 0 && map[pos] != Wall && parent[pos] == Point::Zero())
{
parent[pos] = search.front();
map[pos] = 0;
search.push(pos);
}
}
search.pop();
}
Point marking = rb;
while (marking != Point::Zero())
{
map[marking] = Shortest;
marking = parent[marking];
}
}
Camera2D camera;
Font font(20);
while (System::Update())
{
camera.update();
{
auto hoge = camera.createTransformer();
auto piyo = ScalableWindow::CreateTransformer();
for (int i = 0; i < map.width(); ++i)
{
for (int k = 0; k < map.height(); ++k)
{
Color color;
switch (map[k][i])
{
case Shortest:
color = (KeyZ.pressed() ? Palette::Red : Palette::White);
break;
case Wall:
color = Palette::Darkgray;
break;
default:
color = Palette::White;
break;
}
Rect((i)* PanelSize, (k)* PanelSize, PanelSize).draw(color);
}
}
}
camera.draw();
ScalableWindow::DrawBlackBars();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment