Gene Boggs > Game-Battleship-0.0503 > Game::Battleship::Player

Download:
Game-Battleship-0.0503.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 0.0502   Source  

NAME ^

Game::Battleship::Player - A Battleship player class

SYNOPSIS ^

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

DESCRIPTION ^

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

PUBLIC METHODS ^

new %ARGUMENTS
  $player => Game::Battleship::Player->new(
      name  => 'Aaron',
      fleet => \@fleet,
      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 Game::Battleship::Craft objects.

    If not explicitly provided, the standard fleet (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 ten by ten grid is used.

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 appropriate 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 Game::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 Game::Battleship::Grid object attribute named for the opponent) at the given coordinate, the opponent's "craft grid" is updated by lower-casing the Game::Battleship::Craft object id at the given coordinate, the opponent Game::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 Game::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 ^

Game::Battleship, Game::Battleship::Craft, Game::Battleship::Grid

AUTHOR ^

Gene Boggs <gene@cpan.org>

COPYRIGHT AND LICENSE ^

See Game::Battleship.

syntax highlighting: