• Post Reply Bookmark Topic Watch Topic
  • New Topic

Testing for poker hands  RSS feed

 
Jeong Ryu
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
two 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...


 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
two 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...

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)
 
Jeong Ryu
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony 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.

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().
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William 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.

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
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William 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


So a candidate has to be knowledgeable about poker?
 
Jeong Ryu
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff 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.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Nice try though.

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!