Maze generation: Difference between revisions
Content added Content deleted
(Updated D code) |
(Shorter first D version) |
||
Line 807: | Line 807: | ||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans| |
{{trans|Python}} |
||
<lang d>import std.stdio,std. |
<lang d>import std.stdio, std.algorithm, std.range, std.random, |
||
std.string, std.typecons; |
|||
void main() { |
void main() { |
||
enum int |
enum int w = 16, h = 8; |
||
alias std.array.replicate R; |
|||
⚫ | |||
auto ver = array(map!((_){ return R(["| "], w) ~ ["|"]; }) |
|||
(iota(h))) ~ []; |
|||
⚫ | |||
alias Tuple!(uint,"x", uint,"y") P; |
|||
(iota(h + 1))); |
|||
⚫ | |||
void |
void walk(in int x, in int y) /*nothrow*/ { |
||
vis[y][x] = true; |
|||
alias Tuple!(uint,"x", uint,"y") P; |
|||
auto d = [P(x-1,y), P(x, y+1), P(x+1, y), P(x, y-1)]; |
|||
randomShuffle(d); |
|||
foreach (p; d) { |
|||
if ( |
if (p.x >= w || p.y >= h || vis[p.y][p.x]) continue; |
||
if (p.x == x) hor[max(y, p.y)][x] = "+ "; |
|||
if (p.y == y) ver[y][max(x, p.x)] = " "; |
|||
walk(p.x, p.y); |
|||
} |
|||
⚫ | |||
} |
} |
||
} |
} |
||
visit(uniform(0, W), uniform(0, H)); |
|||
walk(uniform(0, w), uniform(0, h)); |
|||
foreach ( |
foreach (a, b; lockstep(hor, ver)) |
||
writeln(join(a ~ ["\n"] ~ b)); |
|||
foreach (x; 0 .. W) |
|||
s ~= hWalls[x][y] ? " " : " |"; |
|||
s ~= "\n+"; |
|||
foreach (x; 0 .. W) |
|||
s ~= vWalls[x][y] ? " +" : "--+"; |
|||
writeln(s); |
|||
} |
|||
}</lang> |
}</lang> |
||
Output example: |
Output example: |
||
<pre>+--+--+--+--+--+--+--+--+--+--+--+ |
<pre>+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
||
| | | | | |
| | | | | |
||
+ + + + + + |
+ +--+ + +--+--+ + + +--+ +--+ + +--+--+ |
||
| | | | |
| | | | | | | | | | | |
||
+ +--+ +--+--+ + |
+ + + + +--+ +--+--+ +--+--+ +--+ +--+ + |
||
| |
| | | | | | | | |
||
+ +--+ |
+ +--+ +--+--+ +--+--+ +--+ +--+ +--+--+ + |
||
| | | | | | |
| | | | | | | | | |
||
+ |
+--+ +--+ +--+--+ + +--+ + +--+--+--+ + + |
||
| | | | | | |
| | | | | | | | | | | |
||
+--+--+ + + |
+ +--+ +--+ + + +--+ + +--+--+ + +--+ + |
||
| | | | | |
| | | | | | | | |
||
+ |
+--+ +--+--+--+--+--+--+--+--+ +--+--+ + +--+ |
||
| | |
| | | | | | | | |
||
+ +--+--+ |
+ +--+ +--+--+--+--+ + + + + +--+--+ + + |
||
| | | |
| | | | | |
||
+--+--+--+--+--+--+--+--+--+--+--+</pre> |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+</pre> |
||
===Alternative version === |
===Alternative version === |
||
See [[Maze_solving#D]] |
See [[Maze_solving#D]] |