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 |
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 |
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. |
|||
if (p[0] >= w || p[1] >= h || vis[p[1]][p[0]]) continue; |
|||
if (p[0] == x) hor[max(y, p[1])][x] = "+ "; |
|||
if (p |
if (p[1] == y) ver[y][max(x, p[0])] = " "; |
||
walk(p[0], p[1]); |
|||
if (p.y == y) ver[y][max(x, p.x)] = " "; |
|||
walk(p.tupleof); |
|||
} |
} |
||
} |
} |