I'm simply trying to check if the card is high, so the hand can be evaluated. Also, similar for checking pairs, straight, etc. But right now, these are recursive, so I'm not sure how to check the rank. Thanks for help!!
functions.cpp
int Hand::find_high_rank() const
{
int high_rank = 0;
for (unsigned i = 0; i<m_hand.size(); i++)
high_rank = max(high_rank, m_hand[i].get_rank());
return high_rank;
}
bool Hand::is_straight()
{
if (! (is_straight()))
return false;
m_high_rank = find_high_rank();
return true;
}
//these functions are similar for flush, two pair, full house, etc.
header file
class Card{
public:
Card(int value, char suit) : value(value), suit(suit) {}
Card ();
private:
int value;
char suit;
};
class Deck {
public:
void createDeck();
void shuffleDeck();
Card Draw();
Deck();
void Print() const;
private:
vector<Card> deck;
};
enum hand_kind
{ HIGH_CARD,
ONE_PAIR,
TWO_PAIR,
THREE_OF_A_KIND,
STRAIGHT,
FLUSH,
FULL_HOUSE,
FOUR_OF_A_KIND,
STRAIGHT_FLUSH,
};
class Hand {
vector<Card> m_hand;
hand_kind m_kind;
int m_high_rank;
bool is_straight_flush ();
bool is_four();
bool is_full_house();
bool is_flush ();
bool is_straight ();
bool is_three();
bool is_same_rank (int rank);
bool is_two_pair ();
int find_high_rank () const;
int how_many (int rank) const;
public:
Hand ();
void add_card_to_hand (const Card & card);
hand_kind classify_hand ();
bool operator < (const Hand & rhs) const;
friend ostream & operator << (ostream & os, const Hand & hand);
};
Also here is where the deck is setup:
void Deck::createDeck() {
deck.clear();
static const char suits[] = {'C','D','H','S'};
for (int suit=0; suit < 4; suit++)
for (int val=1; val <=13; val++)
deck.push_back(Card(val,suits[suit]));
}
But right now, these are recursive, so I'm not sure how to check the rank.
Well, the condition in your recursive call will never evaluate to false
bool Hand::is_straight() {
if (!(is_straight())) // <<<<
return false;
m_high_rank = find_high_rank();
return true; // <<<< Should have some condition here instead
}
I dont't really get, why you try to solve this with a recursive function call in 1st place.
Simply sorting (using e.g. std::sort()
1) your vector<Card> m_hand;
by rank (value
), and checking if all ranks, are in contiguous descending order, would qualify for a straight hand for example (with a sorted m_hand
):
bool Hand::is_straight() {
int current_rank = m_hand.begin()->value;
for(std::vector<Card>::const_iterator it = m_hand.begin(),++it;
it != m_hand.end();
++it) {
if(it->value + 1 != current_rank) {
return false; // No straight
}
current_rank = it->value;
}
return true;
}
1) Isn't that what we're doing when playing poker in real life, with real cards?