#!/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 rand 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;