The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Chess::Opening::Book - A chess opening book

SYNOPSIS

        die "Chess::Opening::Book is an abstract base class";

DESCRIPTION

Chess::Opening::Book is an abstract base class for chess opening books. Instantiate one of the sub classes Chess::Opening::Book::Polyglot or Chess::Opening::Book::ECO instead.

PUBLIC INTERFACE

new ARGS

See the documentation of the sub class you are instantiating for usage information.

lookupFEN FEN

Looks up a position in Forsyth-Edwards-Notation FEN in the opening book. See "Positions" in Chess::Opening for more information on FEN.

The method returns a Chess::Opening::Book::Entry object if the position was found in the book, otherwise false.

Note that the sub class Chess::Opening::Book::ECO returns a Chess::Opening::ECO::Entry, a specialization of a Chess::Opening::Book::Entry..

EXAMPLES

The following code implements a visualization of ECO data:

    use strict;
    
    use Chess::Opening::Book::ECO;
    use Chess::Rep;
    use Storable qw(dclone);
    
    my $book = Chess::Opening::Book::ECO->new;
    my $start = Chess::Rep->new;
    
    print_position($book, $start, 0);
    
    sub print_position {
        my ($book, $pos, $plies) = @_;

        my $entry = $book->lookupFEN($pos->get_fen);
        if (!$entry) {
            print "\n";
            return;
        }
    
        my ($eco, $variation) = ($entry->eco, $entry->variation);
        print "$eco $variation\n";
    
        my $moveno = 1 + int($plies / 2);
        my $prefix = ' ' x (3 * $plies) . "$moveno.";
        if ($plies & 1) {
            $prefix .= '..';
        } else {
            $prefix .= ' ';
        }
    
        my $moves = $entry->moves;
        foreach my $move (sort keys %$moves) {
            my $copy = dclone $pos;
            my $move_info = $copy->go_move($move);
            print "$prefix$move_info->{san} ";
            print_position($book, $copy, $plies + 1);
        }
    }

It instantiates a Chess::Opening::Book::ECO for lookup. You can also visualize a Chess::Opening::Book::Polyglot book in a similar manner but you have to change the presentation below.

The code then recursively prints the entries with the method print_position(). The Forsyth-Edwards Notation of the current position is looked up, and the information found is printed. Neither the methods eco() nor variation() are implemented for polyglot opening books. You would rather print the weight of the current position for them.

The following lines calculates the indentation level before the code loops over the moves leading away from the current position. For each move found the function recurses one level deeper.

You will notice that the script is very slow. This is because the method "go_move()" from Chess::Rep that updates the position with a move is slow. For a faster version you should replace Chess::Rep with something that uses bitboards.

COPYRIGHT

Copyright (C) 2019 Guido Flohr <guido.flohr@cantanea.com>, all rights reserved.

SEE ALSO

Chess::Opening::Book, Chess::Opening::Book::Polyglot, Chess::Opening::Book::ECO, Chess::Rep, perl(1)