Maze generation: Difference between revisions

Shorter first D version
(Updated D code)
(Shorter first D version)
Line 807:
 
=={{header|D}}==
{{trans|F#Python}}
<lang d>import std.stdio, std.randomalgorithm, std.algorithmrange, std.arrayrandom,std.typecons;
std.string, std.typecons;
 
void main() {
enum int Ww = 1116, Hh = 8;
alias std.array.replicate R;
auto hWalls = new bool[][](W, H),
auto ver = array(map!((_){ return vWallsR(["| = new"], w) ~ bool["|"][](W,; H}),
seen = new bool[][](W, H (iota(h))) ~ [];
auto nshor = array(filtermap!((x_){ return xR(["+--"], !=w) n~ ["+"]; })(ns));
alias Tuple!(uint,"x", uint,"y") P;
(iota(h + 1)));
auto hWallsvis = new bool[][](Wh, Hw),;
 
void visitwalk(in int x, in int y) /*nothrow*/ {
seenvis[xy][yx] = true;
P[]alias D = [PTuple!(x-1uint,y), P("x+1,y)", P(xuint,"y-1"), P(x,y+1)];
P[]auto nsd = array(filter!([P(px-1,y){return, p.P(x, <y+1), WP(x+1, && p.y), <P(x, H; })(D)y-1)];
while randomShuffle(ns.lengthd) {;
foreach immutable n = ns[uniform(0,p; $d)]; {
if (!seen[np.x] >= w || p.y >= h || vis[np.y][p.x]) {continue;
if (p.x !== n.x) ? hWallshor[minmax(xy, np.xy)][yx] := "+ ";
if (p.y == y) vWallsver[xy][minmax(yx, np.yx)]) = true" ";
walk(p.x, visit(np.tupleofy);
}
ns = array(filter!((x){ return x != n; })(ns));
}
}
visit(uniform(0, W), uniform(0, H));
 
writelnwalk("+"uniform(0, "--+".replicatew), uniform(W0, h));
foreach (ya, b; 0lockstep(hor, .. Hver)) {
stringwriteln(join(a s =~ ["|\n"] ~ b));
foreach (x; 0 .. W)
s ~= hWalls[x][y] ? " " : " |";
s ~= "\n+";
foreach (x; 0 .. W)
s ~= vWalls[x][y] ? " +" : "--+";
writeln(s);
}
}</lang>
Output example:
<pre>+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| | | | |
+ +--+ + +--+--+ + +-- +--+ + --+ + +--+--+
| | | | | | | | | | | |
+ + + + +--+ +--+--+ + --+--+ +--+ +--+ +
| | | | | | | |
+ +--+-- +--+--+ +--+--+ +--+ +--+ +--+--+ +
| | | | | | | | |
+ --+ + --+ +--+--+ + +--+ + +--+--+--+ + +
| | | | | | | | | | |
+ +--+ +--+ + +-- +--+ + +--+--+ + +--+ +
| | | | | | | | |
+ --+ + --+--+ --+--+--+--+--+--+ +--+--+ + +--+
| | | | | | | | |
+ +--+ +--+ --+--+--+ +-- + + + +--+--+ + +
| | | | |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+</pre>
===Alternative version ===
See [[Maze_solving#D]]
Anonymous user