Maze generation: Difference between revisions
Content added Content deleted
(→{{header|Perl 6}}: demagicalize) |
(Updated D entry) |
||
Line 1,039: | Line 1,039: | ||
=={{header|D}}== |
=={{header|D}}== |
||
<lang d>import std.stdio, std.algorithm, std.range, std.random |
<lang d>import std.stdio, std.algorithm, std.range, std.random; |
||
alias R = std.array.replicate; |
alias R = std.array.replicate; |
||
Line 1,045: | Line 1,045: | ||
enum int w = 14, h = 10; |
enum int w = 14, h = 10; |
||
auto vis = new bool[][](h, w), |
auto vis = new bool[][](h, w), |
||
hor = iota(h + 1).map!(_ => R(["+---"], w) |
hor = iota(h + 1).map!(_ => R(["+---"], w)).array, |
||
ver = iota(h).map!(_ => R(["| "], w) ~ "|" |
ver = iota(h).map!(_ => R(["| "], w) ~ "|").array; |
||
void walk(in int x, in int y) /*nothrow*/ { |
void walk(in int x, in int y) /*nothrow*/ { |
||
Line 1,052: | Line 1,052: | ||
static struct P { immutable uint x, y; } // Will wrap-around. |
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)]; |
auto d = [P(x-1, y), P(x, y+1), P(x+1, y), P(x, y-1)]; |
||
foreach (p; d.randomCover |
foreach (p; d.randomCover(unpredictableSeed.Random)) { |
||
if (p.x >= w || p.y >= h || vis[p.y][p.x]) continue; |
if (p.x >= w || p.y >= h || vis[p.y][p.x]) continue; |
||
if (p.x == x) hor[max(y, p.y)][x] = "+ "; |
if (p.x == x) hor[max(y, p.y)][x] = "+ "; |
||
Line 1,060: | Line 1,060: | ||
} |
} |
||
walk(uniform(0, w), uniform(0, h)); |
walk(uniform(0, w), uniform(0, h)); |
||
foreach (a, b; zip( |
foreach (a, b; hor.zip(ver ~ [])) |
||
join(a ~ ["+\n"] ~ b).writeln |
join(a ~ ["+\n"] ~ b).writeln; |
||
}</lang> |
}</lang> |
||
{{out}} |
{{out}} |