Posted in Poker March 12th, 2010
How do I check for poker hands in Java?
If i have 5 cards with rank and suit how do I check for different poker hands in java i.e. straights, flushes, etc? I’m not looking for code, just a way to do it. thanks!
This entry was posted on Friday, March 12th, 2010 at 3:38 pm and is filed under Poker. You can follow any responses to this entry through the RSS 2.0 feed. Responses are currently closed, but you can trackback from your own site.
One Comment on this post
Posted by videobobkart March 12th, 2010 at 3:57 pm
There won’t be any trick to it. You will have a series of methods, each one taking an array of 5 cards and returning a boolean. One for each kind of hand you are trying to detect. Then the code within each method must just do the appropriate checks. It will be somewhat detail-oriented but I don’t see a way around it.
For example:
isPair – look at each card and see if there is another in the hand of the same rank
isThreeOfAKind – similar to isPair but must find two other cards with the same rank as each chosen card
isFlush – take the first card, note its suit, then check that the other four cards have that same suit.
isStraight – find the lowest card, then check that among the other four cards there is a card with each of: one higher rank, two higher rank, three higher rank, and four higher rank. With Aces being either low or high there will be some additional work in this method to accommodate that.
isStraightFlush – could be implemented by calling isStraight and isFlush and only returning true if those are both true
isRoyalFlush – first check for isStraightFlush then check that the lowest card has rank 10.
You get the idea.
One detail to consider is whether you want to consider three-of-a-kind as a pair, since technically there are two cards with the same rank. If not, then the code for IsPair will have to also call isThreeOfAKind (and isFourOfAKind) and not return true if any of those are true.
Wild cards will complicate these methods of course. Then they will need a way of knowing what is wild as they look at the cards to see if they meet the requirements of the kind of hand they are checking for.