#!/usr/bin/perl
use warnings;
use strict;
#This is supposed to generate a random maze for use in a roguelike gam
+e.
my $wall = '#';
my $floor = '.';
my ($dungeonwidth, $dungeonheight) = (40, 40);
my @dungeon = ($wall) x ($dungeonwidth * $dungeonheight);
#the dungeon is stored as an array. The location directly above or bel
+ow any particular square is found by subtracting the dungeon width or
+ adding it, respectively.
sub generatemap {
my @directions = (-1, 1, -$dungeonwidth, $dungeonwidth);
my @notcurrentloc;
my $var;
my $roomcount = 0;
my $choice;
my $newloc;
my $currentloc = int (rand(@dungeon));
my $specialfloor = shift;
#Eventually this spot will check to see if you're on a special floor.
$dungeon[$currentloc] = $floor;
while($roomcount < $#dungeon/2) {
$choice = (-1, 1, -$dungeonwidth, $dungeonwidth)[rand 4];
$newloc = $currentloc + $choice;
if ($newloc < 0 || $newloc > $#dungeon) {
redo;
}
@notcurrentloc = grep($_ != -$choice, @directions);
if ($dungeon[$newloc] ne $floor && $dungeon[$currentloc] eq $f
+loor) {
foreach $var(@notcurrentloc) {
if ($dungeon[$newloc+$var] eq $floor) {
$currentloc = int (rand(@dungeon));
redo;
}
else {
$currentloc = $newloc;
$dungeon[$currentloc] = $floor;
$roomcount++;
}
}
}
else {
$currentloc = int (rand(@dungeon));
redo;
}
}
}
sub printmap {
my $i;
my $increment = 0;
my $length = $dungeonwidth-1;
for ($i = 0; $i < $dungeonheight; $i++) {
print @dungeon[$increment..$length];
print "\n";
$increment = $increment + $dungeonwidth;
$length = $length + $dungeonwidth;
}
}
generatemap;
printmap;
|