Games::Maze::FirstPerson - First person viewpoint of Games::Maze
Version 0.03
use Games::Maze::FirstPerson; my $maze = Games::Maze::FirstPerson->new(); if ( $maze->south ) { $maze->go_south; } print $maze->to_ascii if $maze->has_won;
This module is merely a wrapper around Games::Maze. I needed a simple maze module which would represent a maze from a first-person viewpoint but nothing on the CPAN did that, hence this code.
Games::Maze
Patches welcome.
None.
my $maze = Games::Maze::FirstPerson->new(@arguments);
This constructor takes the same arguments as Games::Maze. Currently we only support 2D rectangular mazes.
print $maze->to_ascii;
This method returns an ascii representation of the maze constructed with periods and spaces. It is not the same as the Games::Maze representation.
$maze->location($x, $y);
Set the X and Y location in the maze.
X
Y
my $x = $maze->x;
Returns the current X location in the maze.
my $y = $maze->y;
Returns the current Y location in the maze.
my $rows = $maze->rows;
Returns the number of rows of the maze.
my $columns = $maze->cols;
Returns the number of columns of the maze.
Same as $maze->cols.
$maze->cols
if ( $maze->north ) { ... }
Returns true if there is an opening to the north of the current position.
$maze->go_north;
Moves one space to the north. Returns false if you cannot go that way.
if ( $maze->south ) { ... }
Returns true if there is an opening to the south of the current position.
$maze->go_south;
Moves one space to the south. Returns false if you cannot go that way.
if ( $maze->west ) { ... }
Returns true if there is an opening to the west of the current position.
$maze->go_west;
Moves one space to the west. Returns false if you cannot go that way.
if ( $maze->east ) { ... }
Returns true if there is an opening to the east of the current position.
$maze->go_east;
Moves one space to the east. Returns false if you cannot go that way.
print $maze->surroundings;
Prints an ascii representation of the immediate surroundings. For example, if there are exits to the north and east, it will look like this:
. . . ...
my @directions = $maze->directions;
Returns a list of directions in which you can currently move. Directions are in lower-case and in the order "north", "south", "east" and "west".
if ($maze->has_won) { ... }
Returns true if you have reached the exit.
my $facing = $maze->facing; print "You are currently facing $facing\n";
This method returns the direction you are currently facing as determined by the last direction you have moved. When a maze if first created, you are facing south.
The following simple program will print out the surroundings of the location the person is currently at and allow them to move through the maze until they reach the end. It is also included in the examples/ directory of this distribution.
examples/
#!/usr/bin/perl use strict; use warnings; use Term::ReadKey; use Games::Maze::FirstPerson; my $rows = 5; my $columns = 8; my $maze = Games::Maze::FirstPerson->new( dimensions => [$rows,$columns] ); 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;
Curtis "Ovid" Poe, <moc.oohay@eop_divo_sitruc>
<moc.oohay@eop_divo_sitruc>
Please report any bugs or feature requests to bug-games-maze-firstperson@rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Games-Maze-FirstPerson. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-games-maze-firstperson@rt.cpan.org
See John Gamble's Games::Maze.
Copyright 2005 Curtis "Ovid" Poe, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Games::Maze::FirstPerson, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Games::Maze::FirstPerson
CPAN shell
perl -MCPAN -e shell install Games::Maze::FirstPerson
For more information on module installation, please visit the detailed CPAN module installation guide.