NAME
Games::Battleship - "You sunk my battleship!"
SYNOPSIS
use Games::Battleship;
$g = Games::Battleship->new(qw( Gene Aeryk ));
$g->add_player('Stephanie');
$winner = $g->play();
print $winner->name(), " wins!\n";
@player_objects = @{ $g->players };
$player_obj = $g->player('Professor Snape');
DESCRIPTION
A Games::Battleship
object represents a battleship game between players. Each has a fleet of vessles 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 vessles. These vessles 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 Games::Battleship::*
modules. See the distribution test script for working code examples.
WHY?
I played this with my friends, Aeryk and Stepanie, and decided that it would be a fun challenge to automate with line segment geometry.
PUBLIC METHODS
- new
-
$g = Games::Battleship->new; $g = Games::Battleship->new( $name_1, $player_object, { name => $name_2, fleet => \@fleet, dimensions => [ $width, $height ], }, );
Construct a new
Games::Battleship
object.The players can be given as scalars, a
Games::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 Games::Battleship::Player for details on the default and custom settings.
- 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
Games::Battleship::Player
object or a hash reference of meaningfulGames::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..
- play
-
$winner = $g->play;
Take a turn for each player, striking all the opponents, until there is only one player left alive.
Return the
Games::Battleship::Player
object that is the game winner. - player
-
$player_obj = $g->player($name); $player_obj = $g->player($number); $player_obj = $g->player($key);
Return the
Games::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).
PRIVATE METHODS
- _get_coordinate
-
($x, $y) = $g->_get_coordinate;
Return a grid position.
Currently this returns a random integer pair with the player grid dimensions as the maximum.
Eventually this method will honor a game type attribute to allow different interfaces such as CGI, Curses, Win32, etc. to get the coordinate from the players interactively.
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 missles fired from your aircraft carrier. 1 tomahawk missle fired from your battleship. 2 apache missles 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
Games::Battleship::Player, Games::Battleship::Craft, Games::Battleship::Grid
AUTHOR
Gene Boggs <gene@cpan.org>
COPYRIGHT
Copyright 2003-2004, Gene Boggs
LICENSE
This software is free to use for non-commercial, personal purposes.