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

Download:
Game-Battleship-0.0503.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 0.0503   Source  

NAME ^

Game::Battleship - "You sunk my battleship!"

SYNOPSIS ^

  use Game::Battleship;
  my $g = Game::Battleship->new(qw( Gene Aaron ));
  $g->add_player('Alisa');
  my $winner = $g->play();
  print $winner->name(), " wins!\n";

DESCRIPTION ^

A Game::Battleship object represents a battleship game between players. Each has a fleet of vessels and operates with a pair of playing grids One is for their own fleet and one for where the enemy has been seen.

Everything is an object with default but mutable attributes. This way games can have two or more players each with a single fleet of custom vessels. These vessels are pretty simple and standard right now...

A game can be played with the handy play() method or for finer control, use individual methods of the Game::Battleship::* modules. See the distribution test script for working code examples.

METHODS ^

new()

  $g = Game::Battleship->new;
  $g = Game::Battleship->new(
      $name_1,
      $player_object,
      { name => $name_2,
        fleet => \@fleet,
        dimensions => [ $width, $height ], },
  );

Construct a new Game::Battleship object.

The players can be given as scalars, a Game::Battleship::Player object or as a hash reference containing meaningful object attributes.

If not given explicitly, 'player_1' and 'player_2' are used as the player names and two 10x10 grids with 5 predetermined ships are setup by default for each.

See Game::Battleship::Player for details on the default and custom settings.

game_type()

add_player()

  $g->add_player;
  $g->add_player($name);
  $g->add_player({
      name => $name,
      fleet => \@fleet,
      dimensions => [$w, $h],
  });
  $g->add_player($player, $number);

Add a player to the existing game.

This method can accept either nothing, a string, a Game::Battleship::Player object or a hash reference of meaningful Game::Battleship::Player attributes.

This method also accepts an optional numeric second argument that is the player number.

If this number is not provided, the least whole number that is not represented in the player IDs is used. If a player already exists with that number, a warning is emitted and the player is not added..

player()

  $player_obj = $g->player($name);
  $player_obj = $g->player($number);
  $player_obj = $g->player($key);

Return the Game::Battle::Player object that matches the given name, key or number (where the key is /player_\d+/ and the number is just the numeric part of the key).

players()

play()

  $winner = $g->play;

Take a turn for each player, striking all the opponents, until there is only one player left alive.

Return the Game::Battleship::Player object that is the game winner.

TO DO ^

Implement the "number of shots" measure. This may be based on life remaining, shots taken, hits made or ships sunk (etc?).

Make the play method output the player grids for each turn.

Keep pending games and personal scores in a couple handy text files.

Make an eg/simple program with text and then one with colored text.

Implement game type and then allow network play.

Make an eg/cgi program both as text and with Imager.

Make standalone GUI programs too...

Enhance to include these features: sonar imaging from your submarine. 2 Exocet missiles fired from your aircraft carrier. 1 Tomahawk missile fired from your battleship. 2 Apache missile fired from your destroyer. 2 torpedoes fired from your submarine. 2 recon airplanes for surveillance.

This all just means implementing weapon and recon classes with name, quantity and footprint.

SEE ALSO ^

* The code in the t/ directory.

* Game::Battleship::Craft, Game::Battleship::Grid, Game::Battleship::Player

* http://en.wikipedia.org/wiki/Battleship_%28game%29

AUTHOR ^

Gene Boggs <gene@cpan.org>

COPYRIGHT ^

Copyright 2003-2012, Gene Boggs

LICENSE ^

This software is free to use for non-commercial, personal purposes.

syntax highlighting: