Determine poker hands

Hi there I am developing an iOS application to determine poker hands. At the moment my app lets the user pick 5 cards of their choice and my program outputs the five cards which they have selected (e.g. ace of hearts, two of hearts, three of clubs, queen of diamonds, ace of clubs).

I need my program to tell the user the poker hand which they have selected (e.g. full house, two of a kind, pair etc.)
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.
@ OP: What does the class that represents each 'card' instance look like? You're not building off of your last post are you?
Hi thanks for the replies, i want to set up the different types of hands in my hand.m file / hand.h file. I have no idea how to go about this other than this below what I have so far:

if ([self RoyalFlush]) return @"Royal Flush";
if ([self StraightFlush]) return @"Straight Flush";
if ([self fourOfAKind]) return @"Four of a Kind";
if ([self FullHouse]) return @"Full House";
if ([self Flush]) return @"Flush";
if ([self Straight]) return @"Straight";
if ([self ThreeOfAKind]) return @"Three of a Kind";
if ([self TwoPair]) return @"Two Pair";

obviously I have to determine what each of these hands are somewhere but I could use some examples to help me get going.

Thank you

closed account (z05DSL3A)
roldane2345, Did you notice this is a C++ forum not an Objective-C one?
@ Grey Wolf: Thank you for ID'ing the syntax! I was losing my mind on this one. +1 Hero.
Topic archived. No new replies allowed.