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

Represents the card deck in the game. More...

#include <deck.h>

Inheritance diagram for deck:
unique_serializable serializable

Public Member Functions

 deck ()
 Constructs a new deck object.
 
 deck (const std::vector< card * > &cards)
 Constructs a new deck object.
 
 ~deck () override
 Destructs a deck object.
 
bool is_empty () const noexcept
 Checks if _remaining_cards is empty, i.e., if all cards are dealt.
 
unsigned int get_number_of_remaining_cards () const noexcept
 Gets the number of remaining cards, i.e., the number of cards not dealt yet.
 
void setup_round ()
 Sets up the deck for a new round.
 
bool draw_cards (const player *player, int round_number, std::string &err)
 Draws a number of random cards based on the current round number and places them into a player's hand.
 
carddraw_trump () const
 Draws a random trump card from the remaining cards.
 
void write_into_json (rapidjson::Value &json, rapidjson::Document::AllocatorType &allocator) const override
 Serializes a deck 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 deckfrom_json (const rapidjson::Value &json)
 Deserializes a deck 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 card deck in the game.

This class encapsulates all information about a card deck, including all cards of the game and the remaining cards not dealt yet.

In the whole game, there is only one object of each possible cards. The _all_cards member of the deck class holds pointers to these cards that are not changed once created and are used to update the _remaining_cards member of the deck class when a new round is set up. The _remaining_cards member helps to keep track of which cards are already dealt and which can still be dealt and used to draw a trump.

Constructor & Destructor Documentation

◆ deck() [1/2]

deck::deck ( )

Constructs a new deck object.

This is the main constructor of the deck class used by the game_state to create a deck for a game. When this constructor is called, all possible cards in Wizard are created and added to the deck. This is the only time actual card objects are created, every card only exists exactly once. In all other instances where cards are removed or added somewhere (e.g., to a player's hand), actually only pointers are added or removed.

◆ deck() [2/2]

deck::deck ( const std::vector< card * > & cards)
explicit

Constructs a new deck object.

Parameters
cardsAll cards of the game.

◆ ~deck()

deck::~deck ( )
override

Destructs a deck object.

As explained for the deck's main constructor, the deck creates the actual card objects, and thus this is the only place where the actual cards are deleted, and not only pointers.

Member Function Documentation

◆ draw_cards()

bool deck::draw_cards ( const player * player,
int round_number,
std::string & err )

Draws a number of random cards based on the current round number and places them into a player's hand.

Parameters
playerThe player to which the cards are added.
round_numberThe current round number determining how many cards are drawn.
errThe error message updated in case something does not work.
Returns
A boolean indicated if drawing the cards worked or not.

This function is used to place cards into the players' hands at the start of each round. The cards are randomly drawn from the remaining cards, added to a player's hand, and then are removed from the remaining cards. The number of cards to be drawn should be the current round number (if round number 0-indexed, the round_number parameter must be the round number + 1).

◆ draw_trump()

card * deck::draw_trump ( ) const
nodiscard

Draws a random trump card from the remaining cards.

Returns
The card used as trump.

This function randomly draws one card from the remaining cards, which is then used as the trump for a round. In the last round, there are no more remaining cards after dealing. In this case, a nullptr is returned. The logic to handle this case further is implemented in the game_state class.

◆ from_json()

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

Deserializes a deck object from a json object.

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

◆ get_number_of_remaining_cards()

unsigned int deck::get_number_of_remaining_cards ( ) const
nodiscardnoexcept

Gets the number of remaining cards, i.e., the number of cards not dealt yet.

Returns
The number of remaining cards.

◆ is_empty()

bool deck::is_empty ( ) const
nodiscardnoexcept

Checks if _remaining_cards is empty, i.e., if all cards are dealt.

Returns
A boolean indicating whether all cards are dealt or not.

All cards are dealt only in the last round of the game.

◆ setup_round()

void deck::setup_round ( )

Sets up the deck for a new round.

Setting up a deck for a new round is done by refreshing the remaining cards from all cards.

◆ write_into_json()

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

Serializes a deck object into a json object.

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

Implements serializable.


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