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

Holds the state of one game. More...

#include <GameState.hpp>

Inheritance diagram for GameState:
UniqueSerializable Serializable ServerGameState

Public Member Functions

 GameState ()
 
 GameState (std::string id)
 
 GameState (std::string id, Board *board, std::vector< Player * > &players, SerializableValue< bool > *is_started, SerializableValue< bool > *is_finished, SerializableValue< int > *current_player_idx, SerializableValue< int > *play_direction)
 
 ~GameState ()
 
bool IsFull ()
 
bool IsStarted ()
 
bool IsFinished ()
 
bool IsPlayerInGame (Player *player)
 
bool IsAllowedToPlayNow (Player *player)
 
std::vector< Player * > & GetPlayers ()
 
PlayerGetCurrentPlayer ()
 
int GetPlayerIndex (Player *player)
 
BoardGetBoard ()
 
void WriteIntoJson (rapidjson::Value &json, rapidjson::Document::AllocatorType &allocator) const override
 
unsigned int ThrowDice (Player *player, std::string string)
 
- Public Member Functions inherited from UniqueSerializable
std::string GetId () const
 
void WriteIntoJson (rapidjson::Value &json, rapidjson::MemoryPoolAllocator< rapidjson::CrtAllocator > &allocator) const override
 
- Public Member Functions inherited from Serializable
virtual rapidjson::DocumentToJson () const
 

Static Public Member Functions

static GameStateFromJson (const rapidjson::Value &json)
 

Protected Attributes

Board_board
 
std::vector< Player * > _players
 
SerializableValue< bool > * _is_started
 
SerializableValue< bool > * _is_finished
 
SerializableValue< int > * _current_player_idx
 
SerializableValue< int > * _play_direction
 
- Protected Attributes inherited from UniqueSerializable
std::string id
 

Static Protected Attributes

static const int _max_nof_players = 4
 
static const int _min_nof_players = 2
 

Additional Inherited Members

- Protected Member Functions inherited from UniqueSerializable
 UniqueSerializable ()
 
 UniqueSerializable (std::string id)
 

Detailed Description

Holds the state of one game.

A derived class ServerGameState is used on the server side to execute game state modifications, while the client uses this class only to display the game state.

Constructor & Destructor Documentation

◆ GameState() [1/3]

GameState::GameState ( )

Empty Constructor. Runs UniqueSerializable empty constructor, creates Board and defines all other protected vars corresponding to an unstarted game with no players.

◆ GameState() [2/3]

GameState::GameState ( std::string  id)
explicit

Runs UniqueSerializable constructor with id, creates Board and defines all other protected vars corresponding to an unstarted game with no players.

◆ GameState() [3/3]

GameState::GameState ( std::string  id,
Board board,
std::vector< Player * > &  players,
SerializableValue< bool > *  is_started,
SerializableValue< bool > *  is_finished,
SerializableValue< int > *  current_player_idx,
SerializableValue< int > *  play_direction 
)

Runs UniqueSerializable constructor with id, This constructor is used by the client to obtain a GameState (deserialization from json).

◆ ~GameState()

GameState::~GameState ( )

Destructor. Deletes all dynamically allocated members and sets them to nullptr.

Member Function Documentation

◆ FromJson()

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

Creates a GameState from a json object using the deserialization constructor.

◆ GetBoard()

Board * GameState::GetBoard ( )

Returns a pointer to the _board.

◆ GetCurrentPlayer()

Player * GameState::GetCurrentPlayer ( )

Returns a pointer to the Player that is currently allowed to play.

◆ GetPlayerIndex()

int GameState::GetPlayerIndex ( Player player)

Returns the index of the Player in _players. returns -1 if that player can't be found.

◆ GetPlayers()

std::vector< Player * > & GameState::GetPlayers ( )

Returns a reference to _players.

◆ IsAllowedToPlayNow()

bool GameState::IsAllowedToPlayNow ( Player player)

Returns true if the Player is allowed to play now.

◆ IsFinished()

bool GameState::IsFinished ( )

Returns true if the game has finished.

◆ IsFull()

bool GameState::IsFull ( )

Returns true if the game has reached the maximum number of players.

◆ IsPlayerInGame()

bool GameState::IsPlayerInGame ( Player player)

Returns true if the Player is currently in the game.

◆ IsStarted()

bool GameState::IsStarted ( )

Returns true if the game has started.

◆ ThrowDice()

unsigned int GameState::ThrowDice ( Player player,
std::string  string 
)

Throws the dice for the player and returns the result.

◆ WriteIntoJson()

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

Writes the GameState into a json object.

Implements Serializable.

Member Data Documentation

◆ _board

Board* GameState::_board
protected

The board on which the game is played (see Board)

◆ _current_player_idx

SerializableValue<int>* GameState::_current_player_idx
protected

Index of the player that is currently allowed to play. Wrapped in SerializableValue to allow tcp communication via json format

◆ _is_finished

SerializableValue<bool>* GameState::_is_finished
protected

Indicates whether the game has finished. Wrapped in SerializableValue to allow tcp communication via json format

◆ _is_started

SerializableValue<bool>* GameState::_is_started
protected

Indicates whether the game has started. Wrapped in SerializableValue to allow tcp communication via json format

◆ _max_nof_players

const int GameState::_max_nof_players = 4
staticprotected

At most 4 players can play in a single game

◆ _min_nof_players

const int GameState::_min_nof_players = 2
staticprotected

At least 2 players are needed to start a game

◆ _play_direction

SerializableValue<int>* GameState::_play_direction
protected

1 or -1 depending on which direction is played in. The current implementation always sets this to 1. Wrapped in SerializableValue to allow tcp communication via json format

◆ _players

std::vector<Player*> GameState::_players
protected

Holds pointers to players that are currently in the game (see Player)


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