开发者

Cant figure out why this function never returns the proper result

The function below is supposed to find out when th开发者_如何学Goere are two duplicate cards in this card game I am working on.

int duplicate (struct card hand[])
{
    int i = 0, j = 0, duplicate = 0;

    for (i = 0; i < 5; i++)
    {
        for (j = i + 1; j < 5; j++)
        {
            if (hand[i].suit == hand[j].suit && hand[i].face == hand[j].face)
            {
                duplicate++;
            }
        }
    }
    return duplicate;
}

I cannot figure out why, but when the value of cards within the hand are the same it never adds to the number of duplicate cards.

Im convinced it must be something obvious but I dont see it.

Thanks!


You should let us know what the types for suit and hand are for struct card - that could affect how they should be compared (in particular if they're pointers).

Also, you should let us know what data you're passing in, what result you're getting and what you expect. For example, if you pass in a hand of 5 cards, two of which are the same, it looks to me like you'll get 1 returned (which might be what you expect, but you might be expecting 2 - I don't know). This is because you don't count the card pointed to by i. Again - whether you should depends on what result you want/need.

But another example that almost certainly isn't what you want is if you pass in a hand of 5 cards all of which are the same. You might be expecting a result of 5 (or maybe 4), but what you'll get is 10, because when:

i == 0, you count the 4 dupes
i == 1, you count the next 3 cards as dupes again
i == 2, the next 2 get counted again,

etc...


My guess not knowing anything about the input or the definition of card, is that the == operator isn't doing what you think for suit or face. Are those primitives that can be compared out of the box or pointers/objects that need special attention?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜