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

An internal chessboard class. More...

#include <board.h>

Inheritance diagram for Chess::Board:
Chess::WesternBoard Chess::AlmostBoard Chess::AmazonBoard Chess::AtomicBoard Chess::CapablancaBoard Chess::ChancellorBoard Chess::ChecklessBoard Chess::ChigorinBoard Chess::CoRegalBoard Chess::CrazyhouseBoard Chess::GryphonBoard Chess::GustavBoard Chess::HoppelPoppelBoard Chess::JanusBoard Chess::JesonMorBoard Chess::KnightMateBoard Chess::LosAlamosBoard Chess::LosersBoard Chess::ModernBoard Chess::NewZealandBoard Chess::PlacementBoard Chess::PocketKnightBoard Chess::RacingKingsBoard Chess::RestrictedMoveBoard Chess::RifleBoard Chess::SeirawanBoard Chess::ShatranjBoard Chess::StandardBoard Chess::ThreeKingsBoard Chess::TwoKingsEachBoard

Public Types

enum  CoordinateSystem { NormalCoordinates, InvertedCoordinates }
 
enum  FenNotation { XFen, ShredderFen }
 
enum  MoveNotation { StandardAlgebraic, LongAlgebraic }
 

Public Member Functions

 Board (Zobrist *zobrist)
 
virtual CoordinateSystem coordinateSystem () const
 
virtual Boardcopy () const =0
 
virtual QString defaultFenString () const =0
 
QString fenString (FenNotation notation=XFen) const
 
GenericMove genericMove (const Move &move) const
 
virtual int height () const =0
 
void initialize ()
 
bool isLegalMove (const Move &move)
 
virtual bool isRandomVariant () const
 
bool isRepetition (const Move &move)
 
bool isValidSquare (const Square &square) const
 
quint64 key () const
 
QVector< MovelegalMoves ()
 
void makeMove (const Move &move, BoardTransition *transition=nullptr)
 
Move moveFromGenericMove (const GenericMove &move) const
 
Move moveFromString (const QString &str)
 
QString moveString (const Move &move, MoveNotation notation)
 
Piece pieceAt (const Square &square) const
 
Piece pieceFromSymbol (const QString &pieceSymbol) const
 
QStringList pieceList (Side side) const
 
QString pieceString (int pieceType) const
 
QString pieceSymbol (Piece piece) const
 
int plyCount () const
 
int repeatCount () const
 
QString representation (Piece piece) const
 
int reserveCount (Piece piece) const
 
virtual QList< PiecereservePieceTypes () const
 
void reset ()
 
virtual Result result ()=0
 
virtual int reversibleMoveCount () const
 
bool setFenString (const QString &fen)
 
Side sideToMove () const
 
QString startingFenString () const
 
Side startingSide () const
 
virtual Result tablebaseResult (unsigned int *dtm=nullptr) const
 
void undoMove ()
 
virtual Side upperCaseSide () const
 
virtual QString variant () const =0
 
virtual bool variantHasDrops () const
 
virtual bool variantHasOptionalPromotions () const
 
virtual bool variantHasWallSquares () const
 
virtual int width () const =0
 
virtual ~Board ()
 

Protected Member Functions

void addToReserve (const Piece &piece, int count=1)
 
int arraySize () const
 
bool canMove ()
 
virtual int captureType (const Move &move) const
 
Square chessSquare (int index) const
 
Square chessSquare (const QString &str) const
 
void generateDropMoves (QVarLengthArray< Move > &moves, int pieceType) const
 
void generateHoppingMoves (int sourceSquare, const QVarLengthArray< int > &offsets, QVarLengthArray< Move > &moves) const
 
void generateMoves (QVarLengthArray< Move > &moves, int pieceType=Piece::NoPiece) const
 
virtual void generateMovesForPiece (QVarLengthArray< Move > &moves, int pieceType, int square) const =0
 
void generateSlidingMoves (int sourceSquare, const QVarLengthArray< int > &offsets, QVarLengthArray< Move > &moves) const
 
virtual bool isLegalPosition ()=0
 
virtual QString lanMoveString (const Move &move)
 
const MovelastMove () const
 
virtual int maxPieceSymbolLength () const
 
bool moveExists (const Move &move) const
 
virtual Move moveFromLanString (const QString &str)
 
virtual Move moveFromSanString (const QString &str)=0
 
Piece pieceAt (int square) const
 
bool pieceHasMovement (int pieceType, unsigned movement) const
 
void removeFromReserve (const Piece &piece)
 
virtual int reserveType (int pieceType) const
 
virtual QString sanMoveString (const Move &move)=0
 
void setPieceType (int type, const QString &name, const QString &symbol, unsigned movement=0, const QString &gsymbol=QString())
 
void setSquare (int square, Piece piece)
 
int squareIndex (const Square &square) const
 
int squareIndex (const QString &str) const
 
QString squareString (int index) const
 
QString squareString (const Square &square) const
 
virtual QString vFenString (FenNotation notation) const =0
 
virtual void vInitialize ()=0
 
virtual bool vIsLegalMove (const Move &move)
 
virtual void vMakeMove (const Move &move, BoardTransition *transition)=0
 
virtual bool vSetFenString (const QStringList &fen)=0
 
virtual void vUndoMove (const Move &move)=0
 
void xorKey (quint64 key)
 

Friends

LIB_EXPORT QDebug operator<< (QDebug dbg, const Board *board)
 

Detailed Description

An internal chessboard class.

This is the base class for all chess variants. Board's main purposes are:

Member Enumeration Documentation

Coordinate system for the notation of the squares.

Enumerator
NormalCoordinates 

Normal/traditional coordinates used by most chess variants.

The file is denoted by a letter, starting with file 'A' on the left. The rank is denoted by a number, starting with rank '1' at the bottom.

InvertedCoordinates 

Inverted coordinates used by some eastern variants like Shogi.

The file is denoted by a number, starting with file '1' on the right. The rank is denoted by a letter, starting with rank 'A' at the top.

Notation for FEN strings.

Enumerator
XFen 

X-FEN notation.

Note
Specs: http://en.wikipedia.org/wiki/X-FEN
ShredderFen 

Shredder FEN notation.

Notation for move strings.

Enumerator
StandardAlgebraic 

Standard Algebraic notation (SAN).

LongAlgebraic 

Long Algebraic/Coordinate notation.

Constructor & Destructor Documentation

Chess::Board::Board ( Zobrist zobrist)

Creates a new Board object.

Parameters
zobristZobrist keys for quickly identifying and comparing positions. The Board class takes ownership of the zobrist object and takes care of deleting it.
Chess::Board::~Board ( )
virtual

Destructs the Board object.

Member Function Documentation

void Chess::Board::addToReserve ( const Piece piece,
int  count = 1 
)
protected

Adds count pieces of type piece to the reserve.

int Chess::Board::arraySize ( ) const
inlineprotected

Returns the size of the board array, including the padding (the inaccessible wall squares).

bool Chess::Board::canMove ( )
protected

Returns true if the side to move has any legal moves.

int Chess::Board::captureType ( const Move move) const
protectedvirtual

Returns the type of piece captured by move. Returns Piece::NoPiece if move is not a capture.

Reimplemented in Chess::WesternBoard.

Square Chess::Board::chessSquare ( int  index) const
protected

Converts a square index into a Square object.

Square Chess::Board::chessSquare ( const QString str) const
protected

Converts a string into a Square object.

Board::CoordinateSystem Chess::Board::coordinateSystem ( ) const
virtual

Returns the coordinate system used in the variant.

virtual Board* Chess::Board::copy ( ) const
pure virtual

Creates and returns a deep copy of this board.

Implemented in Chess::ChangeOverBoard, Chess::CircularGryphonBoard, Chess::BerolinaGridBoard, Chess::SlippedGridBoard, Chess::KarOukBoard, Chess::SuperAndernachBoard, Chess::SimplifiedGryphonBoard, Chess::FiveCheckBoard, Chess::TwoKingsSymmetricalBoard, Chess::KingletBoard, Chess::ThreeCheckBoard, Chess::AntiAndernachBoard, Chess::DisplacedGridBoard, Chess::MakrukBoard, Chess::ShootBoard, Chess::SittuyinBoard, Chess::NewZealandBoard, Chess::AseanBoard, Chess::CourierBoard, Chess::ShatranjBoard, Chess::CodrusBoard, Chess::GrandBoard, Chess::AntiBoard, Chess::ExtinctionBoard, Chess::PlacementBoard, Chess::SuicideBoard, Chess::HordeBoard, Chess::OukBoard, Chess::GiveawayBoard, Chess::AndernachBoard, Chess::GryphonBoard, Chess::ModernBoard, Chess::SeirawanBoard, Chess::ChancellorBoard, Chess::JanusBoard, Chess::NCheckBoard, Chess::ChigorinBoard, Chess::BerolinaBoard, Chess::ChessgiBoard, Chess::CoRegalBoard, Chess::GustavBoard, Chess::TwoKingsEachBoard, Chess::GridBoard, Chess::KingOfTheHillBoard, Chess::LoopBoard, Chess::PocketKnightBoard, Chess::AlmostBoard, Chess::EmbassyBoard, Chess::HoppelPoppelBoard, Chess::RacingKingsBoard, Chess::RifleBoard, Chess::AmazonBoard, Chess::ChecklessBoard, Chess::FrcBoard, Chess::StandardBoard, Chess::LosAlamosBoard, Chess::ThreeKingsBoard, Chess::CaparandomBoard, Chess::JesonMorBoard, Chess::CrazyhouseBoard, Chess::GothicBoard, Chess::KnightMateBoard, Chess::LosersBoard, Chess::AiWokBoard, Chess::AtomicBoard, Chess::CapablancaBoard, and Chess::RestrictedMoveBoard.

virtual QString Chess::Board::defaultFenString ( ) const
pure virtual
QString Chess::Board::fenString ( FenNotation  notation = XFen) const

Returns the FEN string of the current board position in X-Fen or Shredder FEN notation

void Chess::Board::generateDropMoves ( QVarLengthArray< Move > &  moves,
int  pieceType 
) const
protected

Generates piece drops for pieces of type pieceType.

Note
If pieceType is Piece::NoPiece, moves are generated for every piece type.
See also
generateMoves()
void Chess::Board::generateHoppingMoves ( int  sourceSquare,
const QVarLengthArray< int > &  offsets,
QVarLengthArray< Move > &  moves 
) const
protected

Generates hopping moves for a piece.

Parameters
sourceSquareThe source square of the hopping piece
offsetsAn array of offsets for the target square
Note
The generated moves include captures
void Chess::Board::generateMoves ( QVarLengthArray< Move > &  moves,
int  pieceType = Piece::NoPiece 
) const
protected

Generates pseudo-legal moves for pieces of type pieceType.

Note
If pieceType is Piece::NoPiece (default), moves are generated for every piece type.
See also
legalMoves()
virtual void Chess::Board::generateMovesForPiece ( QVarLengthArray< Move > &  moves,
int  pieceType,
int  square 
) const
protectedpure virtual
void Chess::Board::generateSlidingMoves ( int  sourceSquare,
const QVarLengthArray< int > &  offsets,
QVarLengthArray< Move > &  moves 
) const
protected

Generates sliding moves for a piece.

Parameters
sourceSquareThe source square of the sliding piece
offsetsAn array of offsets for the target square
Note
The generated moves include captures
GenericMove Chess::Board::genericMove ( const Move move) const

Converts a Move into a GenericMove.

Note
The board must be in a position where move can be made.
See also
moveFromGenericMove()
virtual int Chess::Board::height ( ) const
pure virtual
void Chess::Board::initialize ( )

Initializes the board. This function must be called before a game can be started on the board.

bool Chess::Board::isLegalMove ( const Move move)

Returns true if move is legal in the current position.

virtual bool Chess::Board::isLegalPosition ( )
protectedpure virtual

Returns true if the current position is a legal position. If the position isn't legal it usually means that the last move was illegal.

Implemented in Chess::WesternBoard, Chess::ChangeOverBoard, Chess::SittuyinBoard, Chess::PlacementBoard, Chess::GryphonBoard, Chess::RacingKingsBoard, and Chess::ChecklessBoard.

bool Chess::Board::isRandomVariant ( ) const
virtual

Returns true if the variant uses randomized starting positions. The default value is false.

Reimplemented in Chess::FrcBoard, and Chess::CaparandomBoard.

bool Chess::Board::isRepetition ( const Move move)

Returns true if move repeats a position that was reached earlier in the game.

bool Chess::Board::isValidSquare ( const Square square) const

Returns true if square is on the board.

quint64 Chess::Board::key ( ) const
inline

Returns the zobrist key for the current position.

QString Chess::Board::lanMoveString ( const Move move)
protectedvirtual

Converts a Move object into a string in Long Algebraic Notation (LAN)

Reimplemented in Chess::WesternBoard, and Chess::SeirawanBoard.

const Move & Chess::Board::lastMove ( ) const
inlineprotected

Returns the last move made in the game.

QVector< Move > Chess::Board::legalMoves ( )

Returns a vector of legal moves in the current position.

void Chess::Board::makeMove ( const Move move,
BoardTransition transition = nullptr 
)

Makes a chess move on the board.

All details about piece movement, promotions, captures, drops, etc. are stored in transition. These details are useful mainly for updating a graphical representation of the board.

int Chess::Board::maxPieceSymbolLength ( ) const
protectedvirtual

Returns the maximal length of a piece symbol

Reimplemented in Chess::LoopBoard.

bool Chess::Board::moveExists ( const Move move) const
protected

Returns true if a pseudo-legal move move exists.

See also
isLegalMove()
Move Chess::Board::moveFromGenericMove ( const GenericMove move) const

Converts a GenericMove into a Move.

Note
The board must be in a position where move can be made.
See also
genericMove()
Move Chess::Board::moveFromLanString ( const QString str)
protectedvirtual

Converts a string in LAN format into a Move object.

Reimplemented in Chess::WesternBoard, Chess::SeirawanBoard, and Chess::TwoKingsEachBoard.

virtual Move Chess::Board::moveFromSanString ( const QString str)
protectedpure virtual
Move Chess::Board::moveFromString ( const QString str)

Converts a move string into a Move.

Note
Returns a null move if move is illegal.
Notation is automatically detected, and can be anything that's specified in MoveNotation.
See also
moveString()
QString Chess::Board::moveString ( const Move move,
MoveNotation  notation 
)

Converts a Move into a string.

Note
The board must be in a position where move can be made.
See also
moveFromString()
Piece Chess::Board::pieceAt ( const Square square) const

Returns the piece at square.

Piece Chess::Board::pieceAt ( int  square) const
inlineprotected

Returns the piece at square.

Piece Chess::Board::pieceFromSymbol ( const QString pieceSymbol) const

Converts pieceSymbol into a Piece object.

bool Chess::Board::pieceHasMovement ( int  pieceType,
unsigned  movement 
) const
inlineprotected

Returns true if pieceType can move like movement.

QStringList Chess::Board::pieceList ( Side  side) const

Returns list of the pieces of side in current position.

QString Chess::Board::pieceString ( int  pieceType) const

Returns the internationalized name of pieceType.

QString Chess::Board::pieceSymbol ( Piece  piece) const

Converts piece into a piece symbol.

int Chess::Board::plyCount ( ) const
inline

Returns the number of halfmoves (plies) played.

void Chess::Board::removeFromReserve ( const Piece piece)
protected

Removes a piece of type piece from the reserve.

int Chess::Board::repeatCount ( ) const

Returns the number of times the current position was reached previously in the game.

QString Chess::Board::representation ( Piece  piece) const

Returns symbol for graphical representation of piece.

int Chess::Board::reserveCount ( Piece  piece) const

Returns the number of reserve pieces of type piece.

On variants that don't have piece drops this function always returns 0.

QList< Piece > Chess::Board::reservePieceTypes ( ) const
virtual

Returns a list of piece types that can be in the reserve, ie. captured pieces that can be dropped on the board.

The default implementation returns an empty list.

Reimplemented in Chess::SittuyinBoard, Chess::SeirawanBoard, Chess::PlacementBoard, Chess::PocketKnightBoard, and Chess::CrazyhouseBoard.

int Chess::Board::reserveType ( int  pieceType) const
protectedvirtual

Returns the reserve piece type corresponding to pieceType.

The returned value is the type of piece a player receives (in variants that have piece drops) when he captures a piece of type pieceType.

The default value is pieceType.

Reimplemented in Chess::CrazyhouseBoard, and Chess::PocketKnightBoard.

void Chess::Board::reset ( )

Sets the board position to the default starting position of the chess variant.

virtual Result Chess::Board::result ( )
pure virtual
int Chess::Board::reversibleMoveCount ( ) const
virtual

Returns the number of consecutive reversible moves made.

The default implementation always returns -1.

Reimplemented in Chess::WesternBoard.

virtual QString Chess::Board::sanMoveString ( const Move move)
protectedpure virtual
bool Chess::Board::setFenString ( const QString fen)

Sets the board position according to a FEN string.

The fen string can be in standard FEN, X-FEN or Shredder FEN notation.

Returns true if successful; otherwise returns false.

void Chess::Board::setPieceType ( int  type,
const QString name,
const QString symbol,
unsigned  movement = 0,
const QString gsymbol = QString() 
)
protected

Defines a piece type used in the variant. If the piece isn't already defined, it's gets added here. Unlike other initialization which happens in vInitialize(), all piece types should be defined in the constructor.

Parameters
typeType of the piece in integer format
nameThe piece's name (internationalized string)
symbolShort piece name or piece symbol
movementA bit mask for the kinds of moves the piece can make.
gsymbolSelect the piece's graphical representation. If not set the symbol will be used (default).
void Chess::Board::setSquare ( int  square,
Piece  piece 
)
inlineprotected

Sets square to contain piece.

This function also updates the zobrist position key, so subclasses shouldn't mess with it directly.

Side Chess::Board::sideToMove ( ) const
inline

Returns the side to move.

int Chess::Board::squareIndex ( const Square square) const
protected

Converts a Square object into a square index.

int Chess::Board::squareIndex ( const QString str) const
protected

Converts a string into a square index.

QString Chess::Board::squareString ( int  index) const
protected

Converts a square index into a string.

QString Chess::Board::squareString ( const Square square) const
protected

Converts a Square object into a string.

QString Chess::Board::startingFenString ( ) const
inline

Returns the FEN string of the starting position.

Note
This is not always the same as defaultFenString().
Side Chess::Board::startingSide ( ) const
inline

Returns the side that made/makes the first move.

Result Chess::Board::tablebaseResult ( unsigned int *  dtm = nullptr) const
virtual

Returns the expected game result according to endgame tablebases.

If the position is a win for either player, dtm is set to the distance to mate, ie. the number of plies it takes to force a mate.

The default implementation always returns a null result.

Reimplemented in Chess::StandardBoard.

void Chess::Board::undoMove ( )

Reverses the last move.

Side Chess::Board::upperCaseSide ( ) const
virtual

Returns the side whose pieces are denoted by uppercase letters. The default value is White.

virtual QString Chess::Board::variant ( ) const
pure virtual
bool Chess::Board::variantHasDrops ( ) const
virtual

Returns true if the variant allows piece drops. The default value is false.

See also
CrazyhouseBoard

Reimplemented in Chess::SittuyinBoard, Chess::SeirawanBoard, Chess::PlacementBoard, Chess::PocketKnightBoard, and Chess::CrazyhouseBoard.

bool Chess::Board::variantHasOptionalPromotions ( ) const
virtual

Returns true if the variant allows to skip a promotion (or a move treated as promotion) and make a normal move instead. The default value is false, i.e. mandatory promotions.

See also
GrandBoard
SeirawanBoard

Reimplemented in Chess::SittuyinBoard, Chess::SeirawanBoard, and Chess::GrandBoard.

bool Chess::Board::variantHasWallSquares ( ) const
virtual

Returns true if the board accepts wall squares, else false. The default value is false.

Reimplemented in Chess::GustavBoard.

virtual QString Chess::Board::vFenString ( FenNotation  notation) const
protectedpure virtual

Returns the latter part of the current position's FEN string.

This function is called by fenString(). The board state, side to move and hand pieces are handled by the base class. This function returns the rest of it, if any.

Implemented in Chess::WesternBoard, Chess::MakrukBoard, Chess::SeirawanBoard, Chess::OukBoard, and Chess::AseanBoard.

virtual void Chess::Board::vInitialize ( )
protectedpure virtual

Initializes the variant.

This function is called by initialize(). Subclasses shouldn't generally call it by themselves.

Implemented in Chess::WesternBoard, Chess::MakrukBoard, Chess::ShatranjBoard, Chess::CourierBoard, Chess::NCheckBoard, Chess::GryphonBoard, Chess::TwoKingsEachBoard, and Chess::AtomicBoard.

bool Chess::Board::vIsLegalMove ( const Move move)
protectedvirtual

Returns true if move is a legal move.

This function is called by isLegalMove() after making sure that there is a pseudo-legal move same as move. This function shouldn't check for the existence of move by generating moves.

The default implementation only checks if the position after move is legal.

Reimplemented in Chess::WesternBoard, Chess::SittuyinBoard, Chess::ShootBoard, Chess::AntiBoard, Chess::CodrusBoard, Chess::GrandBoard, Chess::HordeBoard, Chess::AtomicBoard, Chess::LosersBoard, and Chess::RestrictedMoveBoard.

virtual void Chess::Board::vMakeMove ( const Move move,
BoardTransition transition 
)
protectedpure virtual

Makes move on the board.

This function is called by makeMove(), and should take care of everything except changing the side to move and updating the move history.

Details about piece movement, promotions, captures, drops, etc. should be stored in transition. If transition is 0 then it should be ignored.

Implemented in Chess::WesternBoard, Chess::MakrukBoard, Chess::SimplifiedGryphonBoard, Chess::SittuyinBoard, Chess::OukBoard, Chess::SeirawanBoard, Chess::CrazyhouseBoard, Chess::PlacementBoard, Chess::AndernachBoard, Chess::NCheckBoard, Chess::GryphonBoard, Chess::GiveawayBoard, Chess::PocketKnightBoard, Chess::AtomicBoard, and Chess::RifleBoard.

virtual bool Chess::Board::vSetFenString ( const QStringList fen)
protectedpure virtual

Sets the board according to a FEN string.

This function is called by setFenString(). The board state, side to move and hand pieces are handled by the base class. This function reads the rest of the string, if any.

Implemented in Chess::WesternBoard, Chess::MakrukBoard, Chess::SittuyinBoard, Chess::OukBoard, Chess::AseanBoard, Chess::SeirawanBoard, Chess::PlacementBoard, Chess::AntiBoard, Chess::NCheckBoard, Chess::AtomicBoard, and Chess::LosersBoard.

virtual void Chess::Board::vUndoMove ( const Move move)
protectedpure virtual

Reverses move on the board.

This function is called by undoMove() after changing the side to move to the side that made it.

Note
Unlike vMakeMove(), this function doesn't require subclasses to update the zobrist position key.

Implemented in Chess::WesternBoard, Chess::MakrukBoard, Chess::SimplifiedGryphonBoard, Chess::SittuyinBoard, Chess::OukBoard, Chess::SeirawanBoard, Chess::CrazyhouseBoard, Chess::PlacementBoard, Chess::AndernachBoard, Chess::NCheckBoard, Chess::GryphonBoard, Chess::PocketKnightBoard, Chess::AtomicBoard, and Chess::RifleBoard.

virtual int Chess::Board::width ( ) const
pure virtual
void Chess::Board::xorKey ( quint64  key)
inlineprotected

Updates the zobrist position key with key.


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