2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ダンジョンの素?

Posted at

初期状態
20140923-120823-235.png

出力例
20140923-121016-271.png

ん~、いまいち。
なんか遅いし。

Siv3DApp.cpp
#include <Siv3D.hpp>
#include <map>

void Main()
{

  const unsigned int WIDTH  = 32 ;
  const unsigned int HEIGHT = 24 ;

  std::vector<std::vector<int>> field(WIDTH, std::vector<int>(HEIGHT, 0)) ;
  std::map<int, Rect> clusters ;
  for (int y = 0, i = 0; y < HEIGHT; ++y) for (int x = 0; x < WIDTH; ++x) {
    clusters[i] = Rect{x, y, 1, 1} ;
    field[x][y] = i++ ;
  }

  for (std::vector<Point> p; clusters.size() > 1; p.clear()) {
    for (auto d : clusters) {
      if ((d.second.x + d.second.w) < WIDTH) {
        auto t = field[d.second.x + d.second.w][d.second.y] ;
        if (d.second.y == clusters[t].y && d.second.h == clusters[t].h) p.emplace_back(Point{d.first, t}) ;
      }
      if ((d.second.y + d.second.h) < HEIGHT) {
        auto t = field[d.second.x][d.second.y + d.second.h] ;
        if (d.second.x == clusters[t].x && d.second.w == clusters[t].w) p.emplace_back(Point{d.first, t}) ;
      }
    }
    if (p.empty()) break ;

    Point t = p[Random<int>(0, p.size()-1)] ;
    if ((Max(clusters[t.x].w, clusters[t.x].h) / Min(clusters[t.x].w, clusters[t.x].h)) >= 3) t = p[Random<int>(0, p.size()-1)] ;
    if (clusters[t.x].x + clusters[t.x].w < WIDTH && t.y == field[clusters[t.x].x + clusters[t.x].w][clusters[t.x].y]) clusters[t.x].w += clusters[t.y].w ;
    else {
      clusters[t.x].h += clusters[t.y].h ;
    }
    for (unsigned int y = 0; y < HEIGHT; ++y) for (unsigned int x = 0; x < WIDTH; ++x) if (field[x][y] == t.y) field[x][y] = t.x ;
    clusters.erase(clusters.find(t.y)) ;
  }

  int i = 0 ;
  for (auto c : clusters) {
    for (unsigned int y = 0; y < HEIGHT; ++y) for (unsigned int x = 0; x < WIDTH; ++x) if (field[x][y] == c.first) field[x][y] = i ;
    ++i ;
  }

  const int CSIZE = 20 ;
  while (System::Update()) {
    for (int y = 0; y < HEIGHT; ++y) for (int x = 0; x < WIDTH; ++x) {
      if (x == (WIDTH - 1) || field[x][y] != field[x+1][y]) Line(CSIZE * (x + 1), CSIZE * y, CSIZE * (x + 1), CSIZE * (y + 1)).draw() ;
      if (y == (HEIGHT - 1) || field[x][y] != field[x][y+1]) Line(CSIZE * x, CSIZE * (y + 1), CSIZE * (x + 1), CSIZE * (y + 1)).draw() ;
    }
  }
}
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?