NAME
Games::Battleship::Grid - A Battleship grid class
SYNOPSIS
use Games::Battleship::Grid;
$grid = Games::Battleship::Grid->new(
fleet => \@fleet,
dimension => [$width, $height],
);
ABSTRACT
A Battleship grid class
DESCRIPTION
A Games::Battleship::Grid
object represents a Battleship grid class.
Check out the powerful and mathematically elegant _segment_intersection
function in the source code of this module.
PUBLIC METHODS
- new %ARGUMENTS
-
fleet => [$CRAFT_1, $CRAFT_2, ... $CRAFT_N]
Array reference of an unlimited number of
Games::Battleship::Craft
objects.If provided, the fleet will be placed on the grid. It is required that the number of ships and their combined sizes be less than the area of the grid.
dimensions => [$WIDTH, $HEIGHT]
Array reference with the grid height and width values.
PRIVATE METHODS AND FUNCTIONS
- _init [$CRAFT_1, $CRAFT_2, ... $CRAFT_N]
-
Initialize the grid with the
Games::Battleship::Craft
object dimensions.If an array reference of craft objects is provided, place them on the grid so that they do not intesect (overlap or touch).
- _tail_coordinates @COORDINATES, $SPAN
-
($orientation, $x1, $y1) = _tail_coordinates($x0, $y0, $span);
Return the vertical or horizontal line segment orientation and the tail coordinates, based on the head coodinates and a span (the length of the segment).
Note that this routine is a function, not an object method.
- _segment_intersection @COORDINATES
-
$intersect = _segment_intersection( px0, py0, px1, py1, qx0, qy0, qx1, qy1 );
Return zero if there is no intersection (or touching or overlap).
Each pair of values define a coordinate and each pair of coordinates define a line segment.
TO DO
Allow diagonal craft placement.
Allow some type of interactive craft repositioning.
Allow placement restriction rules (e.g. not on edges, not adjacent, etc.).
Allow > 2D playing spaces.
SEE ALSO
http://www.meca.ucl.ac.be/~wu/FSA2716/Exercise1.htm
CVS
$Id: Grid.pm,v 1.11 2004/02/05 09:20:23 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.