Eile Mit Weile
Loading...
Searching...
No Matches
Board Class Reference

Data structure for the game board of Eile Mit Weile including operations on it. More...

#include <Board.hpp>

Public Member Functions

 Board ()
 
 ~Board ()
 
std::vector< std::tuple< unsigned, unsigned, unsigned > > ComputePlayableFigures ()
 
bool MakeMove (Player *player, unsigned int figure_idx, unsigned target_space)
 
void MoveHome (Figure *figure, unsigned space_idx)
 
std::optional< ColorGetWinner ()
 
void WriteIntoJson (rapidjson::Value &json, rapidjson::Document::AllocatorType &allocator) const
 
unsigned int ThrowDice ()
 
const std::vector< Space * > & GetSpaces ()
 
void ResetSixCounter ()
 
unsigned int GetSixCounter ()
 
void SetupGame (unsigned int player_count, unsigned int starting_player)
 
bool CheckMove (Player *player, unsigned int start_idx, unsigned int target_idx)
 
void UpdatePlayerColor (unsigned nof_players)
 
unsigned GetLadderIdx (unsigned space_idx)
 
DiceGetDice ()
 
void ResetCurrentPlayableFigures ()
 
std::vector< std::tuple< unsigned, unsigned, unsigned > > GetCurrentPlayableFigures ()
 

Static Public Member Functions

static BoardFromJson (const rapidjson::Value &json)
 

Private Member Functions

 Board (std::vector< Space * > spaces, Dice *dice, int current_player_color_id, std::vector< std::tuple< unsigned, unsigned, unsigned > > current_playable_figures, std::optional< Color > winner)
 

Private Attributes

std::vector< Space * > _spaces
 
Dice_dice
 
Color _current_player_color
 
std::vector< std::tuple< unsigned, unsigned, unsigned > > _current_playable_figures
 
std::optional< Color_winner
 

Detailed Description

Data structure for the game board of Eile Mit Weile including operations on it.

The game logic is implemented in class and it is the main component of the GameState class that gets sent to the clients. Orientation on the board works with space indices that are attributes of Space objects in _spaces. The following convention is used:

  • Index increases in counterclockwise direction
  • Index 0 is the uppermost space of the 3 rightmost spaces (4 spaces before the space where player red moves out of home)
  • Ladder spaces (LadderSpace) for each color are have indices > 67, that is, they come ofter all other spaces and are ordered by color:
    • Red ladder spaces: 68-74
    • Yellow ladder spaces: 75-81
    • Blue ladder spaces: 82-88
    • Green ladder spaces: 89-95
  • The finish area is a single Space derivative (FinishArea) with index 96
  • The home fields are Space derivatives (HomeField) with their indices in 97-100. They are ordered the same way as the ladder spaces and only get added after Board::SetupGame is called, as only then the number of players is known. For 2 players, the yellow and green home fields are not used, whereas for 3 players, only the green home field is not used. Note that this means that the index of a home field of a color depends on the number of players!

Constructor & Destructor Documentation

◆ Board() [1/2]

Board::Board ( )

Constructor: creates a Dice, initializes all member variables, notably _spaces with the appropriate Space derivative

◆ ~Board()

Board::~Board ( )

Destructor: deletes all Space objects in _spaces and the Dice object

◆ Board() [2/2]

Board::Board ( std::vector< Space * >  spaces,
Dice dice,
int  current_player_color_id,
std::vector< std::tuple< unsigned, unsigned, unsigned > >  current_playable_figures,
std::optional< Color winner 
)
private

Deserialization constructor. Used by client to update the board from the json sent by server.

Member Function Documentation

◆ CheckMove()

bool Board::CheckMove ( Player player,
unsigned int  start_idx,
unsigned int  target_idx 
)

return true when a move is valid. Throws an EileMitWeileException otherwise.

◆ ComputePlayableFigures()

std::vector< std::tuple< unsigned, unsigned, unsigned > > Board::ComputePlayableFigures ( )

Computes all playable figures for the current player, formatted as a vector tuples:
\((\text{inital space index of figure}, \text{space index of figure after move}, \text{index of figure})\).
See Board details for explanation on space indices.
The figure id is in 0 to 3 for valid figures. No playable figures is encoded as a single tuple: \((0, 0, 4)\). The return value is also saved in _current_playable_figures.

◆ FromJson()

Board * Board::FromJson ( const rapidjson::Value json)
static

Deserializes a Board object from a rapidjson::Value object. This is used by the client to update its board from the server

◆ GetCurrentPlayableFigures()

std::vector< std::tuple< unsigned int, unsigned int, unsigned int > > Board::GetCurrentPlayableFigures ( )

◆ GetDice()

Dice * Board::GetDice ( )

Returns a pointer to the Dice object (_dice).

◆ GetLadderIdx()

unsigned Board::GetLadderIdx ( unsigned  space_idx)
inline

< Returns the index of the ladder space for the current player color.

◆ GetSixCounter()

unsigned Board::GetSixCounter ( )

Returns the number of sixes rolled in a row from _dice.

◆ GetSpaces()

const std::vector< Space * > & Board::GetSpaces ( )

Returns a reference to the vector of Space pointers.

◆ GetWinner()

std::optional< Color > Board::GetWinner ( )

Returns the Color of the winner if there is one, std::nullopt otherwise.

◆ MakeMove()

bool Board::MakeMove ( Player player,
unsigned int  figure_idx,
unsigned  target_space 
)

Moves a figure of the current player to the target space. Returns true if the move was valid, false otherwise.

◆ MoveHome()

void Board::MoveHome ( Figure figure,
unsigned  space_idx 
)

Moves a figure to its home field.

◆ ResetCurrentPlayableFigures()

void Board::ResetCurrentPlayableFigures ( )

Resets _current_playable_figures to an empty vector.

◆ ResetSixCounter()

void Board::ResetSixCounter ( )

◆ SetupGame()

void Board::SetupGame ( unsigned int  player_count,
unsigned int  starting_player 
)

Sets up the board for a new game. Notably, it allocates the necesary HomeFields in _spaces.

◆ ThrowDice()

unsigned Board::ThrowDice ( )

Throws the dice and returns the result. Calls _dice->RollDice. Also calls Board::ComputePlayableFigures.

◆ UpdatePlayerColor()

void Board::UpdatePlayerColor ( unsigned  nof_players)

Updates the current player color. This happens when a new player starts a turn.

◆ WriteIntoJson()

void Board::WriteIntoJson ( rapidjson::Value json,
rapidjson::Document::AllocatorType &  allocator 
) const

Serializes a Board object into a rapidjson::Value object. This is used by the server to send the board to the client.

Member Data Documentation

◆ _current_playable_figures

std::vector<std::tuple<unsigned, unsigned, unsigned> > Board::_current_playable_figures
private

vector of tuples of playable figures for the current player.

◆ _current_player_color

Color Board::_current_player_color
private

the color of the current player whose turn it is.

◆ _dice

Dice* Board::_dice
private

pointer to the Dice object.

◆ _spaces

std::vector<Space*> Board::_spaces
private

vector of pointers to all Space derivative objects.

◆ _winner

std::optional<Color> Board::_winner
private

the color of the winner if there is one, std::nullopt otherwise.


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