Anonymous user
Maze generation: Difference between revisions
→{{header|Perl 6}}: demagicalize
(→{{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(' '),
: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, [
push @maze, [ (
for 1 ..^ $Y {
push @maze, [
push @maze, [ (
}
push @maze, [
@maze[$start_y][$start_x] =
my @stack;
my $current = [$start_x, $start_y];
loop {
if my $dir = pick_direction(
@stack.push: $current;
$current = move(
}
else {
Line 2,331 ⟶ 2,349:
}
return @maze;
}▼
sub pick_direction(
}
sub move (
}
}
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]];
}
▲}
▲display gen_maze( 29, 19 );</lang>
{{out}}
<small><pre>┌ ╵ ────────────────────────────┬───────────────────────────────────────────┬───────────┬───────────────────────────┐
│
│ ╶───────────┬───────────┐ │ ┌───────────────────────╴ ┌───────┐ ├───╴ ╷ │ ┌───────────┬───┬───╴ │
│
│ ┌───────┐ ╵ ┌───┐ ├───┘ │ ┌───────────┬───────────┤ ╶───┤ │ ╶───┴───┤ │ ┌───┐ │ │ ╶───┤
│ │
│ └───╴ └───────┤ │ ╵ ┌───┘ │ ╷ ╶───┤ ┌───┐ │ ╷ │ ├───────╴ │ ╵ │ │ │ └───┐ │
│
├───────┬───────┐ │ ├───────┤ ╶───┤ └───┐ ╵ │ │ ╵ │ ╵ │ ┌───┐ └───┬───┘ │ │ ╷ │ │
│ │
│ ╶───┤ ╷ ╵ │ ╵ ╷ └───┐ ├───┐ ├───────┤ └───────┴───────┘ │ └───┐ └───╴ │ ╵ ├───┘ │
│ │ │ │ │ │ │ │
├───╴ │ ├───────┴───┐ ├───╴ │ │ │ ╵ ╷ └───┐ ╶───┬───────┬───┘ ┌───┴───────╴ │ ┌───┘ ╷ │
│
│ ╷ │ │ ╶───┐ └───┤ ╷ │ │ └───────┴───┐ └───╴ │ ╷ ╵ ╷ ╵ ╶───────┬───┤ │ ┌───┴───┤
│
│ │ │ │ ╷ ├───╴ ╵ │ │ │ ┌───────┐ └───────────┤ └───┬───┴───────────┐ ╵ │ │ ╵ ╷ │
│ │ │ │ │ │
│
│ │
│ └───┘ │ ┌───┘ │ ┌───┬───────┼───╴ │ ╶───┬───────┤ ╵ │ └───┬───╴ ├───┐ │ └───┐ │ ╷ │
│
│
│ │
│ └───╴ ╷ │ ╷ │ └───┐ │ ╵ │ ╷ ╵ ├───────┐ │ ╶───────┤ └───┐ │ └───┤ ┌───┘ │ │
│ │
├───────────┤ │ │ └───╴ │ └───────┤ └───────┘ ╷ └───┴───┬───╴ ├───╴ │ └───┐ │ │ ╶───┴───┤
│
│ ┌───╴ │ │ ├───╴ ┌───┴───────┬───┴───┐ ┌───────┴───────┐ ╵ ┌───┤ ╶───┤ ╷ │ ╵ └───┬───╴ │
│ │ │ │ │ │ │
│ │ ╶───┘ │ │ ╶───┤ ╶───┐ ╵ ╷ │ └───╴ ┌───╴ └───────┘ ├───╴ │ ├───┴───────┬───┘ ╷ │
│ │ │
│ ├───────┬───┘ ├───────┴───┐ ├───────┤ └───────────┤ ┌───────────┐ │ ┌───┘ │ ╶───┐ ╵ ┌───┘ │
│ │
│ └───╴ │ ╷ │ ╶───┐ ╵ │ ╷ └───────────┐ │ │ ┌───┐ └───┘ │ ╶───┴───┐ └───────┴───────┤
│
├───────╴ │ └───┴───╴ └───────┴───┴───────────╴ │ └───┘ ╵ └───────────┴───╴ ╷ └───────────────┐ │
│ │
└───────────┴───────────────────────────────────────────┴───────────────────────────────────┴───────────────────┴ │ ┘</pre></small>
=={{header|PicoLisp}}==
|