检查扑克牌的大牌和对子
Check high card and pairs for poker hand
我只是想检查卡片是否高,以便评估手牌。此外,类似于检查对、直线等。但现在,这些是递归的,所以我不确定如何检查等级。感谢帮助!!
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.
头文件
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);
};
这里也是设置平台的地方:
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.
嗯,递归调用中的条件永远不会计算为 false
bool Hand::is_straight() {
if (!(is_straight())) // <<<<
return false;
m_high_rank = find_high_rank();
return true; // <<<< Should have some condition here instead
}
我不太明白,为什么您首先要尝试通过递归函数调用来解决这个问题。
简单地排序(例如使用 std::sort()
1)你的 vector<Card> m_hand;
按排名(value
),并检查所有排名是否连续降序,将有资格获得 直手 例如(排序 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) 这不就是我们在现实生活中用真牌打扑克吗?
我只是想检查卡片是否高,以便评估手牌。此外,类似于检查对、直线等。但现在,这些是递归的,所以我不确定如何检查等级。感谢帮助!!
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.
头文件
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);
};
这里也是设置平台的地方:
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.
嗯,递归调用中的条件永远不会计算为 false
bool Hand::is_straight() {
if (!(is_straight())) // <<<<
return false;
m_high_rank = find_high_rank();
return true; // <<<< Should have some condition here instead
}
我不太明白,为什么您首先要尝试通过递归函数调用来解决这个问题。
简单地排序(例如使用 std::sort()
1)你的 vector<Card> m_hand;
按排名(value
),并检查所有排名是否连续降序,将有资格获得 直手 例如(排序 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) 这不就是我们在现实生活中用真牌打扑克吗?