tartan 1.2.2.1
Chess API
|
Chess game board. More...
#include <chess.hpp>
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::Turn * | makeTurn (const Piece::Position &from, const Piece::Position &to) override |
Make turn on Chessboard. | |
virtual Piece * | piece (const std::string &spec) const override |
Convert std::string Piece spec to actual Piece object pointer. | |
virtual Piece * | canInsert (Piece *p) const override |
Throws an exception if Piece cannot be placed at the Board. | |
virtual Piece * | insertPiece (Piece *p) override |
Place piece on Board. | |
virtual void | clear () override |
Clear current Chessboard. | |
virtual bool | isEqual (const Board &) const override |
Compare 2 Board objects. | |
virtual std::string | str () const override |
String represeantation of current Chessboard. | |
virtual Piece::Color | setCurrentTurn (Piece::Color) override |
Set current turn Color. | |
virtual Piece::TurnMap | possibleMoves (const Piece *) const override |
Possible moves of a Piece by it's adress. | |
const King * | whiteKing () const |
Current White King. | |
King * | whiteKing () |
Current White King. | |
const King * | blackKing () const |
Current Black King. | |
King * | blackKing () |
Current Black King. | |
King * | currentKing () |
King that has the color of currentTurn() | |
const King * | currentKing () const |
King that has the color of currentTurn() | |
const King * | currentEnemyKing () const |
King that has the color opposite to currentTurn() | |
King * | currentEnemyKing () |
King that has the color opposite to currentTurn() | |
PieceSetT | defaultPieceSet () const override |
Get default chessboard Piece set. | |
Board () | |
Construct empty Board object. | |
Board (PieceSetT &set) | |
Consturct empty Board object and populate it with set | |
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. | |
Piece::TurnMap | possibleMoves (const Piece::Position &pos) const |
Possible moves of a Piece by it's location on board. | |
Public Member Functions inherited from tt::Board | |
Board () | |
Construct empty Board object. | |
Board (PieceSetT &set) | |
Consturct empty Board object and populate it with set | |
Board (Board &&)=default | |
Move constructor. | |
Board & | operator= (Board &&)=default |
Move assignment operator. | |
Board (const Board &)=delete | |
Board & | operator= (const Board &)=delete |
Piece::Color | currentTurn () const |
Current turn color. | |
Piece::TurnMap | possibleMoves (const Piece::Position &pos) const |
Possible moves of a Piece by it's location on board. | |
const BoardT & | board () const |
Internal board data structure. | |
BoardT & | board () |
Internal board data structure. | |
HistoryT & | history () |
Turn history. | |
const HistoryT & | history () const |
Turn history. | |
std::size_t | movesMade () const |
Total count of moves made on Board. | |
std::size_t | turnIndex () const |
Turn index. | |
Piece *& | operator[] (const Piece::Position &pos) |
Position subscript operator. | |
const Piece * | operator[] (const Piece::Position &pos) const |
Position subscript operator. | |
Piece *& | at (const Piece::Position &pos) |
Board member access by Position. | |
const Piece * | at (const Piece::Position &pos) const |
Board member access by Position. | |
void | setPieceGetter (PieceGetterT g) |
Set the Piece getter functor. | |
PieceGetterT & | pieceGetter () |
Get current piece getter functor. | |
const PieceGetterT & | pieceGetter () const |
Set the Piece getter functor. | |
PieceTypesRetT | getPieceType (PieceTypesArgT possible) |
Get user provided Piece std::type_index. | |
PieceSetT | set (const std::string &str) const |
Produce piece set from std::string. | |
template<class Iterator > | |
PieceSetT | set (Iterator begin, Iterator end) const |
Make piece set out of STL container of std::string Piece specs. | |
T | begin (T... args) |
T | end (T... args) |
void | fill () |
Fills current Board with default pieces. | |
void | fill (PieceSetT &set) |
Fill Board with piece set. | |
void | fill (const std::string &str) |
Fill Board with string representation of Piece objects. | |
template<class Iterator > | |
void | fill (Iterator begin, Iterator end) |
Fill Board from STL container of pointers to Piece objects. | |
void | fill (std::initializer_list< const std::string > list) |
Fill Board from std::initializer_list<cosnt std::string> | |
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. | |
Protected Member Functions inherited from tt::Board | |
virtual Piece * | placePiece (Piece *p) |
Just places the Piece object on the Board. | |
Piece::TurnMap | produceTurn (const Piece::Position &from, const Piece::Position &to, Piece::Turn **turn) |
Make Turn object based on from and to | |
virtual const Piece::Turn * | applyTurn (Piece::Turn *turn) |
Apply valid turn. | |
Protected Attributes | |
King * | c_whiteKing = nullptr |
White King object. | |
King * | c_blackKing = nullptr |
Black King object. | |
King * | c_currentKing = nullptr |
Current King object. | |
King * | c_currentEnemyKing = nullptr |
Current enemy King object. | |
Protected Attributes inherited from tt::Board | |
Piece::Color | b_currentTurnColor = Piece::Color::White |
Current turn color variable. | |
CapturedT | b_capturedPieces |
List of captured pieces. | |
HistoryT | b_history |
Board history of applied Turn objects. | |
PieceGetterT | b_pieceGetter |
Piece getter variable. | |
std::size_t | b_turnIndex = 0 |
Current turn index. | |
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 chess wikipedia page
Class exceptions are described in tartan/chess/exceptions.hpp.
enum tt::chess::Chessboard::Mode : int |
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. |
|
inline |
|
inline |
tt::Board::Board | ( | PieceSetT & | set | ) |
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.
p | checked Piece object |
p
object ex::foreign_piece | if `p` belongs to other Board already |
ex::position_is_taken | if tile at p.position() is taken |
ex::duplicate_king | inserted King witch such color already present |
Reimplemented from tt::Board.
|
overridevirtual |
Clear current Chessboard.
Resets the c_currentEnemyKing, c_currentKing, c_blackKing, c_whiteKing.
Fills board with nullptr
s and deletes every Piece object on the way with delete
.
Reimplemented from tt::Board.
|
inline |
King that has the color opposite to currentTurn()
|
inline |
King that has the color opposite to currentTurn()
|
inline |
King that has the color of currentTurn()
|
inline |
King that has the color of currentTurn()
|
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 |
Implements tt::Board.
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.
p | inserted Piece object |
ex::duplicate_king | `p` is a King object of color and king of that color already been set |
Reimplemented from tt::Board.
|
overridevirtual |
|
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.
from | Position at which the moving Piece is located. |
to | Position at which moving Piece will end up |
ex::check | if c_currentKing King::check() will return true after this move. |
ex::checkmate | if c_currentKing is under checkmate. |
Implements tt::Board.
|
protected |
Marks Tunr objects that will put current King in check.
[in,out] | map | input TurnMap of Turn objects to check if they lead to check |
|
overridevirtual |
Convert std::string Piece spec to actual Piece object pointer.
The spec
has following syntax:
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); |
spec | string represeantation of chess Piece |
spec
description Implements tt::Board.
|
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.
p | pointer to desired Piece |
ex::null_piece | if `p` is `nullptr` |
ex::foreign_piece | if `p` does not belong to this Board object |
Reimplemented from tt::Board.
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.
p | pointer to desired Piece |
ex::null_piece | if `p` is `nullptr` |
ex::foreign_piece | if `p` does not belong to this Board object |
Reimplemented from tt::Board.
TurnMap tt::Board::possibleMoves | ( | const Piece::Position & | pos | ) | const |
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.
pos | Position of desired Piece on board |
|
overridevirtual |
Set current turn Color.
Selects proper c_currentKing, c_currentEnemyKing.
Sets the b_currentTurnColor to col
.
col | new Color value |
Reimplemented from tt::Board.
|
overridevirtual |
String represeantation of current Chessboard.
Implements tt::Board.
|
inline |
|
inline |
|
protected |
Black King object.
|
protected |
Current enemy King object.
opposite to currentKing(). King object that will make Turn after the currentKing().
|
protected |
|
protected |
White King object.