NAME
Games::Battleship - "You sunk my battleship!"
SYNOPSIS
use Games::Battleship;
$g = Games::Battleship->new('Gene', 'Aeryk');
$g->add_player('Stephanie');
$player_obj = $g->player('Rufus');
@player_objects = @{ $g->players };
$winner = $g->play;
print $winner->name ." wins!\n";
ABSTRACT
Hasbro Battleship game implementation
DESCRIPTION
A Games::Battleship
object represents a battleship game with players, fleets and playing grids.
I played this one night with my friend, Aeryk, and decided that it would be a fun challenge to automate. One of the more elegant challenges turned into the Games::Battleship::Grid::_segment_intersection
function.
Besides the handy play
method, a game can be played with the individual methods in the Games::Battleship::*
modules. See the distribution test script for working code.
PUBLIC METHODS
- new [@PLAYERS]
-
$g = Games::Battleship->new; $g = Games::Battleship->new( $player_name, $player_object, { name => $name, fleet => \@fleet, dimensions => [$w1, $h1], }, );
Construct a new
Games::Battleship
object.The players can be given as a scalar name, 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 the standard game is set up. That is, a 10x10 grid with 5 predetermined ships per player.
See Games::Battleship::Player for details on the default settings.
You can actually play a game with any number of players. Each player can have any size grid (of integer dimension) and any number of "ships" (which can also be made up). These options are all easy to set and are described in the individual
Games::Battleship::*
modules. - add_player [$PLAYER] [, $NUMBER]
-
$g->add_player; $g->add_player($player); $g->add_player($player, $number); $g->add_player({ $player => { fleet => \@fleet, dimensions => [$w, $h], } });
Add a player to the existing game.
This method can accept either nothing, a simple scalar as a name, a
Games::Battleship::Player
object or a hash reference where the key is the player name and the value is a hash reference ofGames::Battleship::Player
attributes.Also, this method 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 for some reason, a player already exists with that number, a fatal error is returned.
- 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 $STRING
-
$player_obj = $g->player($name); $player_obj = $g->player($key); $player_obj = $g->player($number);
Return the
Games::Battle::Player
object that matches the given name, key or number (where the key is aplayer_*
key of theGames::Battleship
instance 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 intager coordinate with the player grid dimensions as the maximum.
Eventually this method will honor a game type attribute to allow different interfaces such as CGI, GTk or Curses, etc.
TO DO
Implement the "number of shots" measure. This may be based on life remaining, shots taken, hits made or ships sunk.
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
CVS
$Id: Battleship.pm,v 1.18 2004/02/07 04:41:07 gene Exp $
AUTHOR
Gene Boggs <gene@cpan.org>
COPYRIGHT
Copyright 2003-2004, Gene Boggs
LICENSE
This software is free to use for non-commercial, personal purposes.