$piece = Chess::Piece->new("e2", "white", "White King's pawn");
$piece->set_current_square("e4");
$e4 = $piece->get_current_square();
$piece->set_description("My Piece");
$description = $piece->get_description();
$color = $piece->get_color();
if (!$piece->moved()) {
# do something with the unmoved piece
}
$piece->set_moved(1);
if ($piece->threatened()) {
# do something with the threatened piece
}
$piece->set_threatened(1);
if ($piece->captured()) {
# do something with the captured piece
}
$piece->set_captured(1);
The Chess module provides a framework for writing chess programs with Perl.
This class represents the parent class for all Chess pieces, and contains
accessors and mutators for all the common properties of chess pieces.
The following is an exhaustive list of the properties of a Chess::Piece:
* initial square (read-only, specified at construction)
* color (read-only, specified at construction)
* current square
* description
* a flag indicating whether or not the piece has moved
* a flag indicating whether or not the piece is threatened
* a flag indicating whether or not the piece was captured
See /"METHODS" for details of the methods which manipulate and return these
properties.
Constructs a new Chess::Piece. Requires a two scalar arguments containing the
initial square this piece is on and the color of the piece. If the program
will use colors other than 'black' and 'white', then subclasses of
Chess::Piece will need to override the /"can_reach()" method to take these
colors into account. Optionally takes a third argument containing a text
description of the piece. Returns a blessed Chess::Piece object reference
that can be used to call any of the methods listed in /"Object methods".
The square is not tested for validity, so the program must validate the
square before calling new().
Clones an existing Chess::Piece. Requires no arguments. Returns a blessed
Chess::Piece object reference which has data identical to the cloned piece,
but can be manipulated separately.
Takes a single scalar parameter containing the current square of this piece.
Sets the current square property to this value. Like /"new()", this square
is not tested for validity and should be tested before calling the function.
Takes a single scalar parameter containing true or false. Sets the captured
flag, and also sets the current square property to undef, if the parameter
is true.
Takes a single scalar parameter containing the square to be tested. Returns
true if the piece can reach the given square from its current location, as
determined by a call to the abstract method /"reachable_squares()".
This is an abstract method and must be overridden in all subclasses of
Chess::Piece. Returns a list of squares (in lower-case) that the piece can
reach. This list is used by /"can_reach()" and various methods of
Chess::Game to determine legality of moves and other high-level analyses.
Thus, subclasses of Chess::Piece not provided by this framework must return
all squares that may be reached, regardless of the current state of the
board. The Chess::Game/"is_move_legal()" method will then determine if all
conditions for a particular move have been met.
The program uses a reference which is undefined, or was obtained without
using /"new()" or /"clone()". Ensure that the program only obtains
its references from new() or clone() and that the reference refers to a
defined value.
Copyright (c) 2002, 2005 Brian Richardson. All rights reserved. This module
is Free Software. It may be modified and redistributed under the same terms
as Perl itself.