tartan 1.2.2.1
Chess API
|
8x8 game board More...
#include <board.hpp>
Public Types | |
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 | |
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. | |
virtual Piece::Color | setCurrentTurn (Piece::Color col) |
Set current turn Color. | |
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. | |
virtual const Piece::Turn * | makeTurn (const Piece::Position &from, const Piece::Position &to)=0 |
Make the turn. | |
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. | |
virtual Piece * | canInsert (Piece *p) const |
Throws an exception if Piece cannot be placed at the Board. | |
virtual Piece * | insertPiece (Piece *p) |
Place piece on Board. | |
virtual void | clear () |
Clears current Board. | |
virtual PieceSetT | defaultPieceSet () const =0 |
Default piece set for the Board. | |
virtual bool | isEqual (const Board &other) const =0 |
Compare 2 Board objects. | |
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. | |
virtual std::string | str () const =0 |
String representation of Board. | |
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. | |
virtual Piece * | piece (const std::string &spec) const =0 |
Convert std::string Piece spec to actual Piece object pointer. | |
T | begin (T... args) |
T | end (T... args) |
Fill functions | |
Every fill() function passes the ownership of Functions that take the string Piece specialization allocates new objects with piece() function. The insertPiece() is used on each produced/recieved Piece object. This function throws corresponding exceptions on errors.
| |
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 | |
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 | |
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. | |
Friends | |
class | Piece::Turn |
bool | operator== (const Board &lhs, const Board &rhs) |
Equal to operator. | |
bool | operator!= (const Board &, const Board &) |
Not equal to operator. | |
std::ostream & | operator<< (std::ostream &os, const Board &b) |
Bitwise left shift operator on std::ostream. | |
8x8 game board
This class is used to host Piece objects and should be inherited to use. Also You have reimlemtent all pure virtual methods to use it.
Class exceptions are defined in tartan/board/exceptions.hpp.
tt::Board::Board | ( | PieceSetT & | set | ) |
|
protectedvirtual |
Apply valid turn.
This function should describe things to do before the Turn::apply(int) is called. Current implementation adds Turn::capture() to b_capturedPieces, then flips b_currentTurnColor to opposite.
turn | Turn object to apply |
Piece *& tt::Board::at | ( | const Piece::Position & | pos | ) |
const Piece * tt::Board::at | ( | const Piece::Position & | pos | ) | const |
|
inline |
Internal board data structure.
|
inline |
Internal board data structure.
Throws an exception if Piece cannot be placed at the Board.
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 |
Reimplemented in tt::chess::Chessboard.
|
virtual |
Clears current Board.
Fills board with nullptr
s and deletes every Piece object on the way with delete
.
Reimplemented in tt::chess::Chessboard.
|
inline |
Current turn color.
|
pure virtual |
Default piece set for the Board.
This function should be reimplemented in child class to match it's game layout.
Implemented in tt::chess::Chessboard.
void tt::Board::fill | ( | ) |
Fills current Board with default pieces.
The default pieces is given by defaultPieceSet() function.
void tt::Board::fill | ( | const std::string & | str | ) |
void tt::Board::fill | ( | Iterator | begin, |
Iterator | end | ||
) |
Fill Board from STL container of pointers to Piece objects.
Calls the set(const std::string) const to produce PieceSetT, then fill(PieceSetT) to actually fill the Board.
Iterator | STL iterator type |
begin | start of STL container |
end | end of STL container |
void tt::Board::fill | ( | PieceSetT & | set | ) |
void tt::Board::fill | ( | std::initializer_list< const std::string > | list | ) |
Board::PieceTypesRetT tt::Board::getPieceType | ( | Board::PieceTypesArgT | types | ) |
Get user provided Piece std::type_index.
This function calls the b_pieceGetter functor that were set via setPieceGetter(). If b_pieceGetter were not set and this function were called, the std::runtime_error exception is thrown.
possible | STL contaier of possible types |
std::runtime_error | if b_pieceGetter is not set. |
|
inline |
|
inline |
Place piece on Board.
Default implementation checks if p
can be placed on Board with canInsert(), if it can, then it places the p
in the current Board object with placePiece().
p | Piece pointer to insert |
p
pointer Reimplemented in tt::chess::Chessboard.
|
pure virtual |
Compare 2 Board objects.
other | Board object to compare with |
true
if corresponding tiles have equal adress or cast to same Piece type, false
otherwise Implemented in tt::chess::Chessboard.
|
pure virtual |
Make the turn.
This function shall be reimplemented in child class. It have to call produceTurn() to get started.
Implemented in tt::chess::Chessboard.
|
inline |
Piece *& tt::Board::operator[] | ( | const Piece::Position & | pos | ) |
const Piece * tt::Board::operator[] | ( | const Piece::Position & | pos | ) | const |
|
pure virtual |
Convert std::string Piece spec to actual Piece object pointer.
This function should convert in-game std::string notation for Piece objects to actual objects.
For example, in chess game the qc3 means the black Queen at c3, Qd5 means the white Queen on d5.
spec | Piece std::string specification |
spec
string Implemented in tt::chess::Chessboard.
|
inline |
|
inline |
Just places the Piece object on the Board.
p | placed Piece |
p
object pointer 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 in tt::chess::Chessboard, and tt::chess::Chessboard.
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 |
|
protected |
Make Turn object based on from
and to
Function checks selected Piece object for empty tile, piece in wrong color, no possible moves, no moves at all. If some of the basic conditions aren't satisfied, corresponding exception is thrown. Then it makes basic Turn object from selected Piece Piece::TurnMap. Function should be called from reimplemented makeTurn(), and alter Turn acceptance logic.
from | Piece::Position of Piece to move | |
to | Piece::Position to move the Piece to | |
[in] | turn | produced Piece::Turn object pointer selected as valid from returned TurnMap object |
ex::tile_is_empty | if p.position() is empty on board |
ex::piece_in_wrong_color | if `p` color does not match b_currentTurnColor. |
ex::can_not_move | `p` can not move |
ex::no_such_move | `p` does not have such move at all. |
Board::PieceSetT tt::Board::set | ( | const std::string & | str | ) | const |
Produce piece set from std::string.
Function uses piece() to allocate actual Pice object from std::string spec.
str | input string with space separated piece specs |
Board::PieceSetT tt::Board::set | ( | Iterator | begin, |
Iterator | end | ||
) | const |
Make piece set out of STL container of std::string Piece specs.
Iterator | STL container iterator |
begin | begin iterator of STL container |
end | end iterator of STL container |
|
virtual |
Set current turn Color.
Sets the b_currentTurnColor to col
.
col | new Color value |
Reimplemented in tt::chess::Chessboard.
|
inline |
|
pure virtual |
String representation of Board.
Useful for debugging purposes. Should be reimplemented in child class.
Implemented in tt::chess::Chessboard.
|
inline |
Turn index.
Underlying varriable gets inclremented on Turn::apply(). Acts like a timestamp fo current Board state.
|
friend |
Bitwise left shift operator on std::ostream.
This function puts the str() function result to os
object with <<
operator.
os | calling object |
b | Board object for display |
os
object
|
protected |
List of captured pieces.
Pieces that have retired and have been removed from Board should go to this list. The default applyTurn() function does that if turn captures something.
|
protected |
Current turn color variable.
|
protected |
Board history of applied Turn objects.
All turn objects that have been passed to applyTurn() function go to that list by default with help of Turn::clone().
|
protected |
Piece getter variable.
This variable keeps the functor for getting the new user-reuqested Piece std::type_index.
This could be useful for example in chess game, when pawn reaches the end of the board and player have to choose the piece to exchange it for.
|
protected |
Current turn index.
This varriable is incremented each time the Turn::apply() is called on Piece that belongs to current Board object.