
Games::Battleship::Player - A Battleship player class

use Games::Battleship::Player;
$aeryk = Games::Battleship::Player->new(name => 'Aeryk');
$gene = Games::Battleship::Player->new(name => 'Gene');
print 'Player 1: ', $aeryk->name, "\n",
'Player 2: ', $gene->name, "\n";
$aeryk->strike($gene, 0, 0);
# Repeat and get a duplicate strike warning.
$strike = $aeryk->strike($gene, 0, 0);
print $aeryk->grid($gene), "\nThat was a " .
( $strike == 1 ? 'hit!'
: $strike == 0 ? 'miss.'
: 'duplicate?' ), "\n";
$craft_obj = $aeryk->craft($id);

A Games::Battleship::Player object represents a Battleship player complete with fleet and game surface.

$player => Games::Battleship::Player->new(
name => 'Aeryk',
fleet => \@fleet,
dimensions => [$x, $y],
);
An optional attribute provided to give the player a name.
If not provided, the string "player_1" or "player_2" is used.
Array reference of Games::Battleship::Craft objects.
If not explicitely provided, the standard fleet (with 5 ships) is created by default.
Array reference with the player's grid height and width values.
If the grid dimensions are not explicitly specified, the standard ten by ten grid is used.
$grid = $player->grid(); $grid = $player->grid($enemy);
Return the playing grid as a "flush-left" text matrix like this:
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . S S S . . . . . . . . . . C . . . . . . . . A C . . . . D . . . A C . . B . D . . . A . . . B . . . . . A . . . B . . . . . A . . . B
Eventually, this method will respect the game type and return an apppropriate representation, such as a PNG file or XML, etc.
$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 the Games::Battleship::Craft object id at the given coordinate, the opponent Games::Battleship::Craft object hits attribute is incremented, the striking player's score 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 (-1) is returned.
$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.

Return true or false if another player's strike is successful. That is, return a one if there is a craft at the given coordinate and zero otherwise.

Include a weapon argument in the strike method.
Make the grid method honor the game type and return something appropriate.


Gene Boggs <gene@cpan.org>

See Games::Battleship.