Jump to content

Maze generation: Difference between revisions

Simpler D version
(Shorter first D version)
(Simpler D version)
Line 814:
enum int w = 16, h = 8;
alias std.array.replicate R;
auto verhor = 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);
 
void walk(in int x, in int y) /*nothrow*/ {
vis[y][x] = true;
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)];
randomShuffle(d);
foreach (p; d) {
Line 834 ⟶ 832:
 
walk(uniform(0, w), uniform(0, h));
foreach (a, b; lockstep(hor, ver ~ []))
writeln(join(a ~ ["+\n"] ~ b));
}</lang>
Output example:
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.