View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Brian Richardson > Chess > Chess::Game::MoveList



Annotate this POD


New  3
Open  1
View/Report Bugs


Chess::Game::MoveList - a specialized list class for recording the moves of a Chess::Game


    $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";


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.




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


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();

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();

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();

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?

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();

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");

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();


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.


Please report any bugs to the author.


Brian Richardson <>


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.

syntax highlighting: