in reply to Re: Writing a random dungeon generator in perl.
in thread Writing a random dungeon generator in perl.

To the second part of your question, you'll probably have an easier time with your project if you try some of these tips:

I've revised parts of your example to show what I mean. (Not the most Perlish way to do it, but a step closer that should be clear to see the difference.) I've left out most of your room logic as it wasn't clear what you intended.

#!/usr/bin/perl use strict; use warnings; my ($dungeon_width, $dungeon_height) = (20, 20); my $WALL = '#'; my $ROOM = '.'; my @dungeon = ($WALL) x ( $dungeon_width * $dungeon_height ); + sub generatemap { # clear a starting location my $cur_location = int rand( @dungeon ); # 0 to $#dungeon $dungeon[ $cur_location ] = $ROOM; # clear squares for (about) half the dungeon my $room_count = 1; while ($room_count < @dungeon / 2) { # pick a square adjacent to the current one my $choice = (-1, +1, -$dungeon_width, +$dungeon_width)[int ra +nd 4]; my $new_location = $cur_location + $choice; # pick again if it would take us out of bounds if ($new_location < 0 || $new_location > $#dungeon) { redo; } # clear it if it's not already a room $cur_location = $new_location; if ( $dungeon[ $cur_location ] ne $ROOM ) { $dungeon[ $cur_location ] = $ROOM; $room_count++; } } } sub printmap { for my $row (0 .. $dungeon_height - 1) { my $first_col = $row * $dungeon_width; my $last_col = $first_col + $dungeon_width - 1; print @dungeon[ $first_col .. $last_col ], "\n"; } } generatemap; printmap;

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.