Greenhorn
posted 7 years agoOct 27, 2013 Back in 2007 Indiana organised a contest for Java 7-Card Poker Hand Evaluators. This resulted in one of the longest threads on the old forum, now archived here The code from the contest wasn't saved in the archive so it's attached below. Two weeks ago I finished writing a 7-card poker hand evaluator in Java. It's inspired by Cactus Kev's 5-card poker hand evaluator (I used a variation of the bit scheme). The final algorithm is fairly simple but I learned a lot while working on the project. A hand value is produced by passing a hand mask into a Poker-eval evaluation function. A hand value can be compared, using standard integer comparison operators, with other hand values to determine whether one hand beats another. To make the code fast, I've preserved the notion of a hand mask and a hand value. An AP Computer Science project representing a game of poker. ethnt/poker. An AP Computer Science project representing a game of poker. ethnt/poker. Pull requests 0. Review code, manage projects, and build software together. Branch: master. Find file Copy path poker / Hand.java.
Hi,
I am making a poker program and I am stuck on figuring out how to determine whether or not the cards you have are either a pair, three of a kind, etc. I think I have to use for loops to check whether the number of suites equals the 5 cards dealt and whether you have a straight (this is to test for royal flush). But, I am not sure how to write that out. Could someone give me some guidance please?
I am making a poker program and I am stuck on figuring out how to determine whether or not the cards you have are either a pair, three of a kind, etc. I think I have to use for loops to check whether the number of suites equals the 5 cards dealt and whether you have a straight (this is to test for royal flush). But, I am not sure how to write that out. Could someone give me some guidance please?
Bartender
posted 7 years ago- 1
I am stuck on figuring out how to determine whether or not the cards you have are either a pair, three of a kind, etc
If you were playing cards how would you do this? Get a pen and paper and write what steps you take to figure out if you have a pair, three of a kind etc.
BTW Rather than returning an int from your result() method (why isn't it called something meaningful like getRanking()) why not define an enum for each of the possible hands and return that.
Ranch Hand
posted 7 years ago- 1
Tony is right. I was about to post the same advice.
I actually use Poker during interviews because there are multiple collection types and algortihms involved.
The candidate does not have to get it perfect, but we are looking for the thought process.
WP
I actually use Poker during interviews because there are multiple collection types and algortihms involved.
The candidate does not have to get it perfect, but we are looking for the thought process.
WP
lowercase baba
posted 7 years agotwo things to consider:
a royal flush is really the same thing as a straight flush - it just happens to be an ace-high straight flush.
you don't need distinct code for a straight flush. you can test to see if it is a straight, and you can test to see if it is a flush. If both are true...
a royal flush is really the same thing as a straight flush - it just happens to be an ace-high straight flush.
you don't need distinct code for a straight flush. you can test to see if it is a straight, and you can test to see if it is a flush. If both are true...
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Bartender
posted 7 years agotwo things to consider:
a royal flush is really the same thing as a straight flush - it just happens to be an ace-high straight flush.
you don't need distinct code for a straight flush. you can test to see if it is a straight, and you can test to see if it is a flush. If both are true...
a royal flush is really the same thing as a straight flush - it just happens to be an ace-high straight flush.
you don't need distinct code for a straight flush. you can test to see if it is a straight, and you can test to see if it is a flush. If both are true...
Also for a full house you can test for a pair and a three of a kind (assuming your isPair() method properly checks for pairs - ie there are exactly 2 cards which are the same and not at least 2 cards the same)
Greenhorn
posted 7 years agoTony Docherty wrote:
I am stuck on figuring out how to determine whether or not the cards you have are either a pair, three of a kind, etc
If you were playing cards how would you do this? Get a pen and paper and write what steps you take to figure out if you have a pair, three of a kind etc.
BTW Rather than returning an int from your result() method (why isn't it called something meaningful like getRanking()) why not define an enum for each of the possible hands and return that.
Ok, so this is to test for Straight.
Java Poker Hand Evaluator Code Download
First I created the enum Rank.Then I check if the random face of card iterates like that of the counter. However, I am not sure how to check for the face number of the array cards.
P.S. The assignment asks for the results, so I thought I should just use that instead of getRank().
Bartender
posted 7 years agoWilliam P O'Sullivan wrote:I actually use Poker during interviews because there are multiple collection types and algortihms involved.
The candidate does not have to get it perfect, but we are looking for the thought process.
The candidate does not have to get it perfect, but we are looking for the thought process.
I totally agree with William here. Poker, even if you're just ranking hands, is NOT about simply knowing what value they are:
What's the difference between a Royal flush and a straight flush? Can you define it in object-oriented terms? What do a straight flush and a straight have in common?
This and many other questions like it will lead you to Poker Nirvana.
Winston
'Leadership is nature's way of removing morons from the productive flow' - Dogbert
Articles by Winston can be found here
Articles by Winston can be found here
Bartender
posted 7 years agoJeong Ryu wrote:
You're still jumping into coding too soon. You need to think carefully about the logic for a straight, and write down the rules and steps for finding one precisely and completely in English (or your native language), possibly combined with some pseudocode, before trying to write Java.
Poker Java Code
When you do write Java, go back and read it and convert it to English, or whatever is most natural to you, and see if it makes sense.For example, the above code says to me: 'For each card, indexed 0 to 5, if the card at the given index is the same as the index (for example, if the card at index 1 is 1 and the card at index 2 is 2) then it's a straight.' I hope you can see why that doesn't makes sense. In addition to the obvious reason, the code suggests to me that you're representing each card as a simple int. That won't do. Each card has a suit and a rank (not to be confused with your Rank enum for ordering hand types).
Bartender
posted 7 years agoWilliam P O'Sullivan wrote:Tony is right. I was about to post the same advice.
I actually use Poker during interviews because there are multiple collection types and algortihms involved.
The candidate does not have to get it perfect, but we are looking for the thought process.
WP
I actually use Poker during interviews because there are multiple collection types and algortihms involved.
The candidate does not have to get it perfect, but we are looking for the thought process.
WP
So a candidate has to be knowledgeable about poker?
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Greenhorn
posted 7 years agoJeff Verdegan wrote:
Jeong Ryu wrote:
You're still jumping into coding too soon. You need to think carefully about the logic for a straight, and write down the rules and steps for finding one precisely and completely in English (or your native language), possibly combined with some pseudocode, before trying to write Java.
When you do write Java, go back and read it and convert it to English, or whatever is most natural to you, and see if it makes sense.
For example, the above code says to me: 'For each card, indexed 0 to 5, if the card at the given index is the same as the index (for example, if the card at index 1 is 1 and the card at index 2 is 2) then it's a straight.' I hope you can see why that doesn't makes sense. In addition to the obvious reason, the code suggests to me that you're representing each card as a simple int. That won't do. Each card has a suit and a rank (not to be confused with your Rank enum for ordering hand types).
Thank you!
I will post back here after I have a more concrete understanding.
Bartender
posted 7 years agoJelle Klap wrote:So a candidate has to be knowledgeable about poker?
One would hope that a candidate that doesn't might have the sense to say that they don't.
Nice try though.
Java Poker Hand Evaluator
Winston
'Leadership is nature's way of removing morons from the productive flow' - Dogbert
Articles by Winston can be found here
Articles by Winston can be found here
Bartender
posted 7 years agoWinston Gutkowski wrote:
Jelle Klap wrote:So a candidate has to be knowledgeable about poker?
One would hope that a candidate that doesn't might have the sense to say that they don't.
Indeed.
And they don't have to know much to be able to carry on an intelligent conversation about the design. If they know only that players get groups of cards called 'hands' and that there is a ranking or ordering to those hands based on what cards are in them, the candidate should be able to lay out a decent chunk of the class structure. When it comes to talking about determining which hand you have, then even knowing only that groups of similar cards or sequential cards are valuable can again take the discussion a goodly way, at least for an interview.
The better the candidate is at separating abstractions from implementation, the more he'll be able to talk about design for an app he has little or no domain knowledge about.