-3
bool Hand::isFlush()
{
    if(cardVector[0].suit=cardVector[1].suit=cardVector[2].suit=cardVector[3].suit=cardVector[4].suit)return true;
    return false;
}

bool Hand :: isThreeOfKind() 

{


        if (cardVector[4].rank=cardVector[3].rank, // comparing card 5 to 4, then card 5 to 3.
        cardVector[4].rank=cardVector[2].rank)
        {
            return true;
        }

        return false;
         if (cardVector[3].rank=cardVector[2].rank, //comparing card 4 to 3 and the card 4 to 2
            cardVector[3].rank=cardVector[1].rank)
            {
                return true;
            }
         return false;
         if (cardVector[2].rank=cardVector[1].rank,
                    cardVector[2].rank=cardVector[0].rank) //comparing card 3 to 2 and the card 3 to 1
                 {
                        return true;
                 }      
        return false;}



int main ()
{

    cout<< "welcome" << endl;


    Deck deck;
    float flushCount=0;
    float threeKind=0;
    float count= 16000;
        for(int i=0;i<count;i++)
    {
        deck.shuffle();
        Hand hand=deck.dealHand();
        if(hand.isFlush())flushCount++;


    }
        for (int j=0;j<count;j++)
        {
            Hand hand=deck.dealHand();
            if (hand.isThreeOfKind())threeKind++;

        }
    cout << "The amount of flushes in a game run 160000 times is..."<< endl;
    cout << flushCount << endl;
    cout << (flushCount/count)*100 << endl;
    cout << " Your have this many  "<< threeKind << endl; 





    system("pause");
    return 0;
}

当我运行代码时,我得到的值threeKind等于 的值count。我想要做的是在一手 5 张牌中获得三个种类的数量。我觉得里面的逻辑Hand::isThreeOfKind()可能不正确?我试图重复我所做的bool Hand::isFlush()

4

1 回答 1

1

这是未经测试的,因为我没有 Hand、Deck 和大概 Card 的声明,但它应该让您了解一种测试不同手牌的方法。

如上所述,您的代码中有几个问题,例如在 if 检查中不使用==来测试相等性和使用逗号而不是逻辑运算符 ( &&, )。||

一般来说,最好只有一个 Score 函数为每种类型的手牌(high_card、one_pair、two_pair、three_of_a_kind、flush 等)返回不同的值,而不是多次测试每手牌。它可以让您通过分数轻松比较手牌,并大大减少您需要做的重复工作量,例如计算等级和花色。对手牌进行排序也是一个好主意,因为它简化了对顺子的测试,因为一旦您消除了其余的得分手,您就可以测试第一张牌和最后一张牌之间的差距。

bool Hand::isFlush()
{
    for(int i = 1; i < 5; ++i)
    {
        if(cardVector[i].suit != cardVector[0].suit)
        {
            return false;
        }
    }
    return true;
}

bool Hand::isThreeOfKind() 
{
    //This is 16 because I don't know if your ranks start at 0 or 2
    int counts[16] = {0};
    for(int i = 0; i < 5; ++i)
    {
        ++counts[cardVector[i].rank];
    }
    //This is just to give you an idea of how having single score function
    //can eliminate work.  If you only want to test for 3 of a kind then
    //you don't need the pairs and fours tests and counts
    int num_pairs = 0;
    int num_threes = 0;
    int num_fours = 0;
    for(int i = 0; i < 16; ++i)
    {
        if(counts[i] == 2)
        {
            ++num_pairs;
        }
        else if(counts[i] == 3)
        {
            ++num_threes;
        }
        else if(counts[i] == 4)
        {
            ++num_fours;
        }
    }
    if(num_threes == 1)
    {
        return true;
    }
    return false;
}
于 2013-09-18T08:23:32.837 回答