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