检查高牌和扑克牌对

时间:2015-02-20 17:57:47

标签: c++ poker

我只是想检查卡是否很高,因此可以评估手牌。此外,类似于检查对,直线等。但是现在,这些是递归的,所以我不确定如何检查排名。谢谢你的帮助!

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]));
}

1 个答案:

答案 0 :(得分:1)

  

但是现在,这些是递归的,所以我不确定如何检查等级。

嗯,递归调用中的条件永远不会评估为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)这不是我们在现实生活中使用真实扑克玩扑克时所做的事情吗?

相关问题