NAME

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

SYNOPSIS

use Chess::Play;

my $cp = Chess::Play->new();
$cp->reset();
$cp->set_piece_val($p_val, $n_val, $b_val, $r_val, $q_val);
$cp->set_depth($depth)
$cp->legal_moves()
$cp->do_move($move)
$cp->best_move()
$cp->game_over();
$cp->print_board();
$cp->play()
$cp->xboard_play([$custom_name])

DESCRIPTION

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.

METHODS

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

    Create a new object to play chess.

  • $cp->reset()

    Reset to the start position.

  • $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.

    Examples:
     my $cp = Chess::Play->new();
    
     Execute some moves
     if ($cp->do_move("e2e4") == 1) {..}
     if ($cp->do_move("e7e5") == 1) {..}
    
     Calculating legal moves
     my @legal_moves = $cp->legal_moves()
    
     Play a chess game using stdin
     $cp->reset();
     $ce->set_depth(2);
     $ce->play();
    
     Play a chess game using xboard
     $cp->reset();
     $ce->set_depth(2);
     $cp->xboard_play("My_Chess_Engine")
    
     xboard -fcp /path/to/my_engine.pl or xboard -fcp /path/to/my_engine.pl -scp /path/to/my_engine2.pl

AUTHOR

Giuliano Ippoliti, g1ul14n0 AT gmail

COPYRIGHT

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.

SEE ALSO

perl(1).