Trag has asked for the wisdom of the Perl Monks concerning the following question:
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;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Writing a random dungeon generator in perl.
by xdg (Monsignor) on Aug 15, 2005 at 21:51 UTC | |
by xdg (Monsignor) on Aug 15, 2005 at 22:47 UTC | |
Re: Writing a random dungeon generator in perl.
by displeaser (Hermit) on Aug 16, 2005 at 10:05 UTC | |
by extremely (Priest) on Aug 16, 2005 at 14:23 UTC | |
Re: Writing a random dungeon generator in perl.
by kirbyk (Friar) on Aug 15, 2005 at 21:51 UTC | |
Re: Writing a random dungeon generator in perl.
by Trag (Monk) on Aug 15, 2005 at 22:48 UTC | |
by xdg (Monsignor) on Aug 16, 2005 at 08:14 UTC | |
Re: Writing a random dungeon generator in perl.
by GrandFather (Saint) on Aug 17, 2005 at 11:55 UTC |
Back to
Seekers of Perl Wisdom