Maze generation: Difference between revisions

Content added Content deleted
(Updated D entry)
(Simpler D entry)
Line 1,553: Line 1,553:
import std.stdio, std.algorithm, std.range, std.random;
import std.stdio, std.algorithm, std.range, std.random;


enum int w = 14, h = 10;
enum uint w = 14, h = 10;
auto vis = new bool[][](h, w),
auto vis = new bool[][](h, w),
hor = iota(h + 1).map!(_ => ["+---"].replicate(w)).array,
hor = iota(h + 1).map!(_ => ["+---"].replicate(w)).array,
ver = h.iota.map!(_ => ["| "].replicate(w) ~ "|").array;
ver = h.iota.map!(_ => ["| "].replicate(w) ~ "|").array;


void walk(in int x, in int y) /*nothrow*/ {
void walk(in uint x, in uint y) /*nothrow*/ {
vis[y][x] = true;
vis[y][x] = true;
foreach (p; [[x-1,y], [x,y+1], [x+1,y], [x,y-1]].randomCover) {
static struct P { immutable uint x, y; } // Will wrap-around.
auto d = [P(x-1, y), P(x, y+1), P(x+1, y), P(x, y-1)];
if (p[0] >= w || p[1] >= h || vis[p[1]][p[0]]) continue;
foreach (p; d.randomCover) {
if (p[0] == x) hor[max(y, p[1])][x] = "+ ";
if (p.x >= w || p.y >= h || vis[p.y][p.x]) continue;
if (p[1] == y) ver[y][max(x, p[0])] = " ";
if (p.x == x) hor[max(y, p.y)][x] = "+ ";
walk(p[0], p[1]);
if (p.y == y) ver[y][max(x, p.x)] = " ";
walk(p.tupleof);
}
}
}
}