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 Games::Battleship::Player 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.

* game_type 'text' | 'cgi'
$g->game_type($type);
$type = $g->game_type;

* NOTE: Currently, this method is not implemented, so don't get your hopes up just yet. I will add this to an upcoming release, and there will be happiness in the valley.

Specify or retreive the type of game to play. This setting is optional and used by the _get_coordinate method to properly request input of coordinates.

If not set, a random coordinate is chosen based on a player's Games::Battleship::Grid dimension attribute.

For text (e.g. curses), this is an interactive console request. For CGI programs, this is a call to the CGI::param method.

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 of Games::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 a player_* key of the Games::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 (as integers).

TO DO

Allow network play.

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

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

Make a simple eg/ program with text.

Make an eg/ Curses program with colored text.

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

Make an eg/ standalone GUI program too.

Enhance to include the features in Hasbro's "Advanced Mission Game": (2) exocet missles fired from your aircraft carrier; (1) tomahawk missle with a massive footprint fired from your battleship; (2) apache missles fired from your destroyer; (2) torpedoes fired from your sub; (2) recon airplanes for surveillance; sonar imaging from your sub. This means implementing weapon and recon classes with name, quantity, footprint, etc.

SEE ALSO

Games::Battleship::Player

http://www.hasbro.com/pl/page.viewproduct/product_id.9388/dn/default.cfm

CVS

# $Id: Battleship.pm,v 1.17 2004/02/05 09:22:30 gene Exp $

AUTHOR

Gene Boggs <gene@cpan.org>

COPYRIGHT AND LICENSE

Copyright 2003, Gene Boggs

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 237:

Expected text after =item, not a bullet