The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Games::Maze::FirstPerson - First person viewpoint of Games::Maze

VERSION

Version 0.03

SYNOPSIS

use Games::Maze::FirstPerson;

my $maze = Games::Maze::FirstPerson->new();
if ( $maze->south ) {
    $maze->go_south;
}
print $maze->to_ascii if $maze->has_won;

DESCRIPTION

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.

Patches welcome.

EXPORT

None.

METHODS

new

my $maze = Games::Maze::FirstPerson->new(@arguments);

This constructor takes the same arguments as Games::Maze. Currently we only support 2D rectangular mazes.

to_ascii

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.

location

$maze->location($x, $y);

Set the X and Y location in the maze.

x

my $x = $maze->x;

Returns the current X location in the maze.

y

my $y = $maze->y;

Returns the current Y location in the maze.

rows

my $rows = $maze->rows;

Returns the number of rows of the maze.

cols

my $columns = $maze->cols;

Returns the number of columns of the maze.

columns

Same as $maze->cols.

north

if ( $maze->north ) { ... }

Returns true if there is an opening to the north of the current position.

go_north

$maze->go_north;

Moves one space to the north. Returns false if you cannot go that way.

south

if ( $maze->south ) { ... }

Returns true if there is an opening to the south of the current position.

go_south

$maze->go_south;

Moves one space to the south. Returns false if you cannot go that way.

west

if ( $maze->west ) { ... }

Returns true if there is an opening to the west of the current position.

go_west

$maze->go_west;

Moves one space to the west. Returns false if you cannot go that way.

east

if ( $maze->east ) { ... }

Returns true if there is an opening to the east of the current position.

go_east

$maze->go_east;

Moves one space to the east. Returns false if you cannot go that way.

surroundings

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:

. .
.
...

directions

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".

has_won

if ($maze->has_won) { ... }

Returns true if you have reached the exit.

facing

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.

EXAMPLE

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.

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

AUTHOR

Curtis "Ovid" Poe, <moc.oohay@eop_divo_sitruc>

BUGS

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.

ACKNOWLEDGEMENTS

See John Gamble's Games::Maze.

COPYRIGHT & LICENSE

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.