RosettaCodeData/Task/Maze-generation/D/maze-generation.d

23 lines
900 B
D
Raw Permalink Normal View History

2015-02-20 00:35:01 -05:00
void main() @safe {
2014-01-17 05:32:22 +00:00
import std.stdio, std.algorithm, std.range, std.random;
enum uint w = 14, h = 10;
2013-04-10 21:29:02 -07:00
auto vis = new bool[][](h, w),
2014-01-17 05:32:22 +00:00
hor = iota(h + 1).map!(_ => ["+---"].replicate(w)).array,
ver = h.iota.map!(_ => ["| "].replicate(w) ~ "|").array;
2013-04-10 21:29:02 -07:00
2015-02-20 00:35:01 -05:00
void walk(in uint x, in uint y) /*nothrow*/ @safe /*@nogc*/ {
2013-04-10 21:29:02 -07:00
vis[y][x] = true;
2015-02-20 00:35:01 -05:00
//foreach (immutable p; [[x-1,y], [x,y+1], [x+1,y], [x,y-1]].randomCover) {
foreach (const p; [[x-1, y], [x, y+1], [x+1, y], [x, y-1]].randomCover) {
2014-01-17 05:32:22 +00:00
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[1] == y) ver[y][max(x, p[0])] = " ";
walk(p[0], p[1]);
2013-04-10 21:29:02 -07:00
}
}
walk(uniform(0, w), uniform(0, h));
2014-04-02 16:56:35 +00:00
foreach (const a, const b; hor.zip(ver ~ []))
2015-02-20 00:35:01 -05:00
join(a ~ "+\n" ~ b).writeln;
2013-04-10 21:29:02 -07:00
}