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


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');
  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);


DESCRIPTION

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


PUBLIC METHODS

new %ARGUMENTS
  $player => Games::Battleship::Player->new(
      name  => 'Aeryk',
      fleet => \@fleet,
      dimensions => [$x, $y],
  );
grid
  $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, @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 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 $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, return a one if there is a craft at the given coordinate and zero otherwise.


TO DO

Include a weapon argument in the strike method.

Make the grid method honor the game type and return something appropriate.


SEE ALSO

the Games::Battleship manpage

the Games::Battleship::Craft manpage

the Games::Battleship::Grid manpage


AUTHOR

Gene Boggs <gene@cpan.org>


COPYRIGHT AND LICENSE

See the Games::Battleship manpage.

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