# Need help with pokerhand/two pairs

• 12-01-2012, 12:05 PM
VIhuri
Need help with pokerhand/two pairs
I need to create a program that checks whether the randomly assigned hand contains two pairs. Now this could be done with one heck of an if - method containing all the possible variations of two pairs. Another way I figured would be to sort the array and check whether any two indexes that are alongside were equal. However, I cannot use Arrays.sort.

Now I'm at a loss on how to proceed. The for - methods I thought of that would put each member of the array against each other would also count in three of the same and four of the same.

Any hints on how to proceed here?
• 12-01-2012, 12:37 PM
pbrockway2
Re: Need help with pokerhand/two pairs
Why can't you use sort()? If it's an arbitrary assignment condition, then perhaps you are meant to implement your own sort method for the array.

Sorting would make sense, not only for detecting two pairs (remember to distinguish 2 pairs from 4 of a kind) but for doing other hand evaluations.

---

Another approach would be to create an array of 13 ints. Use a for loop to go through the hand counting the card values. Then another for loop could go through the array of face value counts and see how many 2's there are.
• 12-01-2012, 12:53 PM
VIhuri
Re: Need help with pokerhand/two pairs
I decided to go with the one heck of an if-statement. It's clumsy as hell but works.

Also need to work out this: I need to print out the number of the card and whether it's a club, heart etc.

I can parse out the information of the type and turn it to integers 1-4. Is there anyway to attach a string to each of these values? So if the card's type has a value of 1, it would print out "spades"?

edit: I cannot use sort() because this assignment is done in artificial environment. I can only type certain amount of code; the rest is given. And the it hasn't imported java.util.Arrays.
• 12-01-2012, 01:32 PM
VIhuri
Re: Need help with pokerhand/two pairs
Ooooh yeah, baby! Got it working!

Well, with the clumsy huge if-method but still.

The second part was so easy it's embarassing. Just created a String-array.
• 12-01-2012, 02:17 PM
DarrylBurke
Re: Need help with pokerhand/two pairs
Quote:

Originally Posted by VIhuri
I can parse out the information of the type and turn it to integers 1-4. Is there anyway to attach a string to each of these values? So if the card's type has a value of 1, it would print out "spades"?

Quote:

Originally Posted by VIhuri
The second part was so easy it's embarassing. Just created a String-array.

Have you learned Enums yet?
Enum Types (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

db
• 12-01-2012, 10:45 PM
VIhuri
Re: Need help with pokerhand/two pairs
Nope. I've encountered the word in error messages but that's about it.

I would like to hear how the two pairs problem would have been solved in a more efficient way. The tools we've been given in this class thus far aren't exactly very refined (if, while/for, tables, subprograms)