Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Trag's scratchpad

by Trag (Monk)
on Jun 01, 2004 at 19:04 UTC ( [id://358406]=scratchpad: print w/replies, xml ) Need Help??

#!/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;
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-03-29 10:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found