Games::Battleship::Craft - A Battleship craft class


Games-Battleship documentation Contained in the Games-Battleship distribution.

Index


Code Index:

NAME

Top

Games::Battleship::Craft - A Battleship craft class

SYNOPSIS

Top

  use Games::Battleship::Craft;

  my $craft = Games::Battleship::Craft->new(
      id => 'T',
      name => 'tug boat',
      points => 1,
  )

  $points_remaining = $craft->hit;

DESCRIPTION

Top

A Games::Battleship::Craft object represents the profile of a Battleship

PUBLIC METHODS

Top

new %ARGUMENTS

* id => $STRING

A scalar identifier to use to indicate position on the grid. If one is not provided, the uppercased first name character will be used by default.

Currently, it is required that this be a single uppercase letter (the first letter of the craft name, probably), since a hit will be indicated by "lowercasing" this mark on a player grid.

* name => $STRING

A required attribute provided to give the craft a name.

* points => $NUMBER

An attribute used to define the line segment span on the playing grid.

* position => [$X, $Y]

The position of the craft bow ("nose") on the grid.

Currently, the craft is assumed to have a horizontal or vertical alignment. Soon there will be diagonal positioning...

hit
  $points_remaining = $craft->hit;

Increment the craft's hit attribute value and return what's left of the craft (total point value minus the number of hits).

TO DO

Top

Have different numbers of different weapons.

Allow a craft to have a width.

Allow diagonal positions too. Why not?

AUTHOR

Top

Gene Boggs <gene@cpan.org>

COPYRIGHT AND LICENSE

Top


Games-Battleship documentation Contained in the Games-Battleship distribution.

# $Id: Craft.pm,v 1.12 2004/08/25 05:35:33 gene Exp $

package Games::Battleship::Craft;
$VERSION = 0.03;
use strict;
use warnings;
use Carp;

sub new {
    my ($proto, %args) = @_;
    my $class = ref ($proto) || $proto;

    # The name is a required attribute.
    croak "Craft name not provided.\n" unless defined $args{name};

    my $self = {
        # Who am I?
        id   => $args{id},
        name => $args{name},
        # Where's my bow (craft nose)?
        position => $args{position} || undef,
        # How much am I worth?
        points => $args{points} || undef,
        # How many times have I been hit?
        hits => $args{hits} || 0,
    };

    # Default the id to the uppercased first char of name.
    $self->{id} = ucfirst substr $self->{name}, 0, 1
        unless defined $self->{id};

    bless $self, $class;
    return $self;
}

sub hit {
    my $self = shift;
    # Tally the hit.
    $self->{hits}++;
    # Hand back the remainder of the craft's value.
    return $self->{points} - $self->{hits};
}

1;

__END__