hi everyone
i'm new in programing, so don't judge me if i will say something stupid...
i'm trying to write i poker game in C++ but have faced with several problems, the first one is how to determine a winner...
for example i player hand is:
Ace of spades card number is: 39
Four of spades 42
Six of spades 44
Ace of diamonds 0
King of diamonds 12
dealer:
Four of diamonds 3
three of spades 41
Seven of hearts 19
three of hearts 15
Five of diamonds 4
how to determine that player wins...? i could write down all combination... but there is thousands of them...

Recommended Answers

All 4 Replies

You need to write a couple of hand scoring functions. You don't look for all possible combinations, but look at a player's hand and determine what, if any, of the poker hands it constitutes.

In evaluating a hand, you need to look at first the value of the cards, disregarding suit. Is there a pair, three of a kind, four of a kind? If there's a three of a kind, are the remaining cards a pair? If none of those, are the five cards in sequence, to make a straight? Lastly, check if all the cards are of the same suit, for a flush. The results of these last two tests tell if it's a straight flush.

One tip in creating these functions - sort the cards in the hand by value - makes most of these evaluations easier.

That's a start for evaluating a hand. Comparing two hands to determine a winner will add more complexity and further testing if hands are same type. But you can work on that after the basic evaluation is complete.

So, write us some code, show it, and we'll go from there.

I assume that each suit has a specific numbers associated to them; i.e. Diamonds are 0 - 12, Hearts are 15 - 27, etc. With that in mind, you can do some things to help.

Like check if the cards are in the same suit by dividing the card number by 13 (any Diamond = 0, Heart = 1, etc). Or modding the card number by 13 to see if they have a pair/three of a kind/four of a kind (Ace = 0, Jack = 11, etc) or even if they have a straight.

I think those two will along will allow you to do most of the work.

You might consider ranking the suit 2 - Ace so that 2 = 0 and A = 12, that way you can determine which card is highest without making a special case for the ace. 3 of Diamonds is obviously lower the 6 of hearts (dia3%13 = 2, and hea6%13 = 5) but it isn't obvious that the Ace of diamonds is higher than the heart (diaA%13 = 0, and head6%13 = 5).

I would also suggest creating a hand rank like in this image.
http://img2.baltgames.lv/v2/usergalleries/199601.jpg
That way when you determine that player one has a pair of aces, you can mark their hand with a 9 and mark the dealers hand with a 9, see that both contain a pair, then compare the card number and see Ace beats 3.

Hope this helps.

Are you feeling lucky...

I'm gonna go all-in on this; I've written a few ascii table games so I know what ye' are talking aboot here.

There are of course many ways to do this. I would suggest to write specific functions to test every possible winning situation that can occur in the game of poker. Then you can pass players 'hands' into each function. The functions could be designed to return TRUE for each test.

Without getting hot and heavy into each function definition, I'll just give you a general overview of the suggested design:

struct card{
 
     string suite;
     int value;
};

//Function prototypes:
bool is_one_pair(card&);
bool is_two_pair(card&);
bool is_three_of_kind(card&);
bool is_straight(card&);
bool is_flush(card&);
bool is_full_house(card&);
bool is_four_of_kind(card&);
bool is_straight_flush(card&);
void high_card(card&);
  
//create containers that will represent a hand for each player
card player1_hand[5], player2_hand[5], player3_hand[5], player4_hand[5];


//Now the part ye' been askin' aboot

//Somewhere along the line you can do something like this:

if (is_one_pair(player1_hand))
{
     //code to handle hand containing 1 pair
}
else if(is_two_pair(player1_hand))
{
     //code to handle hand containing 2 pair
}
else if(is_three_of_kind(player1_hand))
{
     //code to handle hand containing 3 of a kind
}
else if(is_straight(player1_hand))
{
     //code to handle hand containing a straight
}
else if(is_flush(player1_hand))
{
     //code to handle hand containing a flush
}
else if(is_full_house(player1_hand))
{
     //code to handle hand containg a full house
}
else if(is_four_of_kind(player1_hand))
{
     //code to handle hand containing 4 of a kind
}
else if(is_straight_flush(player1_hand))
{
     //code to handle a straight flush
}
else
     high_card(player1_hand));
     {
          //get the high card of the hand
     }

Well, hopefully that will give ye' something to think about. I wrote this pseudo code based on 5 card draw poker, but can be easily modified to other poker variants.

One more consideration: some people handle the 'royal flush' as a special case.. but in actually it's just a straight flush.

More Considerations: Some hands will test TRUE in more than one function; example: a hand containing 2-pair will also test TRUE for 1-pair... a hand containing a straight-flush will also test TRUE for a straight and for a flush, so be prepared to handle these situations accordingly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.