NAME
Games::Battleship::Player - A Battleship player class
SYNOPSIS
use Games::Battleship::Player;
$aeryk => Games::Battleship::Player->new(name => 'Aeryk');
$gene => Games::Battleship::Player->new(name => 'Gene');
$strike = $aeryk->strike($gene, 0, 0);
print $aeryk->grid($gene),
($strike == 1
? 'Hit!'
: $strike == 0
? 'Missed.'
: 'duplicate');
# Repeat strike and get a warning.
$aeryk->strike($gene, 0, 0);
# This method is handy, but only used in the strike method.
$craft_obj = $aeryk->craft($id);
ABSTRACT
A Battleship player class
DESCRIPTION
A Games::Battleship::Player
object represents a Battleship player class, complete with fleet and game surface.
PUBLIC METHODS
- new %ARGUMENTS
-
$player => Games::Battleship::Player->new( name => 'Aeryk', fleet => \@crafts, dimensions => [$x, $y], );
name => $STRING
An optional attribute provided to give the player a name.
If not provided, the string "player_1" or "player_2" is used.
fleet => [$CRAFT_1, $CRAFT_2, ... $CRAFT_N]
Array reference of
Games::Battleship::Craft
objects.If not explicitely provided, the standard one (with 5 ships) is created by default.
dimensions => [$WIDTH, $HEIGHT]
Array reference with the player's grid height and width values.
If the grid dimensions are not explicitly specified, the standard 10 x 10 grid is used by default.
- grid
-
$grid = $player->grid($enemy);
Return the playing grid as a text matrix like this,
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . S S S . . . . . . . . . . C . . . . . . . . A C . . . . D . . . A C . . B . D . . . A . . . B . . . . . A . . . B . . . . . A . . . B
- strike $PLAYER, @COORDINATE
-
$strike = $player->strike($enemy, $x, $y);
Strike the enemy at the given coordinate and return a numeric value to indicate success or failure.
The player to strike must be given as a
Games::Battleship::Player
object and the coordinate must be given as a numeric pair.On success, an "x" is placed on the striking player's "opponent map grid" (a
Games::Battleship::Grid
object attribute named for the opponent) at the given coordinate, the opponent's "craft grid" is updated by lowercasing theGames::Battleship::Craft
objectid
at the given coordinate, the opponentGames::Battleship::Craft
objecthits
attribute is incremented, the striking player'sscore
attribute is incremented, and a one (i.e. true) is returned.If an enemy craft is completely destroyed, a happy warning is emitted.
On failure, an "o" is placed on the striking player's "opponent map grid" at the given coordinate and a zero (i.e. false) is returned.
If a player calls for a strike at a coordinate that was already struck, a warning is emitted and a negative one (i.e. -1) is returned.
- craft $KEY [, $VALUE]
-
$craft = $player->craft($id); $craft = $player->craft(id => $id); $craft = $player->craft(name => $name);
Return the player's
Games::Battleship::Craft
object that matches the given argument(s).If the last argument is not provided the first argument is assumed to be the
id
attribute.
PRIVATE METHODS
- _is_a_hit @COORDINATE
-
Return true or false if another player's strike is successful. That is, there is a craft at the given coordinate.
TO DO
Include a weapon argument in the strike
method.
SEE ALSO
AUTHOR
Gene Boggs <gene@cpan.org>
COPYRIGHT AND LICENSE
Copyright 2003, Gene Boggs
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.