RosettaCodeData/Task/Maze-generation/Rascal/maze-generation.rascal

30 lines
827 B
Text
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
import IO;
import util::Math;
import List;
public void make_maze(int w, int h){
2026-04-30 12:34:36 -04:00
vis = [[0 | _ <- [1..w]] | _ <- [1..h]];
ver = [["| "| _ <- [1..w]] + ["|"] | _ <- [1..h]] + [[]];
hor = [["+--"| _ <- [1..w]] + ["+"] | _ <- [1..h + 1]];
2023-07-01 11:58:00 -04:00
2026-04-30 12:34:36 -04:00
void walk(int x, int y){
vis[y][x] = 1;
2023-07-01 11:58:00 -04:00
2026-04-30 12:34:36 -04:00
d = [<x - 1, y>, <x, y + 1>, <x + 1, y>, <x, y - 1>];
while (d != []){
<<xx, yy>, d> = takeOneFrom(d);
if (xx < 0 || yy < 0 || xx >= w || yy >= w) continue;
if (vis[yy][xx] == 1) continue;
if (xx == x) hor[max([y, yy])][x] = "+ ";
if (yy == y) ver[y][max([x, xx])] = " ";
walk(xx, yy);
}
}
walk(arbInt(w), arbInt(h));
for (<a, b> <- zip(hor, ver)){
println(("" | it + "<z>" | z <- a));
println(("" | it + "<z>" | z <- b));
}
2023-07-01 11:58:00 -04:00
}