Here's an idea. Start by sorting the cards by value (Ace, King, .... 3, 2).
Then write functions isRoyalFlush(sortedHand &), isStraightFlush(sortedHand&),
etc. for each of the hands.
Finally, you can rank them with:
sortedHand = sort(hand);
if (isRoyalFlush(sortedHand)) rank = RoyalFlush;
else if (isFourOfKind(sortedHand) rank = FourOfKind;
else if isStraightFlush(sortedHand) rank = StraightFlush;
etc.
Note that each function can assume that the hand is NOT one of higher rank. So finding two pair is as easy as:
1 2 3 4 5 6 7 8
|
bool isTwoPair(sortedHand &hand)
{
int count = 0;
for (int i=0; i<4; ++i) {
if (hand[i].value == hand[i+1].val) ++count;
}
return count == 2;
}
|
This code wouldn't work if there were 3 of a kind, but if you require that no higher hand exists, the code is pretty easy.