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 >; |
|||
⚫ | |||
enum Direction <DeadEnd Up Right Down Left>; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
{ |
{ |
||
my %walls; |
|||
my @maze; |
my @maze; |
||
push @maze, [ 6, 0, (14, 10) xx $X - 1, 12, 0 ]; |
|||
⚫ | |||
push @maze, [ (5, 16) xx $X, 5, 0 ]; |
|||
for 1 ..^ $Y { |
|||
%walls{'y'}[$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 |
my $current = [$start_x, $start_y]; |
||
loop { |
loop { |
||
if my $dir = pick_direction( @maze, $current ) { |
|||
@stack.push: $current; |
|||
move( @maze, |
$current = move( @maze, $dir, $current ); |
||
} |
|||
else { |
|||
last unless @stack; |
|||
$current = @stack.pop; |
|||
} |
|||
⚫ | |||
} |
} |
||
return |
return @maze; |
||
} |
} |
||
sub |
sub pick_direction(@maze, [$x,$y]) { |
||
my |
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]; |
|||
(Right if @maze[$y][$x + 2]); |
|||
@neighbors. |
@neighbors.pick or DeadEnd; |
||
return |@neighbors.roll(1) if @neighbors; |
|||
} |
} |
||
sub move ( |
sub move (@maze, $dir, @cur) { |
||
$ |
my ($x,$y) = @cur; |
||
given |
given $dir { |
||
when |
when Up { --$y; @maze[$y][$x] = 0; @maze[$y][$x-1] -= 2; @maze[$y][$x+1] -= 8; --$y; } |
||
when $ |
when Down { ++$y; @maze[$y][$x] = 0; @maze[$y][$x-1] -= 2; @maze[$y][$x+1] -= 8; ++$y; } |
||
when |
when Left { --$x; @maze[$y][$x] = 0; @maze[$y-1][$x] -= 4; @maze[$y+1][$x] -= 1; --$x; } |
||
when |
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 ( |
sub display (@maze) { |
||
⚫ | |||
say '+' ~ ('---' xx $walls{'y'}[0]).join('+') ~ '+'; |
|||
for @y -> $w, $c { |
|||
print @code[$w]; |
|||
say ~$walls{'y'}[$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}}== |