Chess::Piece::Rook - an object representation of a rook in a game of chess


Chess documentation Contained in the Chess distribution.

Index


Code Index:

NAME

Top

Chess::Piece::Rook - an object representation of a rook in a game of chess

SYNOPSIS

Top

    $rook = Chess::Piece::Rook->new("a1", "white", 
                                    "White Queen's rook");
    $true = $rook->can_reach("h1");
    $true = $rook->can_reach("a8");
    $false = $rook->can_reach("d4");

DESCRIPTION

Top

The Chess module provides a framework for writing chess programs with Perl. This class forms part of that framework, representing a rook in a Chess::Game.

METHODS

Top

Construction

Constructs a new Chess::Piece::Rook. Requires two scalar parameters containing the initial square and color of the piece. Optionally takes a third parameter containing a description of the piece.

Class methods

There are no class methods for this class.

Object methods

Overrides base class version. Returns a list of squares that this pawn can reach from its current position. See "reachable_squares()" in Chess::Piece for more details on this method.

DIAGNOSTICS

Top

This module produces no warning messages. See DIAGNOSTICS in Chess::Board or DIAGNOSTICS in Chess::Piece for possible errors or warnings the program may produce.

BUGS

Top

Please report any bugs to the author.

AUTHOR

Top

Brian Richardson <bjr@cpan.org>

COPYRIGHT

Top


Chess documentation Contained in the Chess distribution.

package Chess::Piece::Rook;

use Chess::Board;
use Chess::Piece;
use base 'Chess::Piece';
use strict;

sub new {
    my ($caller, $sq, $color, $desc) = @_;
    my $class = ref($caller) || $caller;
    my $self = $caller->SUPER::new($sq, $color, $desc);
    return bless $self, $class;
}

sub reachable_squares {
    my ($self) = @_;
    my $csq = $self->get_current_square();
    my $x = Chess::Board->horz_distance("a4", $csq);
    my $y = Chess::Board->vert_distance("d1", $csq);
    my $row_start = 'a' . ($y + 1);
    my $row_end = 'h' . ($y + 1); 
    my $col_start = chr(ord('a') + $x) . '1';
    my $col_end = chr(ord('a') + $x) . '8';
    my @row = Chess::Board->squares_in_line($row_start, $row_end);
    my @col = Chess::Board->squares_in_line($col_start, $col_end);
    my @squares = (@row, @col);
    return grep !/^$csq$/, @squares;
}

1;