Jump to content
  • Advertisement
Sign in to follow this  
deftware

Detecting poker hands

This topic is 4627 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there, I'm writing a Poker game for fun but I've stumped myself with the problem of detecting what hands the player has. I don't know how to go about looking at a list of seemingly random cards and determining if they follow the rules for different hand rankings. Anybody have any ideas? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
I would break it up into a smaller problem. How do you tell if the player has a pair? How do you tell if the player has two pair? How about three of a kind?

It's not too bad to tell if a player has a specific type of hand so maybe you can write functions like has_pair(), has_two_pair(), has_full_house() (which itself can be written in terms of has_pair() and has_three_of_kind(), etc. and then combine those into get_player_hands()

Share this post


Link to post
Share on other sites
I would probably do something simple like this. It obviously isn't the fastest way becuase there's a lot of duplicate checking. eg. when checking for 4 of a kind, you may find a pair, but you need to recheck for this later in the IsPair function. Like I said it's simple, slow, but should work. :)

Starting checking from the highest hand down to the lowest and return the appropriate rank/score.

int GetRankOfHand(CardList Cards)
{
if IsRoyalFlush(Cards) then
return TheRank
else if IsStraightFlush(Cards) then
return TheRank
else if .......
.....
......
else
Return RankOfHighestCard
}

IsRoyalFlush etc. check soley for the combination of cards that make up this hand. RankOfHighestCard returns the value of the highest card. Using this method you GetRankOfHand should always return the highest rank.

After checking all the hands, you can just compare the values returned to see who is the winner. Or let your AI use this value to decide how to proceed.

There's a good page here that describes the various hands you need to check for.. http://www.pagat.com/vying/pokerrank.html

Hope it's of some value to you.

fig

Share this post


Link to post
Share on other sites
Damn, I took 15 minutes to write that? Must be a timezone thing. :)

Share this post


Link to post
Share on other sites
Quote:
Original post by fig
After checking all the hands, you can just compare the values returned to see who is the winner. Or let your AI use this value to decide how to proceed.


You'd need to return more than just the rank of the highest card, imagine this situation.
Player A has: AK752
Player B has: AJ843

Returning just the rank of would indicate that both players have an ace high, but wouldn't show that Player A beats B because he has an ace with a king kicker vs. B's ace with a jack kicker

Share this post


Link to post
Share on other sites
If the ranks come out the same for 2 or more hands you can always write a function that resolves ties whch just checks off the highest cards in order for each of the players.

Share this post


Link to post
Share on other sites
I've written a few card games in the past. And it does boil down to breaking them down into smaller test functons.

Make sure the cards are represented internally in a way that allows you to work out thier face value and suit (remember you have to account for flushes), test the values of the cards for patterns, and also the suit of the cards for flushes. keep and work on a copy of the hand, that you refresh fo each test, you may need alter values while testing to discount them

Then sort the card values in order(Ascending or decending, up to you). Test for the possible types of hand, starting with Royal flush, Str Flush, 4 of a kind etc.

Its easier to test the numerical values 1st then the suit values. If you test for a straight, just sort the numbers in order, and ensue they follow in sequence, 1,2,3,4,5 if the test succeeds, its a straight, then test the suit and if all the same upgrade the hand value to a stright flush.

If you find a set of cards meet 1 of those test conditions, assign a value for that type of hand (10 for RF, 9 for SF etc) and then compare hands, if the hands match, check for the kickers and return a value for that, maybe even a 2nd,3rd and 4th kicker value in the case of a high card hand.

Watch out for a couple of oddities, don't confuse 3 of a kind with a full house, and in 2 pair make sure the highest pair count, then the lower pair, but also there's a slim chance the kicker might count in a rated hand. If you work from a possible high hand down this should not happen.

You also need to be careful you check for a tie, its rare but it happens and you can find yourself iterating out of the hand if you don't check for it.

There's a load of rules you can impliment to speed the test up if running on a low speed system, for example if you have a flush,or straight, you can't have a full house or trio or pair, but you might as well just run through each test till you've done and take the highest value you find.

If you follow a simple process of checking for each type of hand, you can't go too far wrong.

Share this post


Link to post
Share on other sites


Hey, thanks a lot guys. I think I got it figured out now.

For the player I'm detecting number of possible outs and their probabilities of occuring by the river. Incorporating that into detecting the player's hand was really wracking my brains. Thanks again.

Share this post


Link to post
Share on other sites
Pokersource is a good starting point. :)

Its strength AND weakness is the fact that it will evaluate a poker hand given an arbitrary number of cards (ie, 5 cards, 6 cards, or 7 cards)

Due to this "feature" it is not well optimised. Specifically, the LAST evaluation is for "high card" hands which unfortunately is THE most common hand to hold given an even distribution of legal inputs. These hands can be detected and handled much sooner in the logic chain via a single table lookup if you know how many cards you are dealing with priori.

Still, its very good as is.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!