Maze generation: Difference between revisions

Content added Content deleted
(→‎{{header|REXX}}: included a CHANGESTR subroutine (function) as a link. -- ~~~~)
(→‎{{header|Perl 6}}: upgrade to Unicode line drawing, unify maze representation)
Line 2,296: Line 2,296:


=={{header|Perl 6}}==
=={{header|Perl 6}}==
{{Works with|Rakudo|2011.01}}
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.
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>my @code = ' ', < ╵ ╶ └ ╷ │ ┌ ├ ╴ ┘ ─ ┴ ┐ ┤ ┬ ┼ x >;
<lang perl6>display( gen_maze( 11, 8 ) );


enum Direction <DeadEnd Up Right Down Left>;
sub gen_maze ( $x_size,

$y_size,
display gen_maze( 29, 19 );
$start_x = (^$x_size).pick,

$start_y = (^$y_size).pick )
sub gen_maze ( $X,
$Y,
$start_x = (^$X).pick * 2 + 1,
$start_y = (^$Y).pick * 2 + 1 )
{
{
my %walls;
my @maze;
my @maze;
push @maze, [ 6, 0, (14, 10) xx $X - 1, 12, 0 ];
for ^$y_size -> $x {
@maze[$x] = [ 1 xx $x_size];
push @maze, [ (5, 16) xx $X, 5, 0 ];
for 1 ..^ $Y {
%walls{'y'}[$x] = ['|' xx $x_size];
%walls{'x'}[$x] = ['---' xx $x_size];
push @maze, [ 7, 10, (15, 10) xx $X - 1, 13, 0 ];
push @maze, [ (5, 16) xx $X, 5, 0 ];
}
}
push @maze, [ 3, (10, 11) xx $X - 1, 0, 9, 0 ];
@maze[$start_y][$start_x] = 0;

my @stack;
my @stack;
my @current = $start_y, $start_x;
my $current = [$start_x, $start_y];
loop {
loop {
if my @next = get_unvisited_neighbors( @maze, @current ) {
if my $dir = pick_direction( @maze, $current ) {
@stack.push: [@current];
@stack.push: $current;
move( @maze, @next, @current, %walls );
$current = move( @maze, $dir, $current );
@current := @next;
}
}
else {
else {
last unless @stack;
last unless @stack;
$current = @stack.pop;
@current := @stack.pop;
}
}
}
}
return %walls;
return @maze;
}
}


sub get_unvisited_neighbors(@maze, @current) {
sub pick_direction(@maze, [$x,$y]) {
my ($x, $y) = @current;
my @neighbors =
(Up if @maze[$y - 2][$x]),
my @neighbors;
(Down if @maze[$y + 2][$x]),
@neighbors.push([ $x-1, $y ]) if $x > 0 and @maze[$x-1][$y];
(Left if @maze[$y][$x - 2]),
@neighbors.push([ $x+1, $y ]) if $x < @maze and @maze[$x+1][$y];
@neighbors.push([ $x, $y-1 ]) if $y > 0 and @maze[$x][$y-1];
(Right if @maze[$y][$x + 2]);
@neighbors.push([ $x, $y+1 ]) if $y < @maze[0] and @maze[$x][$y+1];
@neighbors.pick or DeadEnd;
return |@neighbors.roll(1) if @neighbors;
}
}


sub move ($maze, $next, $current, $walls) {
sub move (@maze, $dir, @cur) {
$maze[$next[0]][$next[1]] = 0;
my ($x,$y) = @cur;
given () {
given $dir {
when $next[0] < $current[0] { $walls{'x'}[$next[0]][$current[1]] = ' '}
when Up { --$y; @maze[$y][$x] = 0; @maze[$y][$x-1] -= 2; @maze[$y][$x+1] -= 8; --$y; }
when $next[0] > $current[0] { $walls{'x'}[$current[0]][$current[1]] = ' '}
when Down { ++$y; @maze[$y][$x] = 0; @maze[$y][$x-1] -= 2; @maze[$y][$x+1] -= 8; ++$y; }
when $next[1] < $current[1] { $walls{'y'}[$current[0]][$current[1]] = ' ' }
when Left { --$x; @maze[$y][$x] = 0; @maze[$y-1][$x] -= 4; @maze[$y+1][$x] -= 1; --$x; }
when $next[1] > $current[1] { $walls{'y'}[$current[0]][$next[1]] = ' ' }
when Right { ++$x; @maze[$y][$x] = 0; @maze[$y-1][$x] -= 4; @maze[$y+1][$x] -= 1; ++$x; }
}
}
@maze[$y][$x] = 0;
[$x,$y];
}
}


sub display ($walls) {
sub display (@maze) {
for @maze -> @y {
say '+' ~ ('---' xx $walls{'y'}[0]).join('+') ~ '+';
for ^$walls{'x'} -> $i {
for @y -> $w, $c {
print @code[$w];
say ~$walls{'y'}[$i].join(' ') ~ ' |';
say '+' ~ $walls{'x'}[$i].join('+') ~ '+';
if $c >= 0 { print @code[$c] x 3 }
}
print "\n";
}
}
}</lang>
}</lang>
{{out}}
{{out}}
<small><pre>┌ ────────────┬───────────┬───────────────┬───────────┬───────────────────┬───────────────────┬───────────────┬───┐
<pre>
│ │ │ │ │ │ │ │ │
+---+---+---+---+---+---+---+---+---+---+---+
│ ┌───────╴ │ ╶───┐ │ ┌───┐ ╷ │ ╶───┐ │ ┌───────────┐ ╵ ╶───┬───╴ ╷ └───╴ ┌───┐ ╵ │
| | | | |
│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │
+---+---+ + + + +---+---+---+---+ +
│ │ ┌───────┘ ╷ └───┘ ╵ │ │ └───┐ │ ╵ │ ┌───┐ ├───────┐ │ ┌───┴───────────┘ └───┐ │
| | | | |
│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │
+ +---+---+ +---+---+ +---+---+---+---+
│ │ └───┐ ┌───┴───────┬───╴ │ ├───┐ │ └───┬───┘ │ ╵ ╵ ╷ └───┘ ├───────────┐ ╶───┐ ╵ │
| | | |
│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │
+ +---+ +---+ +---+ +---+---+---+ +
│ └───┐ ├───┘ ┌───┐ └───────┘ │ ╵ │ ┌───┘ ┌───┴───────┬───┴───────────┘ ┌───┐ └───┐ └───────┤
| | | | | |
│ │ │ │ │ │ │ │ │ │ │ │ │ │
+ +---+---+ +---+ +---+ + +---+---+
├───╴ │ ╵ ┌───┘ ├───────┬───────┘ ╶───┴───┤ ┌───┘ ┌───┐ │ ╶───┬───────┬───┘ └───┐ ├───────┐ │
| | | | | | | |
│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │
+---+ + + +---+---+ + +---+ + +
│ ┌───┴───┬───┴───╴ │ ╷ │ ╶───┬───────┐ ╵ │ ╶───┤ │ └───┐ ╵ ╷ │ ╶───┐ │ │ ╶───┘ │
| | | | | | |
│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │
+ +---+---+---+ + + + +---+---+ +
│ ╵ ╷ │ ┌───────┘ │ ╵ ┌───┘ ╷ └───┬───┴───┐ │ └───╴ └───┬───┘ ├───╴ │ │ └───────┐ │
| | | | | |
│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │
+ + +---+---+---+---+---+---+ +---+ +
│ ╶───┤ │ ╵ ┌───────┼───────┤ ╶───┼───┐ ╵ ╷ │ ├───────┬───┐ ╵ ┌───┘ ┌───┘ ├───┬───╴ │ │
| | |
│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │
+---+---+---+---+---+---+---+---+---+---+---+
│ ┌───┘ │ ╶───┘ ╷ ╵ ╷ └───┐ ╵ ├───┬───┘ │ ╵ ╷ │ ├───────┘ ╶───┤ ╶───┘ │ ╶───┴───┤
</pre>
│ │ │ │ │ │ │ │ │ │ │ │ │ │ │
├───┘ ┌───┴───┬───┐ ├───────┴───┐ └───┐ ╵ │ ╷ └───┬───┘ │ │ ╶───┬───╴ ├───────────┴───────┐ │
│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │
│ ┌───┘ ╷ │ ╵ │ ╶───┐ │ ┌───┴───╴ │ ├───╴ │ ┌───┘ └───╴ │ ╷ │ ┌───┐ ╶───────┘ │
│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │
│ ╵ ╶───┤ └───┐ ├───╴ │ │ │ ┌───────┘ │ ╶───┤ └───────┬───────┘ │ │ │ └───────┬───────┤
│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │
│ ┌───────┴───╴ ├───┘ ╶───┤ │ │ │ ┌───────┴───────┴───────┐ │ ╷ ┌───┴───┤ └───╴ ╷ └───┐ │
│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │
│ │ ╶───────────┘ ┌───────┘ │ │ │ ╵ ┌───────────┐ ╶───┴───┘ │ │ ╷ └───────────┴───┐ ╵ │
│ │ │ │ │ │ │ │ │ │ │ │ │
│ ├───────┬───────────┤ ╶───┬───┘ │ └───┬───┘ ╷ ╶───┴───────┬───┐ │ │ └───┬───────────┐ └───╴ │
│ │ │ │ │ │ │ │ │ │ │ │ │ │ │
│ ╵ ╷ │ ╷ ╶───┴───┐ └───────┴───┐ └───┐ └───┬───╴ ╷ ╵ │ └───┴───┐ ├───────┐ └───────────┤
│ │ │ │ │ │ │ │ │ │ │ │ │ │
├───┬───┘ │ ├───────╴ └───┬───┐ ╷ ├───╴ ├───────┤ ╶───┴───┐ └───┐ ╷ │ ╵ ╷ ├───╴ ┌───┐ │
│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │
│ ╵ ╶───┴───┘ ┌───────╴ ╵ │ └───┘ ╶───┘ ╷ └───────╴ ├───╴ │ └───┴───────┘ ╵ ┌───┘ ╵ │
│ │ │ │ │ │ │ │
└───────────────────┴───────────────┴───────────────────┴───────────────┴───────┴───────────────────────┴──────── ┘</pre></small>


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==