I'm trying to write a roguelike in perl, but I'm having a bit of trouble with the random dungeon generator (which is all I have done so far). I've written the following code. As a test, it's supposed to output a maze, with the walls represented as #, and the floors denoted by periods. The maze is stored in the @dungeon array, and is 80 x 80 tiles. Anything directly above any particular tile can be found by looking 80 tiles either ahead or behind in the array. Anything to the left or right can be found by looking 1 ahead or behind. When I run it at the moment, I get the following error constantly repeated: "Argument "#" isn't numeric in addition (+) at C:\mygame\mapgenerator.pl line 22." That's strange, because no addition is taking place on line 22 (if  ($start < 0 || $start > 6400) {). I'd appreciate help with that error, but the real reason I'm posting this is because I'm sure there's a better way to implement the thing, and I'd appreciate some tips. Thanks!
#!/usr/bin/perl use warnings; use strict; #package mapgenerator; my @dungeon = ('#') x 6400; sub generatemap { my $count; my $choice; my $increment = 0; my $notdoneyet = 1; my $start = int (rand(6399)); #chooses a random point to st +art building the maze my $specialfloor = shift; $dungeon[$start] = '.'; while($notdoneyet) { $choice = (-1, +1, -80, +80)[rand 4]; $start = $start + $choice; if ($start < 0 || $start > 6400) { redo; } elsif ($dungeon[$start] eq '#' && $dungeon[$start] + 1 eq '#' && $dungeon[$start] - 1 eq '#' && $dungeon[$start] + 80 eq '#' + && $dungeon[$start] - 80 eq '#'){ $dungeon[$start] = '.'; } else { $increment++; if ($increment == 10) { $start = int (rand(6399)); $increment = 0; } } $count = grep m/./, @dungeon; if ($count == 3200) { $notdoneyet = 0; } } } sub printmap { my $first = 0; my $second = 80; my $totalprinted = 0; while ($totalprinted < 6400) { print $dungeon[$first..$second]; print "\n"; $first = $first + 80; $second = $second + 80; $totalprinted = $totalprinted + 80; } } generatemap; printmap;

In reply to 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.