| 1 | package Games::Maze::FirstPerson; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Games::Maze; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use constant MOVE_NORTH => -2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use constant MOVE_SOUTH => 2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use constant MOVE_WEST => -2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use constant MOVE_EAST => 2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use constant NORTH_WALL => -1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use constant SOUTH_WALL => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use constant WEST_WALL => -1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use constant EAST_WALL => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 NAME | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | Games::Maze::FirstPerson - First person viewpoint of Games::Maze | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 VERSION | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | Version 0.03 | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =cut | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 3 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | use Games::Maze::FirstPerson; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | my $maze = Games::Maze::FirstPerson->new(); | ||||||||||||||||||||||||||||
| 34 | if ( $maze->south ) { | ||||||||||||||||||||||||||||
| 35 | $maze->go_south; | ||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | print $maze->to_ascii if $maze->has_won; | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | This module is merely a wrapper around C<Games::Maze>. I needed a simple maze | ||||||||||||||||||||||||||||
| 42 | module which would represent a maze from a first-person viewpoint but nothing | ||||||||||||||||||||||||||||
| 43 | on the CPAN did that, hence this code. | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | Patches welcome. | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =head1 EXPORT | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | None. | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =head1 METHODS | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =head2 new | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | my $maze = Games::Maze::FirstPerson->new(@arguments); | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | This constructor takes the same arguments as C<Games::Maze>. Currently we only | ||||||||||||||||||||||||||||
| 58 | support 2D rectangular mazes. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =cut | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 4 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 4 | my $class = shift; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 4 | my %attr_for = @_; | |||||||||||||||||||||||||||
| 66 | 4 | ▶ | if ( exists $attr_for{cell} && 'Quad' ne $attr_for{cell} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $attr_for{"cell"} and "Quad" ne $attr_for{"cell"}
MC/DC50%1/2
exists $attr_for{"cell"} and "Quad" ne $attr_for{"cell"}
exists $attr_for{"cell"}
"Quad" ne $attr_for{"cell"}
Truth table66%2/3
A and B
A= exists $attr_for{"cell"}B= "Quad" ne $attr_for{"cell"}
| |||||||||||||||||||||||||||||
| 67 | 1 | die "'cell' attribute must be 'Quad'"; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | 3 | ▶ | if ( defined( my $dimensions = $attr_for{dimensions} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 3 | ▶ | die "dimensions must be an array ref" unless 'ARRAY' eq ref $dimensions; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 2 | ▶ | die "multi-level mazes not (yet) supported" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@$dimensions > 2 and $dimensions->[2] > 1
MC/DC50%1/2
@$dimensions > 2 and $dimensions->[2] > 1
@$dimensions > 2
$dimensions->[2] > 1
Truth table66%2/3
A and B
A= @$dimensions > 2B= $dimensions->[2] > 1
| |||||||||||||||||||||||||||||
| 72 | if @$dimensions > 2 && $dimensions->[2] > 1; | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 1 | my $maze = Games::Maze->new(@_); | |||||||||||||||||||||||||||
| 76 | 1 | $maze->make; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | # these gymnastics make maneuvering through the maze really, | ||||||||||||||||||||||||||||
| 79 | # really easy. | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | my @grid = | ||||||||||||||||||||||||||||
| 82 | map { | ||||||||||||||||||||||||||||
| 83 | 1 | s/\s+$//; | |||||||||||||||||||||||||||
| 84 | 11 | s/ /0/g; | |||||||||||||||||||||||||||
| 85 | 11 | [ _tighten( split '', $_ ) ] | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | split "\n", $maze->to_ascii; | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 1 | my $east_west; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # find the opening and close it | ||||||||||||||||||||||||||||
| 92 | 1 | foreach my $i ( 0 .. $#{ $grid[0] } ) { | |||||||||||||||||||||||||||
| 93 | 6 | ▶ | if ( $grid[0][$i] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 1 | $grid[0][$i] = 0; | |||||||||||||||||||||||||||
| 95 | 1 | $east_west = $i; | |||||||||||||||||||||||||||
| 96 | 1 | last; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | bless { | ||||||||||||||||||||||||||||
| 101 | maze => $maze, | ||||||||||||||||||||||||||||
| 102 | grid => \@grid, | ||||||||||||||||||||||||||||
| 103 | has_won => 0, | ||||||||||||||||||||||||||||
| 104 | facing => 'south', | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | east_west => $east_west, # X coordinates | ||||||||||||||||||||||||||||
| 107 | north_south => 1, # Y coordinates | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | cols => ( @grid - 1 ) / 2, | ||||||||||||||||||||||||||||
| 110 | 1 | rows => ( @{ $grid[0] } - 1 ) / 2, | |||||||||||||||||||||||||||
| 111 | } => $class; | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 11 | ▶ | sub _tighten { | ||||||||||||||||||||||||||
|
Subroutine
_tighten: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 115 | 11 | my @list = @_; | |||||||||||||||||||||||||||
| 116 | 11 | my @new_list; | |||||||||||||||||||||||||||
| 117 | 11 | ▶ | for ( my $i = 0 ; $i < @list ; $i += 3 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 44 | ▶ | push @new_list, map { $_ ? 0 : 1 } @list[ $i, $i + 1 ]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | 11 | pop @new_list; # get rid of the undef at the end | |||||||||||||||||||||||||||
| 121 | 11 | @new_list; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | ############################################################################## | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =head2 to_ascii | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | print $maze->to_ascii; | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | This method returns an ascii representation of the maze constructed with | ||||||||||||||||||||||||||||
| 131 | periods and spaces. It is not the same as the C<Games::Maze> representation. | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =cut | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 3 | my $WALLS = qr/[-:|]/; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | sub to_ascii { | ||||||||||||||||||||||||||
|
Subroutine
to_ascii: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 138 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 139 | 0 | my $maze = $self->{maze}; | |||||||||||||||||||||||||||
| 140 | 0 | my ( @ascii, $ascii ); | |||||||||||||||||||||||||||
| 141 | 0 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 0 | @ascii = $maze->to_ascii; | |||||||||||||||||||||||||||
| 143 | 0 | return map { s/$WALLS/./g; $_ } @ascii; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | else { | ||||||||||||||||||||||||||||
| 146 | 0 | $ascii = $maze->to_ascii; | |||||||||||||||||||||||||||
| 147 | 0 | $ascii =~ s/$WALLS/./g; | |||||||||||||||||||||||||||
| 148 | 0 | return $ascii; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | ############################################################################## | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =head2 location | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | $maze->location($x, $y); | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | Set the C<X> and C<Y> location in the maze. | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =cut | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 5 | ▶ | sub location { | ||||||||||||||||||||||||||
|
Subroutine
location: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 163 | 5 | my ( $self, $x, $y ) = @_; | |||||||||||||||||||||||||||
| 164 | 5 | ▶ | if ( grep { !defined || !/^\d+/ } ( $x, $y ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $_ && /^\d+/
MC/DC50%1/2
defined $_ && /^\d+/
defined $_
/^\d+/
Truth table66%2/3
A && B
A= defined $_B= /^\d+/
| |||||||||||||||||||||||||||||
| 165 | 2 | die "Arguments to location must be positive integers"; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | 3 | ▶ | if ( $x > $self->{cols} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 1 | die "x value out of range"; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | 2 | ▶ | if ( $y > $self->{rows} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 1 | die "y value out of range"; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | 1 | $_ = ( $_ * 2 ) + 1 foreach $x, $y; | |||||||||||||||||||||||||||
| 174 | 1 | $self->{east_west} = $x; | |||||||||||||||||||||||||||
| 175 | 1 | $self->{north_south} = $y; | |||||||||||||||||||||||||||
| 176 | 1 | return $self; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | ############################################################################## | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =head2 x | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | my $x = $maze->x; | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | Returns the current C<X> location in the maze. | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =cut | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 1 | ▶ | sub x { ( $_[0]{east_west} - 1 ) / 2 } | ||||||||||||||||||||||||||
|
Subroutine
x: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | ############################################################################## | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =head2 y | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | my $y = $maze->y; | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | Returns the current C<Y> location in the maze. | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =cut | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 1 | ▶ | sub y { ( $_[0]{north_south} - 1 ) / 2 } | ||||||||||||||||||||||||||
|
Subroutine
y: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | ############################################################################## | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =head2 rows | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | my $rows = $maze->rows; | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | Returns the number of rows of the maze. | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | =cut | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 1 | ▶ | sub rows { $_[0]{rows} } | ||||||||||||||||||||||||||
|
Subroutine
rows: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | ############################################################################## | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | =head2 cols | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | my $columns = $maze->cols; | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | Returns the number of columns of the maze. | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | =cut | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 1 | ▶ | sub cols { $_[0]{cols} } | ||||||||||||||||||||||||||
|
Subroutine
cols: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | ############################################################################## | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | =head2 columns | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | Same as C<< $maze->cols >>. | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | =cut | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 1 | ▶ | sub columns { $_[0]{cols} } | ||||||||||||||||||||||||||
|
Subroutine
columns: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | ############################################################################## | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | =head2 north | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | if ( $maze->north ) { ... } | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | Returns true if there is an opening to the north of the current position. | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | =cut | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 4 | ▶ | sub north { | ||||||||||||||||||||||||||
|
Subroutine
north: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 248 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 249 | return $self->{grid}[ $self->{north_south} + NORTH_WALL ] | ||||||||||||||||||||||||||||
| 250 | 4 | [ $self->{east_west} ]; | |||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | ############################################################################## | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | =head2 go_north | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | $maze->go_north; | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | Moves one space to the north. Returns false if you cannot go that way. | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | =cut | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | 1 | ▶ | sub go_north { | ||||||||||||||||||||||||||
|
Subroutine
go_north: called CC=2 SCAR=10.1
Pod
documented
| |||||||||||||||||||||||||||||
| 264 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 265 | 1 | ▶ | return unless $self->north; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 266 | 0 | $self->{facing} = 'north'; | |||||||||||||||||||||||||||
| 267 | 0 | $self->{north_south} += MOVE_NORTH; | |||||||||||||||||||||||||||
| 268 | 0 | return $self; | |||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | ############################################################################## | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | =head2 south | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | if ( $maze->south ) { ... } | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | Returns true if there is an opening to the south of the current position. | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | =cut | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | 8 | ▶ | sub south { | ||||||||||||||||||||||||||
|
Subroutine
south: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 282 | 8 | my $self = shift; | |||||||||||||||||||||||||||
| 283 | return $self->{grid}[ $self->{north_south} + SOUTH_WALL ] | ||||||||||||||||||||||||||||
| 284 | 8 | [ $self->{east_west} ]; | |||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | ############################################################################## | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | =head2 go_south | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | $maze->go_south; | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | Moves one space to the south. Returns false if you cannot go that way. | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | =cut | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | 6 | ▶ | sub go_south { | ||||||||||||||||||||||||||
|
Subroutine
go_south: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 298 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 299 | 6 | ▶ | return unless $self->south; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 300 | 5 | $self->{facing} = 'south'; | |||||||||||||||||||||||||||
| 301 | 5 | $self->{north_south} += MOVE_SOUTH; | |||||||||||||||||||||||||||
| 302 | 5 | ▶ | $self->{has_won} = 1 if $self->{north_south} >= @{ $self->{grid} }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 303 | 5 | return $self; | |||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | ############################################################################## | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | =head2 west | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | if ( $maze->west ) { ... } | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | Returns true if there is an opening to the west of the current position. | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | =cut | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | 6 | ▶ | sub west { | ||||||||||||||||||||||||||
|
Subroutine
west: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 317 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 318 | return $self->{grid}[ $self->{north_south} ] | ||||||||||||||||||||||||||||
| 319 | 6 | [ $self->{east_west} + WEST_WALL ]; | |||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | ############################################################################## | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | =head2 go_west | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | $maze->go_west; | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | Moves one space to the west. Returns false if you cannot go that way. | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | =cut | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | 4 | ▶ | sub go_west { | ||||||||||||||||||||||||||
|
Subroutine
go_west: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 333 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 334 | 4 | ▶ | return unless $self->west; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 335 | 3 | $self->{facing} = 'west'; | |||||||||||||||||||||||||||
| 336 | 3 | $self->{east_west} += MOVE_WEST; | |||||||||||||||||||||||||||
| 337 | 3 | return $self; | |||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | ############################################################################## | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | =head2 east | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | if ( $maze->east ) { ... } | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | Returns true if there is an opening to the east of the current position. | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | =cut | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | 5 | ▶ | sub east { | ||||||||||||||||||||||||||
|
Subroutine
east: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 351 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 352 | return $self->{grid}[ $self->{north_south} ] | ||||||||||||||||||||||||||||
| 353 | 5 | [ $self->{east_west} + EAST_WALL ]; | |||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | ############################################################################## | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | =head2 go_east | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | $maze->go_east; | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | Moves one space to the east. Returns false if you cannot go that way. | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | =cut | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | 3 | ▶ | sub go_east { | ||||||||||||||||||||||||||
|
Subroutine
go_east: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 367 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 368 | 3 | ▶ | return unless $self->east; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 369 | 3 | $self->{facing} = 'east'; | |||||||||||||||||||||||||||
| 370 | 3 | $self->{east_west} += MOVE_EAST; | |||||||||||||||||||||||||||
| 371 | 3 | return $self; | |||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | ############################################################################## | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | =head2 surroundings | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | print $maze->surroundings; | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | Prints an ascii representation of the immediate surroundings. For example, if | ||||||||||||||||||||||||||||
| 381 | there are exits to the north and east, it will look like this: | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | . . | ||||||||||||||||||||||||||||
| 384 | . | ||||||||||||||||||||||||||||
| 385 | ... | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | =cut | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | 3 | ▶ | sub surroundings { | ||||||||||||||||||||||||||
|
Subroutine
surroundings: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 390 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 391 | 3 | my $surroundings = ''; | |||||||||||||||||||||||||||
| 392 | 3 | for my $y ( -1 .. 1 ) { | |||||||||||||||||||||||||||
| 393 | 9 | for my $x ( -1 .. 1 ) { | |||||||||||||||||||||||||||
| 394 | $surroundings .= $self->{grid}[ $self->{north_south} + $y ] | ||||||||||||||||||||||||||||
| 395 | 27 | ▶ | [ $self->{east_west} + $x ] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 396 | ? ' ' | ||||||||||||||||||||||||||||
| 397 | : '.'; | ||||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||
| 399 | 9 | $surroundings .= "\n"; | |||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||
| 401 | 3 | return $surroundings; | |||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | ############################################################################## | ||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | =head2 directions | ||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | my @directions = $maze->directions; | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | Returns a list of directions in which you can currently move. Directions are | ||||||||||||||||||||||||||||
| 411 | in lower-case and in the order "north", "south", "east" and "west". | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | =cut | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | 1 | ▶ | sub directions { | ||||||||||||||||||||||||||
|
Subroutine
directions: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 416 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 417 | 1 | return grep { $self->$_ } qw/north south east west/; | |||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | ############################################################################## | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | =head2 has_won | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | if ($maze->has_won) { ... } | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | Returns true if you have reached the exit. | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | =cut | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | 2 | ▶ | sub has_won { $_[0]{has_won} } | ||||||||||||||||||||||||||
|
Subroutine
has_won: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | ############################################################################## | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | =head2 facing | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | my $facing = $maze->facing; | ||||||||||||||||||||||||||||
| 437 | print "You are currently facing $facing\n"; | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | This method returns the direction you are currently facing as determined by | ||||||||||||||||||||||||||||
| 440 | the last direction you have moved. When a maze if first created, you are | ||||||||||||||||||||||||||||
| 441 | facing south. | ||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | =cut | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | 3 | ▶ | sub facing { $_[0]{facing} } | ||||||||||||||||||||||||||
|
Subroutine
facing: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | =head1 EXAMPLE | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | The following simple program will print out the surroundings of the location | ||||||||||||||||||||||||||||
| 450 | the person is currently at and allow them to move through the maze until they | ||||||||||||||||||||||||||||
| 451 | reach the end. It is also included in the C<examples/> directory of | ||||||||||||||||||||||||||||
| 452 | this distribution. | ||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | #!/usr/bin/perl | ||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | use strict; | ||||||||||||||||||||||||||||
| 457 | use warnings; | ||||||||||||||||||||||||||||
| 458 | use Term::ReadKey; | ||||||||||||||||||||||||||||
| 459 | use Games::Maze::FirstPerson; | ||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | my $rows = 5; | ||||||||||||||||||||||||||||
| 462 | my $columns = 8; | ||||||||||||||||||||||||||||
| 463 | my $maze = Games::Maze::FirstPerson->new( | ||||||||||||||||||||||||||||
| 464 | dimensions => [$rows,$columns] | ||||||||||||||||||||||||||||
| 465 | ); | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | print <<"END_CONTROLS"; | ||||||||||||||||||||||||||||
| 468 | q = quit | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | w = move north | ||||||||||||||||||||||||||||
| 471 | a = move west | ||||||||||||||||||||||||||||
| 472 | s = move south | ||||||||||||||||||||||||||||
| 473 | d = move east | ||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | END_CONTROLS | ||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | ReadMode 'cbreak'; | ||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||
| 479 | my %move_for = ( | ||||||||||||||||||||||||||||
| 480 | w => 'go_north', | ||||||||||||||||||||||||||||
| 481 | a => 'go_west', | ||||||||||||||||||||||||||||
| 482 | s => 'go_south', | ||||||||||||||||||||||||||||
| 483 | d => 'go_east' | ||||||||||||||||||||||||||||
| 484 | ); | ||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | while ( ! $maze->has_won ) { | ||||||||||||||||||||||||||||
| 487 | print $maze->surroundings; | ||||||||||||||||||||||||||||
| 488 | my $key = lc ReadKey(0); | ||||||||||||||||||||||||||||
| 489 | if ( 'q' eq $key ) { | ||||||||||||||||||||||||||||
| 490 | print "OK. Quitting\n"; | ||||||||||||||||||||||||||||
| 491 | exit; | ||||||||||||||||||||||||||||
| 492 | } | ||||||||||||||||||||||||||||
| 493 | if ( my $action = $move_for{$key} ) { | ||||||||||||||||||||||||||||
| 494 | unless ( $maze->$action ) { | ||||||||||||||||||||||||||||
| 495 | print "You can't go that direction\n\n"; | ||||||||||||||||||||||||||||
| 496 | } | ||||||||||||||||||||||||||||
| 497 | else { | ||||||||||||||||||||||||||||
| 498 | print "\n"; | ||||||||||||||||||||||||||||
| 499 | } | ||||||||||||||||||||||||||||
| 500 | } | ||||||||||||||||||||||||||||
| 501 | else { | ||||||||||||||||||||||||||||
| 502 | print "I don't understand\n\n"; | ||||||||||||||||||||||||||||
| 503 | } | ||||||||||||||||||||||||||||
| 504 | } | ||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||
| 506 | print "Congratulations! You found the exit!\n"; | ||||||||||||||||||||||||||||
| 507 | print $maze->to_ascii; | ||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||
| 509 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||
| 511 | Curtis "Ovid" Poe, C<< <moc.oohay@eop_divo_sitruc> >> | ||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||
| 513 | =head1 BUGS | ||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||
| 515 | Please report any bugs or feature requests to | ||||||||||||||||||||||||||||
| 516 | C<bug-games-maze-firstperson@rt.cpan.org>, or through the web interface at | ||||||||||||||||||||||||||||
| 517 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Games-Maze-FirstPerson>. | ||||||||||||||||||||||||||||
| 518 | I will be notified, and then you'll automatically be notified of progress on | ||||||||||||||||||||||||||||
| 519 | your bug as I make changes. | ||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||
| 521 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||
| 523 | See John Gamble's L<Games::Maze>. | ||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||
| 525 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||
| 527 | Copyright 2005 Curtis "Ovid" Poe, all rights reserved. | ||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||
| 529 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 530 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||
| 532 | =cut | ||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||
| 534 | 3 | 1; | |||||||||||||||||||||||||||