Wizard
Software Engineering Project - Wizard
Loading...
Searching...
No Matches
trick Class Reference

Represents the trick in the game. More...

#include <trick.h>

Inheritance diagram for trick:
unique_serializable serializable

Public Member Functions

 trick ()
 Constructs a new trick object.
 
 trick (const std::string &id)
 Constructs a new trick object (from_diff).
 
 trick (const std::string &id, const std::vector< std::pair< card *, player * > > &cards, serializable_value< int > *trick_color, serializable_value< int > *trump_color)
 Constructs a new trick object during deserialization.
 
 trick (int trump)
 Constructs a new trick object.
 
 ~trick () override
 Destructs a trick object.
 
int get_trick_color () const
 Gets the trick color.
 
std::vector< std::pair< card *, player * > > get_cards_and_players () const
 Gets the played cards and players who played them as pairs.
 
int get_trump_color () const
 Gets the trump color.
 
void set_up_round (int trump, std::string &err)
 Sets up a trick.
 
playerwrap_up_trick (std::string &err) const
 Wraps up a trick at the end of a trick or round.
 
bool add_card (card *played_card, player *current_player, std::string &err)
 Adds a card to the trick.
 
void set_trick_color (int color) const
 Sets the trick color.
 
void write_into_json (rapidjson::Value &json, rapidjson::Document::AllocatorType &allocator) const override
 Serializes a trick object into a json object.
 
- Public Member Functions inherited from unique_serializable
std::string get_id () const
 
virtual void write_into_json (rapidjson::Value &json, rapidjson::MemoryPoolAllocator< rapidjson::CrtAllocator > &allocator) const override
 
- Public Member Functions inherited from serializable
virtual rapidjson::Document * to_json () const
 

Static Public Member Functions

static trickfrom_json (const rapidjson::Value &json)
 Deserializes a trick object from a json object.
 

Additional Inherited Members

- Protected Member Functions inherited from unique_serializable
 unique_serializable (std::string id)
 
- Protected Attributes inherited from unique_serializable
std::string _id
 

Detailed Description

Represents the trick in the game.

This class encapsulates all information about a trick, including the trick color (suit of the trick), the trump color, and the cards played in the trick.

Every round consists of round-number-many tricks. In each trick, each player plays exactly one card. The first played color determines the trick color, and other players must play cards of that color if they hold any (exception: wizards and jesters can always be played). Each trick has one winner, which is determined by the player who played the first wizard, or by the player who played the highest trump color card, or by the player who played the highest trick color card (in that order).

Constructor & Destructor Documentation

◆ trick() [1/3]

trick::trick ( const std::string & id)
explicit

Constructs a new trick object (from_diff).

Parameters
idThe trick's id.

◆ trick() [2/3]

trick::trick ( const std::string & id,
const std::vector< std::pair< card *, player * > > & cards,
serializable_value< int > * trick_color,
serializable_value< int > * trump_color )

Constructs a new trick object during deserialization.

Parameters
idThe trick's id.
cardsThe trick's cards.
trick_colorThe trick color.
trump_colorThe trump color.

◆ trick() [3/3]

trick::trick ( int trump)
explicit

Constructs a new trick object.

Parameters
trumpThe trump color.

Member Function Documentation

◆ add_card()

bool trick::add_card ( card * played_card,
player * current_player,
std::string & err )

Adds a card to the trick.

Parameters
played_cardThe card that is played.
current_playerThe player who played the card.
errThe error message updated in case something does not work.
Returns
A boolean indicating whether the card could be added to the trick or not.

To keep track of who played which card, this function adds both the card that has been played and the player who played the card as a pair to the trick. In case there is no trick color yet, the function also determines the trick color.

◆ from_json()

trick * trick::from_json ( const rapidjson::Value & json)
static

Deserializes a trick object from a json object.

Parameters
jsonThe json object containing the trick information.
Returns
A pointer to a new trick object created from the given json object.

◆ get_cards_and_players()

std::vector< std::pair< card *, player * > > trick::get_cards_and_players ( ) const
nodiscard

Gets the played cards and players who played them as pairs.

Returns
Pairs of played cards and players who played them.

◆ get_trick_color()

int trick::get_trick_color ( ) const
nodiscard

Gets the trick color.

Returns
The trick color.

◆ get_trump_color()

int trick::get_trump_color ( ) const
nodiscard

Gets the trump color.

Returns
The trump color.

◆ set_trick_color()

void trick::set_trick_color ( int color) const

Sets the trick color.

Parameters
colorThe trick color to be set.

◆ set_up_round()

void trick::set_up_round ( int trump,
std::string & err )

Sets up a trick.

Parameters
trumpThe trump color.
errThe error message updated in case something does not work.

To set up a trick, the played cards are removed from the trick (not the actual card objects, just the pointers), the trick color is set to no color (0), and the trump color is set to the input value.

◆ wrap_up_trick()

player * trick::wrap_up_trick ( std::string & err) const

Wraps up a trick at the end of a trick or round.

Parameters
errThe error message updated in case something does not work.
Returns
The player who won the trick.

To wrap up the trick, this function determines the winner of the trick. This is done considering the cards that have been played during the trick, and considering the trump color. The round is won by the player who played the first wizard, or the first jester in case all players play jesters. If neither wizards nor 4 jesters are played, the player who played the highest card of the trump color wins the trick. If no such card is played, the player with the highest trick color (suit) card wins.

◆ write_into_json()

void trick::write_into_json ( rapidjson::Value & json,
rapidjson::Document::AllocatorType & allocator ) const
overridevirtual

Serializes a trick object into a json object.

Parameters
jsonThe json object for serializing the trick.
allocatorThe json allocator for serializing the trick.

Implements serializable.


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