Chess::Play - Play chess games, calculate legal moves, use a search algorithm


  use Chess::Play;

  my $cp = Chess::Play->new();
  $cp->set_piece_val($p_val, $n_val, $b_val, $r_val, $q_val);


This module allows to play a chess game using STDIN or the xboard graphical interface. Il also can calculate legal moves and uses the Alpha-Beta search algorithm to find the best move.


  • $cp = Chess::Play->new()

    Create a new object to play chess.

  • $cp->reset()

    Reset to the start position.

  • $cp->import_fen($fen)

    Set the position according to the FEN string $fen

  • $fen = $cp->export_fen()

    Export the current position to the FEN string $fen

  • $cp->set_piece_val($p_val, $n_val, $b_val, $r_val, $q_val)

    Change default values for pieces (the defaults are : 1, 3, 3, 5, 9)

  • $cp->set_depth($depth)

    Set the depth of the search algorithm (Alpha-Beta search).

  • @legal_moves = $cp->legal_moves()

    Calculate the list of legal moves

  • $move_ok = $cp->do_move($move)

    execute a move (for instance "e2e4" or "a7a8q"). Return 1 if the move is legal, -1 if invalid, -2 if illegal

  • $game_over = $cp->game_over()

    Tell if the game is over (Sheckmate, Stalemate, Insufficient Material, 50 moves rule). Threeway repetition is not supported in this version. Return "" if the game is not over.

  • $best_move = $cp->best_move()

    Return the best move according to the search algorithm

  • $cp->print_board();

    Print an ASCII representation of the board

  • $cp->play()

    Play a chess game using STDIN

  • $cp->xboard_play()

  • $cp->xboard_play($custom_name)

    Play a chess game using xboard (only the basic xboad directives are supported). You can choose a name for your engine.


  • Create a new Chess::Play object my $cp = Chess::Play->new();

  • Execute some moves if ($cp->do_move("e2e4") == 1) {..} if ($cp->do_move("e7e5") == 1) {..}

  • Calculate legal moves @legal_moves = $cp->legal_moves()

  • Play a chess game using stdin (using coordinate notation) $cp->reset(); $ce->set_depth(2); $ce->play();

  • Create an xboard-compatible chess engine $cp->reset(); $ce->set_depth(2); $cp->xboard_play("My_Chess_Engine")

  • Use xboard xboard -fcp /path/to/ or xboard -fcp /path/to/ -scp /path/to/

  • Find a mate in 2 $cp->import_fen("1R6/8/KN6/8/1k6/7r/2QP4/8 w - - 0 1"); $cp->set_depth(3); $bm = $cp->best_move();


Giuliano Ippoliti, g1ul14n0 AT gmail


This is free software in the colloquial nice-guy sense of the word. Copyright (c) 2009, Giuliano Ippoliti. You may redistribute and/or modify it under the same terms as Perl itself.



2 POD Errors

The following errors were encountered while parsing the POD:

Around line 1740:

You forgot a '=back' before '=head1'

Around line 1742:

'=item' outside of any '=over'