NAME

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

SYNOPSIS

use Games::Battleship;

$g = Games::Battleship->new('Gene', 'Aeryk');

$player_obj = $g->add_player('Stephanie');
$player_obj = $g->player('Stephanie');

$winner = $g->play;
print "$winner->{name} wins!\n";

ABSTRACT

Battleship game implementation

DESCRIPTION

A Games::Battleship object represents a battleship game with players, fleets and playing grids.

No, I did not do this for a school assignment, but rather because I played it one night with my friend, Aeryk, and decided that it might be fun to implement.

NOTE: Currently, this module's play feature is not especially functional for the sole reason that the game type attribute does not exist yet. Please bear with me. An upcoming release will rock.

The game can definitely be played with by using the individual methods in the Games::Battleship* modules.

Please see the distribution test script for some 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.

Please see Games::Battleship::Player for details on the default settings.

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

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 the dimensions attribute.

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

* I have not determined the most appropriate functionality for the gui type, yet. There are many many GUIs out there...

* 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.

add_player [$PLAYER] [, $NUMBER]
$g->add_player;
$g->add_player($player);
$g->add_player($player, $number);
$g->add_player({
    $player => {
        fleet => \@crafts,
        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

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.