Maze generation: Difference between revisions
Content added Content deleted
(Shorter first D version) |
(Simpler D version) |
||
Line 814: | Line 814: | ||
enum int w = 16, h = 8; |
enum int w = 16, h = 8; |
||
alias std.array.replicate R; |
alias std.array.replicate R; |
||
auto |
auto hor = array(map!((_){ return R(["+--"], w); })(iota(h+1))); |
||
auto ver = array(map!((_){ return R(["| "],w)~"|";})(iota(h))); |
|||
auto hor = array(map!((_){ return R(["+--"], w) ~ ["+"]; }) |
|||
(iota(h + 1))); |
|||
auto vis = new bool[][](h, w); |
auto vis = new bool[][](h, w); |
||
void walk(in int x, in int y) /*nothrow*/ { |
void walk(in int x, in int y) /*nothrow*/ { |
||
vis[y][x] = true; |
vis[y][x] = true; |
||
alias Tuple!(uint,"x", uint,"y") P; |
alias Tuple!(uint,"x", uint,"y") P; // 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)]; |
||
randomShuffle(d); |
randomShuffle(d); |
||
foreach (p; d) { |
foreach (p; d) { |
||
Line 834: | Line 832: | ||
walk(uniform(0, w), uniform(0, h)); |
walk(uniform(0, w), uniform(0, h)); |
||
foreach (a, b; lockstep(hor, ver)) |
foreach (a, b; lockstep(hor, ver ~ [])) |
||
writeln(join(a ~ ["\n"] ~ b)); |
writeln(join(a ~ ["+\n"] ~ b)); |
||
}</lang> |
}</lang> |
||
Output example: |
Output example: |