Skip to content
Snippets Groups Projects
Commit f7c94527 authored by mtarnow's avatar mtarnow
Browse files

change jester from being 15 to being 0

parent 3f0a74fd
No related branches found
No related tags found
2 merge requests!17finalize common,!4Resolve "Update classes card, hand and player (in common)"
......@@ -3,51 +3,61 @@
#include "../player/hand.h"
#include "../../exceptions/WizardException.h"
// constructors
// TODO: check if the asserts are necessary
// constructors (from_diff and deserialization)
card::card(std::string id) : unique_serializable(id) { }
card::card(std::string id, serializable_value<std::pair<int, int>> *val)
: unique_serializable(id), _value(val)
{ }
card::card(std::pair<int, int> val) :
unique_serializable(),
_value(new serializable_value<std::pair<int, int>>(val))
{ }
// constructor
card::card(std::pair<int, int> val) : unique_serializable()
{
assert(val.first <= 14 && val.first >= 0); // check the cards value (or wizard / jester)
assert(val.second <= 4 && val.second >= 0); // check the cards color
_value = new serializable_value<std::pair<int, int>>(val);
}
// destructor
card::~card() = default;
// getter function, returns pair of two ints indicating the number of the card and its color
// getter function, returns a pair of two ints indicating the number of the card and its color
// (see header file for more details)
std::pair<int, int> card::get_value() const noexcept {
return _value->get_value();
}
bool card::can_be_played(const trick* const current_trick, const hand* const current_hand) const noexcept {
// return true if this card can be played according to the game rules:
// if the card is a jester or wizard, it can be played
// return true if this card can be played according to the game rules (checked in that order):
// if there is no trick color yet (trick color is 0), then the card can be played
// if the card is a jester or wizard, then it can be played
// it the card has the same color as the trick color, it can be played
// if the card does not have the same color as the trick color, it can only be played if no other card
// on the players hand has the same color as the trick color
// get values of this card
// get trick color -> if 0, card can be played
int trick_color = current_trick->get_trick_color();
if (trick_color == 0) return true;
// get value of this card
std::pair<int, int> value = this->get_value();
// check if card is wizard or jester
if (std::get<0>(value) > 13) return true;
// check if card is wizard or jester -> if yes, card can be played
if (value.first == 0 || value.first == 14) return true;
// if card is not wizard or jester, compare with trick color
int trick_color = current_trick->get_trick_color();
if (std::get<1>(value) == trick_color) return true;
// if card is not wizard or jester, compare with trick color -> if same, can be played
if (value.second == trick_color) return true;
// TODO: make more efficient if possible
// if card does not have trick color, check other cards on hand
const std::vector<card*> cards = current_hand->get_cards();
for (auto it = cards.begin(); it != cards.end(); ++it)
{
std::pair<int, int> current_value = (*it)->get_value();
std::pair<int, int> hand_value = (*it)->get_value();
// if there is a card on the hand with the trick color, the current card cannot be played
if (std::get<1>(current_value) == trick_color) return false;
if (hand_value.second == trick_color) return false;
}
return true;
......
......@@ -11,8 +11,9 @@ class card : public unique_serializable {
private:
// TODO: make sure that _value can only take numbers from 1 to 15 in first position and from 0 to 4 in second
// _value contains the card number as first int (number between 1 and 13, or wizard (14) / jester (15)),
// and also the color as second int (encoded as 1: yellow, 2: red, 3: green, 4: blue, 0 if wizard or jester)
// _value contains the card number as first int (number between 1 and 13 as the cards value,
// or wizard (14) / jester (0)), and also the color as second int
// (encoded as 1: yellow, 2: red, 3: green, 4: blue, 0 if wizard or jester)
serializable_value<std::pair<int, int>>* _value;
// from_diff constructor
......@@ -20,6 +21,7 @@ private:
// deserialization constructor
card(std::string id, serializable_value<std::pair<int, int>>* val);
public:
// constructor & destructor
card(std::pair<int, int> val);
~card();
......
......@@ -4,6 +4,7 @@
hand::hand() : unique_serializable() { }
// from_diff constructor
hand::hand(std::string id) : unique_serializable(id) { }
// deserialization constructor
......@@ -23,7 +24,6 @@ int hand::get_nof_cards() const {
return _cards.size();
}
const std::vector<card*> hand::get_cards() const {
return _cards;
}
......
......@@ -64,7 +64,8 @@ void player::set_game_id(std::string game_id) {
// getter and setter for score
int player::get_score() const noexcept {
int player::get_score() const noexcept
{
return _score->get_value();
}
......@@ -79,6 +80,7 @@ int player::get_nof_tricks() const noexcept
{
return _nof_tricks->get_value();
}
void player::set_nof_tricks(int nof_tricks)
{
_nof_tricks->set_value(nof_tricks);
......@@ -90,6 +92,7 @@ int player::get_nof_predicted() const noexcept
{
return _nof_predicted->get_value();
}
void player::set_nof_predicted(int nof_predicted)
{
_nof_predicted->set_value(nof_predicted);
......@@ -115,8 +118,9 @@ void player::setup_round(std::string& err) {
_nof_predicted->set_value(0);
_nof_tricks->set_value(0);
delete _hand;
_hand = new hand();
// TODO: implement how the new hand is created
// TODO: check if the set up of new hand makes sense
}
void player::wrap_up_round(std::string &err) {
......
......@@ -13,6 +13,7 @@ private:
serializable_value<std::string>* _player_name; // player's name chosen by the player
serializable_value<int>* _nof_tricks; // number of tricks won in current round
serializable_value<int>* _nof_predicted; // number of predicted tricks in current round
// TODO: add scores vector that saves all scores
serializable_value<int>* _score; // score of the player (total game score)
hand* _hand;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment