Chess::Piece::King - an object representation of a king in a game of chess


Chess documentation Contained in the Chess distribution.

Index


Code Index:

NAME

Top

Chess::Piece::King - an object representation of a king in a game of chess

SYNOPSIS

Top

    $king = Chess::Piece::King->new("e1", "white", "White King");
    $true = $king->can_reach("d1");
    $true = $king->can_reach("f1");
    $true = $king->can_reach("d2");
    $true = $king->can_reach("e2");
    $true = $king->can_reach("f2");
    $true = $king->can_reach("g1"); # O-O
    $king->set_moved(1);
    $false = $king->can_reach("g1");
    $false = $king->can_reach("d4");
    $king->set_captured(1); # dies with message
    $king->set_checkmated(1); # use this instead
    if ($king->checkmated()) {
	# game over...
    }

DESCRIPTION

Top

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

METHODS

Top

Construction

Constructs a new Chess::Piece::King. 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.

Takes no parameters. Returns true if the checkmated flag has been set for this king (as determined by "set_checkmated()".

Takes a single scalar parameter containing a boolean value. Sets the checkmated property of this king to that value.

DIAGNOSTICS

Top

The program contains a call to Chess::Piece::King::set_captured. This method has been overridden to croak, as the rules don't allow for capturing the king. See "set_checkmated()".

The program contains a reference to a Chess::Piece::King that was not obtained through "new()" or "clone()" in Chess::Piece. Ensure that the program obtains the reference correctly, and that it does not refer to an undefined value.

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::King;

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

sub captured {
    croak "King can't be captured";
}

sub set_captured {
    croak "King can't be captured";
}

sub reachable_squares {
    my ($self) = @_;
    my $csq = $self->get_current_square();
    my @squares = ( );
    my $sq1 = Chess::Board->square_left_of($csq);
    if (defined($sq1)) {
	push @squares, $sq1;
	my $sq2 = Chess::Board->square_up_from($sq1);
	push @squares, $sq2 if (defined($sq2));
	$sq2 = Chess::Board->square_down_from($sq1);
	push @squares, $sq2 if (defined($sq2));
    }
    $sq1 = Chess::Board->square_right_of($csq);
    if (defined($sq1)) {
	push @squares, $sq1;
	my $sq2 = Chess::Board->square_up_from($sq1);
	push @squares, $sq2 if (defined($sq2));
	$sq2 = Chess::Board->square_down_from($sq1);
	push @squares, $sq2 if (defined($sq2));
    }
    $sq1 = Chess::Board->square_up_from($csq);
    push @squares, $sq1 if (defined($sq1));
    $sq1 = Chess::Board->square_down_from($csq);
    push @squares, $sq1 if (defined($sq1));
    $sq1 = Chess::Board->add_horz_distance($csq, 2);
    push @squares, $sq1 if (defined($sq1) and !$self->moved());
    $sq1 = Chess::Board->add_horz_distance($csq, -2);
    push @squares, $sq1 if (defined($sq1) and !$self->moved());
    return @squares;
}

1;