NAME
Chess::Game::MoveList - a specialized list class for recording the moves of a Chess::Game
SYNOPSIS
$movelist = Chess::Game::MoveList->new("white", "black");
$wpawn = Chess::Game::Pawn->new("e2", "white");
$entry = $movelist->add_move($wpawn, "e2", "e4");
$true = $entry->get_piece() eq $entry;
$bpawn = Chess::Game::Pawn->new("e7", "black");
$entry = $movelist->add_move($bpawn, "e7", "e6");
$entry = $movelist->add_move($wpawn, "e4", "e5");
@del_entries = $movelist->delete_move(1, "white"); # delete the list
$true = $entries[0]->get_piece() eq $wpawn;
$true = $entries[0]->get_dest_square() eq "e4";
$true = $entries[1]->get_piece() eq $bpawn;
$true = $entries[1]->get_dest_square() eq "e6";
DESCRIPTION
The Chess module provides a framework for writing chess programs with Perl. This class forms part of that framework, recording a log of all moves during a Chess::Game in such a fashion that the list can be used to undo moves that have been made.
METHODS
Construction
- new()
-
Creates a new Chess::Game::MoveList. Takes two scalar parameters containing the names of the two players. These names will be used as a key for calls to "get_move()" and "delete_move()".
$movelist = Chess::Game::MoveList("white", "black");
Class methods
Object methods
- clone()
-
Creates a new Chess::MoveList based on an existing one. Returns a new list with identical contents, but can be manipulated separately of the original.
$clone = $movelist->clone();
- get_move_num()
-
Takes no parameters. Returns the current move number of the game. Numbering is identical to numbering in a regular chess game. The move number does not increment until the first player's next turn.
$move_num = $movelist->get_move_num();
- get_last_moved()
-
Takes no parameters. Returns the name of the player who last moved. It will be one of the values passed to "new()" and can be used as a key to "get_move()" and "delete_move()".
$last_moved = $movelist->get_last_moved();
- get_move()
-
Takes two scalar parameters containing the move number and the name of the player to get the move for. Returns a blessed Chess::Game::MoveListEntry with the particulars for that move, or
undef
if that move wasn't found.$entry = $movelist->get_move(1, "white"); # pawn to king's four, perhaps?
- get_all_moves()
-
Takes an optional scalar parameter specifying which player to return a list of moves for. Returns an array of all the entries for moves made by that player. If the player is not specified, returns a two-element array containing references to the first player's and second player's lists respectively.
@wmoves = $movelist->get_all_moves("white"); @bmoves = $movelist->get_all_moves("black"); ($wmoves, $bmoves) = $movelist->get_all_moves();
- add_move()
-
Takes three scalar parameters containing a reference to the piece being moved, the square it is being moved from, and square it is being moved to. Returns a blessed Chess::Game::MoveListEntry containing the particulars for that move.
$entry = $movelist->add_move($pawn, "e2", "e4");
- delete_move()
-
Takes no parameters. Returns the last move to be made, if there is one, and then deletes it. The MoveList is now in exactly the same state as prior to the last move being made.
$entry = $movelist->delete_move();
DIAGNOSTICS
- Invalid Chess::Game::MoveList reference
-
The program contains a reference to a Chess::Game::MoveList object not obtained through "new()" or "clone()". Ensure that all such references were obtained properly, and that the reference refers to a defined value.
- Chess::Game::MoveList player entries must be unique keys
-
"new()" requires that the two arguments can be used as hash keys. Ensure that the call to new contains two defined, unique keys as player names.
- Invalid move number
-
The program contains a call to a method requiring a move number, and passes in a move number of 0 or less. Move numbering starts at 1 to be consistent with a standard chess game.
BUGS
Please report any bugs to the author.
AUTHOR
Brian Richardson <bjr@cpan.org>
COPYRIGHT
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.
6 POD Errors
The following errors were encountered while parsing the POD:
- Around line 32:
'=item' outside of any '=over'
- Around line 40:
You forgot a '=back' before '=head2'
- Around line 44:
'=item' outside of any '=over'
- Around line 103:
You forgot a '=back' before '=head1'
- Around line 105:
'=item' outside of any '=over'
- Around line 122:
You forgot a '=back' before '=head1'