tartan  1.2.1.0
tt::chess::Chessboard Class Reference

Chess game board. More...

#include <chess.hpp>

Inheritance diagram for tt::chess::Chessboard:
[legend]
Collaboration diagram for tt::chess::Chessboard:
[legend]

Public Types

enum  Mode : int { DefaultMode = 0 , CheckingMode = 1 }
 Modes to use with Turn::apply and Piece::moveMap. More...
 
- Public Types inherited from tt::Board
using CapturedT = std::forward_list< const Piece * >
 Type for list of captured Piece objects that no more on the board.
 
using HistoryT = std::list< const Piece::Turn * >
 Type for internal move history representation.
 
using TurnsT = std::list< std::pair< Piece::Position, Piece::Position > >
 Type for representation a sequence of turns.
 
using PieceSetT = std::list< Piece * >
 Type for representing set of pointers to Piece objects.
 
using PieceTypesArgT = std::list< std::type_index >
 Type used as argument to getPieceType()
 
using PieceTypesRetT = std::type_index
 Type that getPieceType() should return.
 
using PieceGetterT = std::function< PieceTypesRetT(PieceTypesArgT)>
 Type of pieceGetter()
 

Public Member Functions

virtual const Piece::TurnmakeTurn (const Piece::Position &from, const Piece::Position &to) override
 Make turn on Chessboard. More...
 
virtual Piecepiece (const std::string &spec) const override
 Convert std::string Piece spec to actual Piece object pointer. More...
 
virtual PiececanInsert (Piece *p) const override
 Throws an exception if Piece cannot be placed at the Board. More...
 
virtual PieceinsertPiece (Piece *p) override
 Place piece on Board. More...
 
virtual void clear () override
 Clear current Chessboard. More...
 
virtual bool isEqual (const Board &) const override
 Compare 2 Board objects. More...
 
virtual std::string str () const override
 String represeantation of current Chessboard. More...
 
virtual Piece::Color setCurrentTurn (Piece::Color) override
 Set current turn Color. More...
 
virtual Piece::TurnMap possibleMoves (const Piece *) const override
 Possible moves of a Piece by it's adress. More...
 
const KingwhiteKing () const
 Current White King. More...
 
KingwhiteKing ()
 Current White King. More...
 
const KingblackKing () const
 Current Black King. More...
 
KingblackKing ()
 Current Black King. More...
 
KingcurrentKing ()
 King that has the color of currentTurn() More...
 
const KingcurrentKing () const
 King that has the color of currentTurn() More...
 
const KingcurrentEnemyKing () const
 King that has the color opposite to currentTurn() More...
 
KingcurrentEnemyKing ()
 King that has the color opposite to currentTurn() More...
 
PieceSetT defaultPieceSet () const override
 Get default chessboard Piece set. More...
 
 Board ()
 Construct empty Board object.
 
 Board (PieceSetT &set)
 Consturct empty Board object and populate it with set More...
 
 Board (Board &&)=default
 Move constructor.
 
 Board (const Board &)=delete
 
virtual Piece::TurnMap possibleMoves (const Piece *p) const
 Possible moves of a Piece by it's adress. More...
 
Piece::TurnMap possibleMoves (const Piece::Position &pos) const
 Possible moves of a Piece by it's location on board. More...
 
- Public Member Functions inherited from tt::Board
 Board ()
 Construct empty Board object.
 
 Board (PieceSetT &set)
 Consturct empty Board object and populate it with set More...
 
 Board (Board &&)=default
 Move constructor.
 
Boardoperator= (Board &&)=default
 Move assignment operator.
 
 Board (const Board &)=delete
 
Boardoperator= (const Board &)=delete
 
Piece::Color currentTurn () const
 Current turn color. More...
 
Piece::TurnMap possibleMoves (const Piece::Position &pos) const
 Possible moves of a Piece by it's location on board. More...
 
const BoardTboard () const
 Internal board data structure. More...
 
BoardTboard ()
 Internal board data structure. More...
 
HistoryThistory ()
 Turns history. More...
 
const HistoryThistory () const
 Turns history. More...
 
std::size_t movesMade () const
 Total count of moves made on Board. More...
 
std::size_t turnIndex () const
 Turn index. More...
 
Piece *& operator[] (const Piece::Position &pos)
 Position subscript operator. More...
 
const Pieceoperator[] (const Piece::Position &pos) const
 Position subscript operator. More...
 
Piece *& at (const Piece::Position &pos)
 Board member access by Position. More...
 
const Pieceat (const Piece::Position &pos) const
 Board member access by Position. More...
 
void setPieceGetter (PieceGetterT g)
 Set the Piece getter functor. More...
 
PieceGetterTpieceGetter ()
 Get current piece getter functor. More...
 
const PieceGetterTpieceGetter () const
 Set the Piece getter functor. More...
 
PieceTypesRetT getPieceType (PieceTypesArgT possible)
 Get user provided Piece std::type_index. More...
 
PieceSetT set (const std::string &str) const
 Produce piece set from std::string. More...
 
template<class Iterator >
PieceSetT set (Iterator begin, Iterator end) const
 Make piece set out of STL container of std::string Piece specs. More...
 
begin (T... args)
 
end (T... args)
 
void fill ()
 Fills current Board with default pieces. More...
 
void fill (PieceSetT &set)
 Fill Board with piece set. More...
 
void fill (const std::string &str)
 Fill Board with string representation of Piece objects. More...
 
template<class Iterator >
void fill (Iterator begin, Iterator end)
 Fill Board from STL container of pointers to Piece objects. More...
 
void fill (std::initializer_list< const std::string > list)
 Fill Board from std::initializer_list<cosnt std::string> More...
 
void refill ()
 Clear and fill Board with default piece set.
 

Protected Member Functions

void markChecks (Piece::TurnMap &map) const
 Marks Tunr objects that will put current King in check. More...
 
- Protected Member Functions inherited from tt::Board
virtual PieceplacePiece (Piece *p)
 Just places the Piece object on the Board. More...
 
Piece::TurnMap produceTurn (const Piece::Position &from, const Piece::Position &to, Piece::Turn **turn)
 Make Turn object based on from and to More...
 
virtual const Piece::TurnapplyTurn (Piece::Turn *turn)
 Apply valid turn. More...
 

Protected Attributes

Kingc_whiteKing = nullptr
 White King object. More...
 
Kingc_blackKing = nullptr
 Black King object. More...
 
Kingc_currentKing = nullptr
 Current King object. More...
 
Kingc_currentEnemyKing = nullptr
 Current enemy King object. More...
 
- Protected Attributes inherited from tt::Board
Piece::Color b_currentTurnColor = Piece::Color::White
 Current turn color variable. More...
 
CapturedT b_capturedPieces
 List of captured pieces. More...
 
HistoryT b_history
 Board history of applied Turn objects. More...
 
PieceGetterT b_pieceGetter
 Piece getter variable. More...
 
std::size_t b_turnIndex = 0
 Current turn index. More...
 

Detailed Description

Chess game board.

This class implements tt::Board pure virtual fucntions and builds up the famous chess game. The game rules which this class implements are described at [2].

Class exceptions are described in tartan/chess/exceptions.hpp.

Member Enumeration Documentation

◆ Mode

Modes to use with Turn::apply and Piece::moveMap.

This is used by markChecks().

Enumerator
DefaultMode 

Default mode. Act as usual.

CheckingMode 

Apply the turn or produce moveMap as performing the validation for King check.

This is used to counter infinite recursion in King movemap when trying to figure out should it perform castling or not.

Also the Pawn promption move uses it to not promote pawn and just move it to the top of board, because when checking for King checks we don't care about exchange piece type.

Member Function Documentation

◆ blackKing() [1/2]

King* tt::chess::Chessboard::blackKing ( )
inline

Current Black King.

Returns
current black King object
See also
c_blackKing

◆ blackKing() [2/2]

const King* tt::chess::Chessboard::blackKing ( ) const
inline

Current Black King.

Returns
current black King object
See also
c_blackKing

◆ Board()

tt::Board::Board

Consturct empty Board object and populate it with set

Parameters
setset of pieces to populate the Board with

◆ canInsert()

Piece * tt::chess::Chessboard::canInsert ( Piece p) const
overridevirtual

Throws an exception if Piece cannot be placed at the Board.

Checks if the inserted Piece is a King object. If it is, then checks that Chessboard does not already have a King of that color.

Parameters
pchecked Piece object
Returns
p object
Exceptions
ex::foreign_pieceif p belongs to other Board already
ex::position_is_takenif tile at p.position() is taken
ex::duplicate_kinginserted King witch such color already present

Reimplemented from tt::Board.

◆ clear()

void tt::chess::Chessboard::clear ( )
overridevirtual

Clear current Chessboard.

Resets the c_currentEnemyKing, c_currentKing, c_blackKing, c_whiteKing. Fills board with nullptrs and deletes every Piece object on the way with delete.

Reimplemented from tt::Board.

◆ currentEnemyKing() [1/2]

King* tt::chess::Chessboard::currentEnemyKing ( )
inline

King that has the color opposite to currentTurn()

Returns
current enemy King object
See also
c_currentEnemyKing
c_currentKing

◆ currentEnemyKing() [2/2]

const King* tt::chess::Chessboard::currentEnemyKing ( ) const
inline

King that has the color opposite to currentTurn()

Returns
current enemy King object
See also
c_currentEnemyKing
c_currentKing

◆ currentKing() [1/2]

King* tt::chess::Chessboard::currentKing ( )
inline

King that has the color of currentTurn()

Returns
current King object

◆ currentKing() [2/2]

const King* tt::chess::Chessboard::currentKing ( ) const
inline

King that has the color of currentTurn()

Returns
current King object

◆ defaultPieceSet()

Board::PieceSetT tt::chess::Chessboard::defaultPieceSet ( ) const
overridevirtual

Get default chessboard Piece set.

Set contains the next:

Piece Position Color
Rook a1 White
Knight b1 White
Bishop c1 White
Queen d1 White
King e1 White
Bishop f1 White
Knight g1 White
Rook h1 White
Pawn a2 White
Pawn b2 White
Pawn c2 White
Pawn d2 White
Pawn e2 White
Pawn f2 White
Pawn g2 White
Pawn h2 White
Rook a8 Black
Knight b8 Black
Bishop c8 Black
Queen d8 Black
King e8 Black
Bishop f8 Black
Knight g8 Black
Rook h8 Black
Pawn a7 Black
Pawn b7 Black
Pawn c7 Black
Pawn d7 Black
Pawn e7 Black
Pawn f7 Black
Pawn g7 Black
Pawn h7 Black
Returns
default Chessboard Piece set

Implements tt::Board.

◆ insertPiece()

Piece * tt::chess::Chessboard::insertPiece ( Piece p)
overridevirtual

Place piece on Board.

Calls the canInsert(), if it does not throw any exceptions, then it sets the c_whiteKing or c_blackKing and c_currentKing with c_currentEnemyKing Piece.

Parameters
pinserted Piece object
Returns
inserted piece object
Exceptions
ex::duplicate_kingp is a King object of color and king of that color already been set

Reimplemented from tt::Board.

◆ isEqual()

bool tt::chess::Chessboard::isEqual ( const Board rhs) const
overridevirtual

Compare 2 Board objects.

Parameters
otherBoard object to compare with
Returns
true if corresponding tiles have equal adress or cast to same Piece type, false otherwise

Implements tt::Board.

◆ makeTurn()

const Turn * tt::chess::Chessboard::makeTurn ( const Piece::Position from,
const Piece::Position to 
)
overridevirtual

Make turn on Chessboard.

Function calls the Board::produceTurn() function, then it validates for the checks with markChecks(). If the move cannot be performed, corresponding exception is thrown.

Parameters
fromPosition at which the moving Piece is located.
toPosition at which moving Piece will end up
Returns
applied Piece::Turn object if everything went okay
Exceptions
ex::checkif c_currentKing King::check() will return true after this move.
ex::checkmateif c_currentKing is under checkmate.

Implements tt::Board.

◆ markChecks()

void tt::chess::Chessboard::markChecks ( Piece::TurnMap map) const
protected

Marks Tunr objects that will put current King in check.

Parameters
[in,out]mapinput TurnMap of Turn objects to check if they lead to check

◆ piece()

Piece * tt::chess::Chessboard::piece ( const std::string spec) const
overridevirtual

Convert std::string Piece spec to actual Piece object pointer.

The spec has following syntax:

<p><pos>

where <p> is a piece type character, and <pos> is it's Position. If the <p> character is uppercase, Piece::p_color property will be set to Piece::Color::White. Else if the <p> is lowercase, the resulting Piece::p_color will be set to Piece::Color::Black. The <pos> part describes the Piece object position on the board. <pos> orphographic conventions are the same as for tt::Piece::Position(const std::string&) argument.

String specificators examples:

spec Returned object
pd4 new tt::chess::Pawn("d4", tt::Piece::Color::Black);
Kh7 new tt::chess::King("h7", tt::Piece::Color::White);
bc1 new tt::chess::Bishop("c1", tt::Piece::Color::Black);
Rg2 new tt::chess::Rook("g2", tt::Piece::Color::White);
Parameters
specstring represeantation of chess Piece
Returns
newly allocated Piece object that corresponds to spec description

Implements tt::Board.

◆ possibleMoves() [1/3]

Piece::TurnMap tt::chess::Chessboard::possibleMoves ( const Piece p) const
overridevirtual

Possible moves of a Piece by it's adress.

Marks moves that lead to checks with markChecks(). This function returns the final TurnMap object with all Turn members marked as possible or not, so user can see what turns they can perform. Could be useful for GUI board games.

Parameters
ppointer to desired Piece
Exceptions
ex::null_pieceif p is nullptr
ex::foreign_pieceif p does not belong to this Board object
See also
possibleMoves(const Piece::Position&) const

Reimplemented from tt::Board.

◆ possibleMoves() [2/3]

TurnMap tt::Board::possibleMoves

Possible moves of a Piece by it's adress.

This function returns the final TurnMap object with all Turn members marked as possible or not, so user can see what turns they can perform. Could be useful for GUI board games.

Parameters
ppointer to desired Piece
Exceptions
ex::null_pieceif p is nullptr
ex::foreign_pieceif p does not belong to this Board object
See also
possibleMoves(const Piece::Position&) const

◆ possibleMoves() [3/3]

TurnMap tt::Board::possibleMoves

Possible moves of a Piece by it's location on board.

This function calls the possibleMoves(const Piece*) const after resolving the Piece object by it's position.

Parameters
posPosition of desired Piece on board
See also
possibleMoves(const Piece*) const

◆ setCurrentTurn()

Piece::Color tt::chess::Chessboard::setCurrentTurn ( Piece::Color  c)
overridevirtual

Set current turn Color.

Selects proper c_currentKing, c_currentEnemyKing. Sets the b_currentTurnColor to col.

Parameters
colnew Color value

Reimplemented from tt::Board.

◆ str()

std::string tt::chess::Chessboard::str ( ) const
overridevirtual

String represeantation of current Chessboard.

Returns
string with Chessboard and it's pieces

Implements tt::Board.

◆ whiteKing() [1/2]

King* tt::chess::Chessboard::whiteKing ( )
inline

Current White King.

Returns
current white King object
See also
c_whiteKing

◆ whiteKing() [2/2]

const King* tt::chess::Chessboard::whiteKing ( ) const
inline

Current White King.

Returns
current white King object
See also
c_whiteKing

Member Data Documentation

◆ c_blackKing

King* tt::chess::Chessboard::c_blackKing = nullptr
protected

Black King object.

See also
blackKing()

◆ c_currentEnemyKing

King* tt::chess::Chessboard::c_currentEnemyKing = nullptr
protected

Current enemy King object.

opposite to currentKing(). King object that will make Turn after the currentKing().

See also
currentEnemyKing()

◆ c_currentKing

King* tt::chess::Chessboard::c_currentKing = nullptr
protected

Current King object.

King object that will make next Turn.

See also
currentKing()

◆ c_whiteKing

King* tt::chess::Chessboard::c_whiteKing = nullptr
protected

White King object.

See also
whiteKing()

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