ChessPlusPlus  WIP
cplusplus.com Community Project
chesspp::board::Board Class Reference

Manages pieces on a chess board. More...

#include <Board.hpp>

Classes

class  Movements
 Holds the valid movements for all pieces on the board. More...
 

Public Types

using BoardSize_t = config::BoardConfig::BoardSize_t
 
using Position_t = config::BoardConfig::Position_t
 
using Suit = config::BoardConfig::SuitClass_t
 
using Pieces_t = std::set< std::unique_ptr< piece::Piece >>
 
using Movements_t = std::multimap< Pieces_t::const_iterator, Position_t, Pieces_t_const_iterator_compare >
 
using Factory_t = std::map< config::BoardConfig::PieceClass_t, std::function< Pieces_t::value_type(Board &, Position_t const &, Suit const &)>>
 
using MovementsRange = util::Range< Movements_t::const_iterator >
 

Public Member Functions

 Board (config::BoardConfig const &conf)
 Constructs the Board from the given chesspp::config::BoardConfig. More...
 
bool occupied (Position_t const &pos) const noexcept
 Check if a location on the board is occupied by at least one piece. More...
 
auto find (piece::Piece const &p) const noexcept-> Pieces_t::const_iterator
 Given a Piece, obtain its iterator. More...
 
auto begin () const noexcept-> Pieces_t::const_iterator
 Get the beginning iterator of the board pieces. More...
 
auto end () const noexcept-> Pieces_t::const_iterator
 Get the end iterator of the board pieces. More...
 
Movements const & pieceTrajectories () const noexcept
 
MovementspieceTrajectories () noexcept
 
Movements const & pieceCapturings () const noexcept
 
MovementspieceCapturings () noexcept
 
Movements const & pieceCapturables () const noexcept
 
MovementspieceCapturables () noexcept
 
MovementsRange pieceTrajectory (piece::Piece const &p) noexcept
 
MovementsRange pieceCapturing (piece::Piece const &p) noexcept
 
MovementsRange pieceCapturable (piece::Piece const &p) noexcept
 
bool capture (Pieces_t::iterator source, Movements_t::const_iterator target, Movements_t::const_iterator capturable)
 Capture a capturable piece. More...
 
bool move (Pieces_t::iterator source, Movements_t::const_iterator target)
 Move a piece without capturing. More...
 
bool valid (Position_t const &pos) const noexcept
 Check if a position is a valid position that exists on the board. More...
 

Static Public Member Functions

static auto registerPieceClass (Factory_t::key_type const &type, Factory_t::mapped_type ctor) -> Factory_t::iterator
 Register a new type of chess piece. More...
 

Public Attributes

config::BoardConfig const & config
 The chesspp::config::BoardConfig currently in use. More...
 

Detailed Description

Manages pieces on a chess board.

Can be iterated over with a range-based for-loop.

Member Typedef Documentation

using chesspp::board::Board::Factory_t = std::map<config::BoardConfig::PieceClass_t, std::function<Pieces_t::value_type (Board &, Position_t const &, Suit const &)>>
using chesspp::board::Board::Movements_t = std::multimap<Pieces_t::const_iterator, Position_t, Pieces_t_const_iterator_compare>
using chesspp::board::Board::MovementsRange = util::Range<Movements_t::const_iterator>
using chesspp::board::Board::Pieces_t = std::set<std::unique_ptr<piece::Piece>>

Constructor & Destructor Documentation

chesspp::board::Board::Board ( config::BoardConfig const &  conf)

Constructs the Board from the given chesspp::config::BoardConfig.

Parameters
confThe BoardConfig, must outlive this instance.

Member Function Documentation

auto chesspp::board::Board::begin ( ) const -> Pieces_t::const_iterator
inlinenoexcept

Get the beginning iterator of the board pieces.

bool chesspp::board::Board::capture ( Pieces_t::iterator  source,
Movements_t::const_iterator  target,
Movements_t::const_iterator  capturable 
)

Capture a capturable piece.

Parameters
sourceThe piece performing the capture.
targetThe new location of the source piece.
capturableThe location being captured.
Returns
true if the capture was successful, false otherwise.
auto chesspp::board::Board::end ( ) const -> Pieces_t::const_iterator
inlinenoexcept

Get the end iterator of the board pieces.

auto chesspp::board::Board::find ( piece::Piece const &  p) const -> Pieces_t::const_iterator
noexcept

Given a Piece, obtain its iterator.

Parameters
pThe Piece to search for.
Returns
The iterator to the piece, or the end iterator.
bool chesspp::board::Board::move ( Pieces_t::iterator  source,
Movements_t::const_iterator  target 
)

Move a piece without capturing.

Parameters
sourceThe piece being moved.
targetThe new location of the source piece.
Returns
true if the capture was successful, false otherwise.
bool chesspp::board::Board::occupied ( Position_t const &  pos) const
noexcept

Check if a location on the board is occupied by at least one piece.

Parameters
posThe position to check.
Returns
true if at least one piece occupies the position, false otherwise.
auto chesspp::board::Board::pieceCapturable ( piece::Piece const &  p)
noexcept
Movements const& chesspp::board::Board::pieceCapturables ( ) const
inlinenoexcept
Movements& chesspp::board::Board::pieceCapturables ( )
inlinenoexcept
auto chesspp::board::Board::pieceCapturing ( piece::Piece const &  p)
noexcept
Movements const& chesspp::board::Board::pieceCapturings ( ) const
inlinenoexcept
Movements& chesspp::board::Board::pieceCapturings ( )
inlinenoexcept
Movements const& chesspp::board::Board::pieceTrajectories ( ) const
inlinenoexcept
Movements& chesspp::board::Board::pieceTrajectories ( )
inlinenoexcept
auto chesspp::board::Board::pieceTrajectory ( piece::Piece const &  p)
noexcept
static auto chesspp::board::Board::registerPieceClass ( Factory_t::key_type const &  type,
Factory_t::mapped_type  ctor 
) -> Factory_t::iterator
inlinestatic

Register a new type of chess piece.

This should be called via assignment to a global variable in the source file of the chess piece implementation.

Parameters
typeThe unique type of the chess piece.
ctorThe function used to construct new instances of the chess piece.
Returns
The registration receipt, to be stored in the global variable.
bool chesspp::board::Board::valid ( Position_t const &  pos) const
inlinenoexcept

Check if a position is a valid position that exists on the board.

Parameters
posThe position to check for validity.
Returns
true if the position is valid, false otherwise.

Member Data Documentation

config::BoardConfig const& chesspp::board::Board::config

The chesspp::config::BoardConfig currently in use.


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