tartan  1.2.1.0
tt::Board Class Referenceabstract

8x8 game board More...

#include <board.hpp>

Inheritance diagram for tt::Board:
[legend]
Collaboration diagram for tt::Board:
[legend]

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 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...
 
virtual Piece::Color setCurrentTurn (Piece::Color col)
 Set current turn Color. More...
 
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...
 
virtual const Piece::TurnmakeTurn (const Piece::Position &from, const Piece::Position &to)=0
 Make the turn. 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...
 
virtual PiececanInsert (Piece *p) const
 Throws an exception if Piece cannot be placed at the Board. More...
 
virtual PieceinsertPiece (Piece *p)
 Place piece on Board. More...
 
virtual void clear ()
 Clears current Board. More...
 
virtual PieceSetT defaultPieceSet () const =0
 Default piece set for the Board. More...
 
virtual bool isEqual (const Board &other) const =0
 Compare 2 Board objects. 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...
 
virtual std::string str () const =0
 String representation of Board. 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...
 
virtual Piecepiece (const std::string &spec) const =0
 Convert std::string Piece spec to actual Piece object pointer. More...
 
begin (T... args)
 
end (T... args)
 
Fill functions

Every fill() function passes the ownership of
recieved Piece objects to underlying Board object.

Functions that take the string Piece specialization allocates new objects with piece() function.

The insertPiece() is used on each produces/recieved Piece object. This function throws corresponding exceptions on errors.

See also
insertPiece(), piece()
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

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

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...
 

Friends

class Piece::Turn
 
bool operator== (const Board &lhs, const Board &rhs)
 Equal to operator. More...
 
bool operator!= (const Board &, const Board &)
 Not equal to operator. More...
 
std::ostreamoperator<< (std::ostream &os, const Board &b)
 Bitwise left shift operator on std::ostream. More...
 

Detailed Description

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 the class.

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

Constructor & Destructor Documentation

◆ Board()

tt::Board::Board ( PieceSetT set)

Consturct empty Board object and populate it with set

Parameters
setset of pieces to populate the Board with

Member Function Documentation

◆ applyTurn()

const Turn * tt::Board::applyTurn ( Piece::Turn turn)
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.

Parameters
turnTurn object to apply

◆ at() [1/2]

Piece *& tt::Board::at ( const Piece::Position pos)

Board member access by Position.

Parameters
postarget Position object
Returns
Piece object pointer at position pos, nullptr if tile is empty.
See also
at()

◆ at() [2/2]

const Piece * tt::Board::at ( const Piece::Position pos) const

Board member access by Position.

Parameters
postarget Position object
Returns
Piece object pointer at position pos, nullptr if tile is empty.
See also
at()

◆ board() [1/2]

BoardT& tt::Board::board ( )
inline

Internal board data structure.

Returns
internal board data structure type object tt::BoardT

◆ board() [2/2]

const BoardT& tt::Board::board ( ) const
inline

Internal board data structure.

Returns
internal board data structure type object tt::BoardT

◆ canInsert()

Piece * tt::Board::canInsert ( Piece p) const
virtual

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

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

Reimplemented in tt::chess::Chessboard.

◆ clear()

void tt::Board::clear ( )
virtual

Clears current Board.

Fills board with nullptrs and deletes every Piece object on the way with delete.

Reimplemented in tt::chess::Chessboard.

◆ currentTurn()

Piece::Color tt::Board::currentTurn ( ) const
inline

Current turn color.

Returns
Color which have to make next turn on Board

◆ defaultPieceSet()

virtual PieceSetT tt::Board::defaultPieceSet ( ) const
pure virtual

Default piece set for the Board.

This function should be reimplemented in child class to match it's game layout.

Returns
defaultPieceSet for current Board implementation

Implemented in tt::chess::Chessboard.

◆ fill() [1/5]

void tt::Board::fill ( )

Fills current Board with default pieces.

The default pieces is given by defaultPieceSet() function.

◆ fill() [2/5]

void tt::Board::fill ( const std::string str)

Fill Board with string representation of Piece objects.

Function allocates memory for new Pieces objects and puts them on board

Parameters
strspace separated set of piece specs
See also
piece(), set()

◆ fill() [3/5]

template<class Iterator >
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.

Template Parameters
IteratorSTL iterator type
Parameters
beginstart of STL container
endend of STL container
See also
set(const std::string) const

◆ fill() [4/5]

void tt::Board::fill ( PieceSetT set)

Fill Board with piece set.

Fills the board with Piece objects in set. *

Parameters
setset of Piece objects to put on Board

◆ fill() [5/5]

void tt::Board::fill ( std::initializer_list< const std::string list)

Fill Board from std::initializer_list<cosnt std::string>

Parameters
arglist of string piece specs @se piece()

◆ getPieceType()

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.

Parameters
possibleSTL contaier of possible types
Returns
selected std::type_index
Exceptions
std::runtime_errorif b_pieceGetter is not set.
See also
b_pieceGetter, setPieceGetter()

◆ history() [1/2]

HistoryT& tt::Board::history ( )
inline

Turns history.

Returns
HistoryT data type object
See also
b_history

◆ history() [2/2]

const HistoryT& tt::Board::history ( ) const
inline

Turns history.

Returns
HistoryT data type object
See also
b_history

◆ insertPiece()

Piece * tt::Board::insertPiece ( Piece p)
virtual

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().

Note
Board object takes the ownership of inserted pieces.
Parameters
pPiece pointer to insert
Returns
p pointer

Reimplemented in tt::chess::Chessboard.

◆ isEqual()

virtual bool tt::Board::isEqual ( const Board other) const
pure virtual

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

Implemented in tt::chess::Chessboard.

◆ makeTurn()

virtual const Piece::Turn* tt::Board::makeTurn ( const Piece::Position from,
const Piece::Position to 
)
pure virtual

Make the turn.

This function shall be reimplemented in child class. It have to call produceTurn() to get started.

Parameters
fromPiece Position to move
toPiece Position of target
Returns
Turn object that has been applied
See also
produceTurn()

Implemented in tt::chess::Chessboard.

◆ movesMade()

std::size_t tt::Board::movesMade ( ) const
inline

Total count of moves made on Board.

Returns
Moves count or history().size()
See also
b_history

◆ operator[]() [1/2]

Piece *& tt::Board::operator[] ( const Piece::Position pos)

Position subscript operator.

Parameters
postarget Position object
Returns
Piece object pointer at position pos, nullptr if tile is empty.
See also
at()

◆ operator[]() [2/2]

const Piece * tt::Board::operator[] ( const Piece::Position pos) const

Position subscript operator.

Parameters
postarget Position object
Returns
Piece object pointer at position pos, nullptr if tile is empty.
See also
at()

◆ piece()

virtual Piece* tt::Board::piece ( const std::string spec) 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.

Parameters
specPiece std::string specification
Returns
pointer to Piece object constructed from spec string

Implemented in tt::chess::Chessboard.

◆ pieceGetter() [1/2]

PieceGetterT& tt::Board::pieceGetter ( )
inline

Get current piece getter functor.

Returns
current Piece getter functor
See also
b_pieceGetter

◆ pieceGetter() [2/2]

const PieceGetterT& tt::Board::pieceGetter ( ) const
inline

Set the Piece getter functor.

Parameters
gpiece getter
See also
b_pieceGetter

◆ placePiece()

Piece * tt::Board::placePiece ( Piece p)
protectedvirtual

Just places the Piece object on the Board.

Warning
This function does not do any checks on Piece object it places. Use with care.
Parameters
pplaced Piece
Returns
p object pointer
See also
canInsert()

◆ possibleMoves() [1/2]

TurnMap tt::Board::possibleMoves ( const Piece p) const
virtual

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

Reimplemented in tt::chess::Chessboard.

◆ possibleMoves() [2/2]

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.

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

◆ produceTurn()

TurnMap tt::Board::produceTurn ( const Piece::Position from,
const Piece::Position to,
Piece::Turn **  turn 
)
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.

Parameters
fromPiece::Position of Piece to move
toPiece::Position to move the Piece to
[in]turnproduced Piece::Turn object pointer selected as valid from returned TurnMap object
Returns
TrunMap object of all possible moves
Exceptions
ex::tile_is_emptyif p.position() is empty on board
ex::piece_in_wrong_colorif p color does not match b_currentTurnColor.
ex::can_not_movep can not move
ex::no_such_movep does not have such move at all.

◆ set() [1/2]

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.

Parameters
strinput string with space separated piece specs
Returns
resulting piece set
See also
piece()

◆ set() [2/2]

template<class Iterator >
Board::PieceSetT tt::Board::set ( Iterator  begin,
Iterator  end 
) const

Make piece set out of STL container of std::string Piece specs.

Template Parameters
IteratorSTL container iterator
Parameters
beginbegin iterator of STL container
endend iterator of STL container
Returns
resulting Piece objects set

◆ setCurrentTurn()

Color tt::Board::setCurrentTurn ( Piece::Color  col)
virtual

Set current turn Color.

Sets the b_currentTurnColor to col.

Parameters
colnew Color value

Reimplemented in tt::chess::Chessboard.

◆ setPieceGetter()

void tt::Board::setPieceGetter ( PieceGetterT  g)
inline

Set the Piece getter functor.

Parameters
gpiece getter
See also
b_pieceGetter

◆ str()

virtual std::string tt::Board::str ( ) const
pure virtual

String representation of Board.

Useful for debugging purposes. Should be reimplemented in child class.

Returns
string of current board state visualized

Implemented in tt::chess::Chessboard.

◆ turnIndex()

std::size_t tt::Board::turnIndex ( ) const
inline

Turn index.

Underlying varriable gets inclremented on Turn::apply(). Acts like a timestamp fo current Board state.

Returns
last Piece::Turn index
See also
b_turnIndex

Friends And Related Function Documentation

◆ operator!=

bool operator!= ( const Board lhs,
const Board rhs 
)
friend

Not equal to operator.

This is a bind to function !lhs.isEqual(rhs).

See also
isEqual()

◆ operator<<

std::ostream& operator<< ( std::ostream os,
const Board b 
)
friend

Bitwise left shift operator on std::ostream.

This function puts the str() function result to os object with << operator.

Parameters
oscalling object
bBoard object for display
Returns
calling os object

◆ operator==

bool operator== ( const Board lhs,
const Board rhs 
)
friend

Equal to operator.

This is a bind to function lhs.isEqual(rhs).

See also
isEqual()

Member Data Documentation

◆ b_capturedPieces

CapturedT tt::Board::b_capturedPieces
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 catures something.

◆ b_currentTurnColor

Piece::Color tt::Board::b_currentTurnColor = Piece::Color::White
protected

Current turn color variable.

See also
currentTurn()

◆ b_history

HistoryT tt::Board::b_history
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().

See also
history(), HistoryT

◆ b_pieceGetter

PieceGetterT tt::Board::b_pieceGetter
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.

◆ b_turnIndex

std::size_t tt::Board::b_turnIndex = 0
protected

Current turn index.

This varriable is incremented each time the Turn::apply() is called on Piece that belongs to current Board object.


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