Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Ovid (2709)

Ovid
  (email not shown publicly)
http://publius-ovidius.livejournal.com/
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Monday September 05, 2005
04:27 PM

Games::Maze::FirstPerson

[ #26605 ]

What do you do with a three-day weekend? Games, of course! Actually, I wanted to learn a bit of Tk and thought it might be interesting to play around with a maze from a first-person viewpoint. Then I discovered that none of the maze software on the CPAN does quite what I want. Games::Maze::FirstPerson should be available soon.

The following code will print a very small bit of your surroundings and let you navigate through the maze until you find the exit. Once in the exit, it prints the entire maze.

#!/usr/bin/perl

use strict;
use warnings;
use Term::ReadKey;
use Games::Maze::FirstPerson;

my $maze = Games::Maze::FirstPerson->new(dimensions => [3,3]);

print <<"END_CONTROLS";
q = quit

w = move north
a = move west
s = move south
d = move east

END_CONTROLS

ReadMode 'cbreak';

my %move_for = (
    w => 'go_north',
    a => 'go_west',
    s => 'go_south',
    d => 'go_east'
);

while ( ! $maze->has_won ) {
    print $maze->surroundings;
    my $key = lc ReadKey(0);
    if ( 'q' eq $key ) {
        print "OK.  Quitting\n";
        exit;
    }
    if ( my $action = $move_for{$key} ) {
        unless ( $maze->$action ) {
            print "You can't go that direction\n\n";
        }
        else {
            print "\n";
        }
    }
    else {
        print "I don't understand\n\n";
    }
}

print "Congratulations!  You found the exit!\n";
print $maze->to_ascii;

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.