NAME
Game::Battleship::Player
VERSION
version 0.0602
SYNOPSIS
use Game::Battleship::Player;
my $aaron = Game::Battleship::Player->new(name => 'Aaron');
my $gene = Game::Battleship::Player->new(name => 'Gene');
print 'Player 1: ', $aaron->name, "\n",
'Player 2: ', $gene->name, "\n";
$aaron->strike($gene, 0, 0);
# Repeat and get a duplicate strike warning.
my $strike = $aaron->strike($gene, 0, 0);
print $aaron->grid($gene), "\nThat was a " .
( $strike == 1 ? 'hit!'
: $strike == 0 ? 'miss.'
: 'duplicate?' ), "\n";
my $craft_obj = $aaron->craft($id);
DESCRIPTION
A Game::Battleship::Player
object represents a Battleship player complete with fleet and game surface.
PUBLIC METHODS
- new
-
$player => Game::Battleship::Player->new( name => 'Aaron', fleet => \@fleet, dimensions => [$x, $y], );
id => $STRING
Computed
grid => $STRING
Computed
life => $STRING
Computed
id => $STRING
Computed
name => $STRING
An optional attribute provided to give the player a name.
If not provided, the string "player_1" or "player_2" is used.
fleet => [$CRAFT_1, $CRAFT_2, ... $CRAFT_N]
Array reference of
Game::Battleship::Craft
objects.If not explicitly provided, the standard fleet (with 5 ships) is created by default.
dimensions => [$WIDTH, $HEIGHT]
Array reference with the player's grid height and width values.
If the grid dimensions are not explicitly specified, the standard ten by ten grid is used.
- matrix
-
$grid = $player->matrix; $grid = $player->matrix($enemy);
Return the playing grid as a "flush-left" text matrix like this:
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . S S S . . . . . . . . . . C . . . . . . . . A C . . . . D . . . A C . . B . D . . . A . . . B . . . . . A . . . B . . . . . A . . . B
- strike
-
$strike = $player->strike($enemy, $x, $y);
Strike the enemy at the given coordinate and return a numeric value to indicate success or failure.
The player to strike must be given as a
Game::Battleship::Player
object and the coordinate must be given as a numeric pair.On success, an "x" is placed on the striking player's "opponent map grid" (a
Game::Battleship::Grid
object attribute named for the opponent) at the given coordinate, the opponent's "craft grid" is updated by lower-casing theGame::Battleship::Craft
objectid
at the given coordinate, the opponentGame::Battleship::Craft
objecthits
attribute is incremented, the striking player'sscore
attribute is incremented, and a one (i.e. true) is returned.If an enemy craft is completely destroyed, a happy warning is emitted.
On failure, an "o" is placed on the striking player's "opponent map grid" at the given coordinate and a zero (i.e. false) is returned.
If a player calls for a strike at a coordinate that was already struck, a warning is emitted and a negative one (-1) is returned.
- craft
-
$craft = $player->craft($id); $craft = $player->craft(id => $id); $craft = $player->craft(name => $name);
Return the player's
Game::Battleship::Craft
object that matches the given argument(s).If the last argument is not provided the first argument is assumed to be the
id
attribute.
PRIVATE METHODS
- _is_a_hit
-
Return true or false if another player's strike is successful. That is, return a one if there is a craft at the given coordinate and zero otherwise.
TO DO
Include a weapon argument in the strike
method.
Make the grid
method honor the game type and return something appropriate.
SEE ALSO
Game::Battleship, Game::Battleship::Craft, Game::Battleship::Grid
AUTHOR
Gene Boggs <gene@cpan.org>
COPYRIGHT AND LICENSE
This software is copyright (c) 2020 by Gene Boggs.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.