Cute Chess  0.1
Classes | Public Types | Public Member Functions | Protected Types | Protected Member Functions | Friends | List of all members
OpeningBook Class Referenceabstract

A collection of opening moves for chess. More...

#include <openingbook.h>

Inheritance diagram for OpeningBook:
PolyglotBook

Classes

struct  Entry
 An entry in the opening book. More...
 

Public Types

enum  AccessMode { Ram, Disk }
 

Public Member Functions

QList< Entryentries (quint64 key) const
 
int import (const PgnGame &pgn, int maxMoves)
 
int import (PgnStream &in, int maxMoves)
 
Chess::GenericMove move (quint64 key) const
 
 OpeningBook (AccessMode mode=Ram)
 
bool read (const QString &filename)
 
bool write (const QString &filename) const
 
virtual ~OpeningBook ()
 

Protected Types

typedef QMultiMap< quint64, EntryMap
 

Protected Member Functions

void addEntry (const Entry &entry, quint64 key)
 
virtual int entrySize () const =0
 
virtual Entry readEntry (QDataStream &in, quint64 *key) const =0
 
virtual void writeEntry (const Map::const_iterator &it, QDataStream &out) const =0
 

Friends

LIB_EXPORT QDataStreamoperator<< (QDataStream &out, const OpeningBook *book)
 
LIB_EXPORT QDataStreamoperator>> (QDataStream &in, OpeningBook *book)
 

Detailed Description

A collection of opening moves for chess.

OpeningBook is a container (binary tree) class for opening moves that can be played by the GUI. When the game goes "out of book", control of the game is transferred to the players.

The opening book can be stored externally in a binary file. When it's needed, it is loaded in memory, and positions can be found quickly by searching the book for Zobrist keys that match the current board position.

Member Typedef Documentation

typedef QMultiMap<quint64, Entry> OpeningBook::Map
protected

The type of binary tree.

Member Enumeration Documentation

AccessMode defines how a book is accessed during play.

Enumerator
Ram 

Load the entire book to RAM.

Disk 

Read moves directly from disk.

Constructor & Destructor Documentation

OpeningBook::OpeningBook ( AccessMode  mode = Ram)

Creates a new OpeningBook with access mode mode.

OpeningBook::~OpeningBook ( )
virtual

Destroys the opening book.

Member Function Documentation

void OpeningBook::addEntry ( const Entry entry,
quint64  key 
)
protected

Adds a new entry to the book.

QList< OpeningBook::Entry > OpeningBook::entries ( quint64  key) const

Returns all entries matching key.

virtual int OpeningBook::entrySize ( ) const
protectedpure virtual

Returns the book format's internal entry size in bytes.

Implemented in PolyglotBook.

int OpeningBook::import ( const PgnGame pgn,
int  maxMoves 
)

Imports a PGN game.

Parameters
pgnThe game to import.
maxMovesThe maximum number of halfmoves that can be imported.

Returns the number of moves imported.

int OpeningBook::import ( PgnStream in,
int  maxMoves 
)

Imports PGN games from a stream.

Parameters
inThe PGN stream that contains the games.
maxMovesThe maximum number of halfmoves per game that can be imported.

Returns the number of moves imported.

Chess::GenericMove OpeningBook::move ( quint64  key) const

Returns a move that can be played in a position where the Zobrist key is key.

If no matching moves are found, an empty (illegal) move is returned.

If there are multiple matches, a random, weighted move is returned. Popular moves have a higher probablity of being selected than unpopular ones.

bool OpeningBook::read ( const QString filename)

Reads a book from filename. Returns true if successful; otherwise returns false.

virtual Entry OpeningBook::readEntry ( QDataStream in,
quint64 *  key 
) const
protectedpure virtual

Reads a new book entry from in and returns it.

The implementation must set key to the hash that belongs to the entry.

Implemented in PolyglotBook.

bool OpeningBook::write ( const QString filename) const

Writes the book to filename. Returns true if successful; otherwise returns false.

virtual void OpeningBook::writeEntry ( const Map::const_iterator &  it,
QDataStream out 
) const
protectedpure virtual

Writes the key and entry pointed to by it, to out.

Implemented in PolyglotBook.

Friends And Related Function Documentation

LIB_EXPORT QDataStream& operator<< ( QDataStream out,
const OpeningBook book 
)
friend

Writes a book to a data stream.

Warning
Do not write multiple OpeningBook objects to the same data stream, because the books are likely to have duplicate entries.
LIB_EXPORT QDataStream& operator>> ( QDataStream in,
OpeningBook book 
)
friend

Reads a book from a data stream.

Note
Multiple book files can be appended to the same OpeningBook object.

The documentation for this class was generated from the following files: