How to resolve the algorithm Maze generation step by step in the D programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Maze generation step by step in the D programming language
Table of Contents
Problem Statement
Generate and show a maze, using the simple Depth-first search algorithm.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Maze generation step by step in the D programming language
Source code in the d programming language
void main() @safe {
import std.stdio, std.algorithm, std.range, std.random;
enum uint w = 14, h = 10;
auto vis = new bool[][](h, w),
hor = iota(h + 1).map!(_ => ["+---"].replicate(w)).array,
ver = h.iota.map!(_ => ["| "].replicate(w) ~ "|").array;
void walk(in uint x, in uint y) /*nothrow*/ @safe /*@nogc*/ {
vis[y][x] = true;
//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) {
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]);
}
}
walk(uniform(0, w), uniform(0, h));
foreach (const a, const b; hor.zip(ver ~ []))
join(a ~ "+\n" ~ b).writeln;
}
You may also check:How to resolve the algorithm Gaussian elimination step by step in the Generic programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the GDScript programming language
You may also check:How to resolve the algorithm Short-circuit evaluation step by step in the zkl programming language
You may also check:How to resolve the algorithm Program termination step by step in the Axe programming language
You may also check:How to resolve the algorithm Jaro similarity step by step in the VBA programming language