Maze generation: Difference between revisions

(→‎{{header|Perl 6}}: upgrade to Unicode line drawing, unify maze representation)
(→‎{{header|Perl 6}}: demagicalize)
Line 2,297:
=={{header|Perl 6}}==
Supply a width and height and optionally the x,y grid coords for the starting cell. If no starting cell is supplied, a random one will be selected automatically. 0,0 is the top left corner.
<lang perl6>constant mapping = :OPEN(' '),
<lang perl6>my @code = ' ', < ╵ ╶ └ ╷ │ ┌ ├ ╴ ┘ ─ ┴ ┐ ┤ ┬ ┼ x >;
:N< ╵ >,
:E< ╶ >,
:NE< └ >,
:S< ╷ >,
:NS< │ >,
:ES< ┌ >,
:NES< ├ >,
:W< ╴ >,
:NW< ┘ >,
:EW< ─ >,
:NEW< ┴ >,
:SW< ┐ >,
:NSW< ┤ >,
:ESW< ┬ >,
:NESW< ┼ >,
:TODO< x >,
:TRIED< · >;
 
enum Code (mapping.map: *.key);
enum Direction <DeadEnd Up Right Down Left>;
my @code = mapping.map: *.value;
 
enum Direction <DeadEnd Up Right Down Left>;
display gen_maze( 29, 19 );
 
sub gen_maze ( $X,
Line 2,309 ⟶ 2,327:
{
my @maze;
push @maze, [ 6ES, 0-N, (14ESW, 10EW) xx $X - 1, 12, 0SW ];
push @maze, [ (5NS, 16TODO) xx $X, 5, 0NS ];
for 1 ..^ $Y {
push @maze, [ 7NES, 10EW, (15NESW, 10EW) xx $X - 1, 13, 0NSW ];
push @maze, [ (5NS, 16TODO) xx $X, 5, 0NS ];
}
push @maze, [ 3NE, (10EW, 11NEW) xx $X - 1, 0, 9-NS, 0NW ];
@maze[$start_y][$start_x] = 0OPEN;
 
my @stack;
my $current = [$start_x, $start_y];
loop {
if my $dir = pick_direction( @maze, $current ) {
@stack.push: $current;
$current = move( @maze, $dir, $current );
}
else {
Line 2,331 ⟶ 2,349:
}
return @maze;
 
sub pick_direction(@maze, [$x,$y]) {
my @neighbors =
(Up if @maze[$y - 2][$x]),
(Down if @maze[$y + 2][$x]),
(Left if @maze[$y][$x - 2]),
(Right if @maze[$y][$x + 2]);
@neighbors.pick or DeadEnd;
}
}
 
sub move (@maze, $dir, @cur) {
my ($x,$y) = @cur;
given $dir {
when Up { --$y; @maze[--$y][$x] = 0OPEN; @maze[$y][$x-1] -= 2E; @maze[$y--][$x+1] -= 8; --$yW; }
when Down { ++$y; @maze[++$y][$x] = 0OPEN; @maze[$y][$x-1] -= 2E; @maze[$y++][$x+1] -= 8; ++$yW; }
when Left { --$x; @maze[$y][--$x] = 0OPEN; @maze[$y-1][$x] -= 4S; @maze[$y+1][$x--] -= 1; --$xN; }
when Right { ++$x; @maze[$y][++$x] = 0OPEN; @maze[$y-1][$x] -= 4S; @maze[$y+1][$x++] -= 1; ++$xN; }
}
@maze[$y][$x] = 0;
[$x,$y];
}
}
 
Line 2,357 ⟶ 2,375:
for @maze -> @y {
for @y -> $w, $c {
print @code[abs $w];
if $c >= 0 { print @code[$c] x 3 }
else { print ' ', @code[abs $c], ' ' }
}
say @code[@y[*-1]];
print "\n";
}
}</lang>
 
display gen_maze( 29, 19 );</lang>
{{out}}
<small><pre>┌ ╵ ────────────────────────────┬───────────────────────────────────────────┬───────────┬───────────────────────────┐
<small><pre>┌ ────────────┬───────────┬───────────────┬───────────┬───────────────────┬───────────────────┬───────────────┬───┐
│ ╶───────────┬───────────┐ │ ┌───────────────────────╴ ┌───────┐ ├───╴ ╷ │ ┌───────────┬───┬───╴ │
│ ┌───────╴ │ ╶───┐ │ ┌───┐ ╷ │ ╶───┐ │ ┌───────────┐ ╵ ╶───┬───╴ ╷ └───╴ ┌───┐ ╵ │
│ │ │ │ │ │ │
│ ┌───────┐ ╵ ┌───┐ ├───┘ │ ┌───────────┬───────────┤ ╶───┤ │ ╶───┴───┤ │ ┌───┐ │ │ ╶───┤
│ │ ┌───────┘ ╷ └───┘ ╵ │ │ └───┐ │ ╵ │ ┌───┐ ├───────┐ │ ┌───┴───────────┘ └───┐ │
│ │ │ │ │ │ │ │ │
│ └───╴ └───────┤ │ ╵ ┌───┘ │ ╷ ╶───┤ ┌───┐ │ ╷ │ ├───────╴ │ ╵ │ │ │ └───┐ │
│ │ └───┐ ┌───┴───────┬───╴ │ ├───┐ │ └───┬───┘ │ ╵ ╵ ╷ └───┘ ├───────────┐ ╶───┐ ╵ │
│ │ │ │ │ │ │ │ │
├───────┬───────┐ │ ├───────┤ ╶───┤ └───┐ ╵ │ │ ╵ │ ╵ │ ┌───┐ └───┬───┘ │ │ ╷ │ │
│ └───┐ ├───┘ ┌───┐ └───────┘ │ ╵ │ ┌───┘ ┌───┴───────┬───┴───────────┘ ┌───┐ └───┐ └───────┤
│ │ │ │ │ │ │ │
│ ╶───┤ ╷ ╵ │ ╵ ╷ └───┐ ├───┐ ├───────┤ └───────┴───────┘ │ └───┐ └───╴ │ ╵ ├───┘ │
├───╴ │ ╵ ┌───┘ ├───────┬───────┘ ╶───┴───┤ ┌───┘ ┌───┐ │ ╶───┬───────┬───┘ └───┐ ├───────┐ │
│ │ │ │ │ │ │
├───╴ │ ├───────┴───┐ ├───╴ │ │ │ ╵ ╷ └───┐ ╶───┬───────┬───┘ ┌───┴───────╴ │ ┌───┘ ╷ │
│ ┌───┴───┬───┴───╴ │ ╷ │ ╶───┬───────┐ ╵ │ ╶───┤ │ └───┐ ╵ ╷ │ ╶───┐ │ │ ╶───┘ │
│ │ │ │ │ │
│ ╷ │ │ ╶───┐ └───┤ ╷ │ │ └───────┴───┐ └───╴ │ ╷ ╵ ╷ ╵ ╶───────┬───┤ │ ┌───┴───┤
│ ╵ ╷ │ ┌───────┘ │ ╵ ┌───┘ ╷ └───┬───┴───┐ │ └───╴ └───┬───┘ ├───╴ │ │ └───────┐ │
│ │ │ │
│ │ │ │ ╷ ├───╴ ╵ │ │ │ ┌───────┐ └───────────┤ └───┬───┴───────────┐ ╵ │ │ ╵ ╷ │
│ ╶───┤ │ ╵ ┌───────┼───────┤ ╶───┼───┐ ╵ ╷ │ ├───────┬───┐ ╵ ┌───┘ ┌───┘ ├───┬───╴ │ │
│ │ │ │ │ │ │ │ │ │ │
┌───┘╶───┘├───┘ ┌───────┴───┘ ╷ └───┐├───┬───┘┌───┴───────╴ ╵ ╷├───┐├───────┘╶───────┐ ╶───┤ ╶───┘└───┐╶───┴───┤└───┬───┘
│ │ │ │ │ │ │
│ └───┘ │ ┌───┘ │ ┌───┬───────┼───╴ │ ╶───┬───────┤ ╵ │ └───┬───╴ ├───┐ │ └───┐ │ ╷ │
├───┘ ┌───┴───┬───┐ ├───────┴───┐ └───┐ ╵ │ ╷ └───┬───┘ │ │ ╶───┬───╴ ├───────────┴───────┐ │
│ │ │ │ │ │ │ │ │ │
┌───┘┌───────┘ ├───────┤ │ ╵ ╶───┐ ┌───┴───┐ ┌───┴───╴ ╶───┘ ├───╴┌───┴───╴ │ ┌───┘ └───╴┌───┘┌───┐ ╶───────┘
│ │ │ │ │ │ │ │ │ │ │
│ └───╴ ╷ │ ╷ │ └───┐ │ ╵ │ ╷ ╵ ├───────┐ │ ╶───────┤ └───┐ │ └───┤ ┌───┘ │ │
│ ╵ ╶───┤ └───┐ ├───╴ │ │ │ ┌───────┘ │ ╶───┤ └───────┬───────┘ │ │ │ └───────┬───────┤
│ │ │ │ │ │ │ │ │ │ │ │
├───────────┤ │ │ └───╴ │ └───────┤ └───────┘ ╷ └───┴───┬───╴ ├───╴ │ └───┐ │ │ ╶───┴───┤
│ ┌───────┴───╴ ├───┘ ╶───┤ │ │ │ ┌───────┴───────┴───────┐ │ ╷ ┌───┴───┤ └───╴ ╷ └───┐ │
│ │ │ │
│ ┌───╴ │ │ ├───╴ ┌───┴───────┬───┴───┐ ┌───────┴───────┐ ╵ ┌───┤ ╶───┤ ╷ │ ╵ └───┬───╴ │
│ │ ╶───────────┘ ┌───────┘ │ │ │ ╵ ┌───────────┐ ╶───┴───┘ │ │ ╷ └───────────┴───┐ ╵ │
│ │ │ │ │ │ │ │
│ │ ╶───┘ │ │ ╶───┤ ╶───┐ ╵ ╷ │ └───╴ ┌───╴ └───────┘ ├───╴ │ ├───┴───────┬───┘ ╷ │
│ ├───────┬───────────┤ ╶───┬───┘ │ └───┬───┘ ╷ ╶───┴───────┬───┐ │ │ └───┬───────────┐ └───╴ │
│ │ │ │ │ │ │
│ ├───────┬───┘ ├───────┴───┐ ├───────┤ └───────────┤ ┌───────────┐ │ ┌───┘ │ ╶───┐ ╵ ┌───┘ │
│ ╵ ╷ │ ╷ ╶───┴───┐ └───────┴───┐ └───┐ └───┬───╴ ╷ ╵ │ └───┴───┐ ├───────┐ └───────────┤
│ │
│ └───╴ │ ╷ │ ╶───┐ ╵ │ ╷ └───────────┐ │ │ ┌───┐ └───┘ │ ╶───┴───┐ └───────┴───────┤
├───┬───┘ │ ├───────╴ └───┬───┐ ╷ ├───╴ ├───────┤ ╶───┴───┐ └───┐ ╷ │ ╵ ╷ ├───╴ ┌───┐ │
│ │ │ │
├───────╴ │ └───┴───╴ └───────┴───┴───────────╴ │ └───┘ ╵ └───────────┴───╴ ╷ └───────────────┐ │
│ ╵ ╶───┴───┘ ┌───────╴ ╵ │ └───┘ ╶───┘ ╷ └───────╴ ├───╴ │ └───┴───────┘ ╵ ┌───┘ ╵ │
└───────────┴───────────────────────────────────────────┴───────────────────────────────────┴───────────────────┴ │ ┘</pre></small>
└───────────────────┴───────────────┴───────────────────┴───────────────┴───────┴───────────────────────┴──────── ┘</pre></small>
 
=={{header|PicoLisp}}==
Anonymous user