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.


In reply to Re^2: Writing a random dungeon generator in perl. by xdg
in thread Writing a random dungeon generator in perl. by Trag

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.