• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with some syntax  RSS feed

 
Oceas Anderson
Greenhorn
Posts: 17
Android Chrome Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Currently I am writing a java program where it creates a deck and shuffles it to deal out two hands. I then need to evaluate each hand to determine what set value each hand has such as (pair, two pair, three of a kind, four of a kind, straight, or flush) I can ignore high cards straight flushes, royal flushes, and rivers. Currently I am having a hard time coming up with the syntax for two pair and straight and suggestions? This part can be found in the class file DeckOfCards in the method getHandValue.

Main.java


Card.java


DeckOfCards.java
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

^That is about to become your best friend. Once your array is in ascending order, things obviously become a lot easier. I'd even recommend you using this to redo your current method for finding one pair, and you can even combine it with finding two pairs. Straights also become much, much easier once the numbers are ordered. Good luck! If you get stuck, you can feel free to ask.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Collections.shuffle() can be used to shuffle deck of cards.

Also, do not compare Strings (or other objects) by using == operator. Use equals() method instead. AvoidTheEqualityOperator (<- click this link)

Instead of writing such long if statements as in getFaces() method you should consider using an enum.
Alternatively, you could use some kind of Map.
 
Oceas Anderson
Greenhorn
Posts: 17
Android Chrome Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel Christophel wrote:
^That is about to become your best friend. Once your array is in ascending order, things obviously become a lot easier. I'd even recommend you using this to redo your current method for finding one pair, and you can even combine it with finding two pairs. Straights also become much, much easier once the numbers are ordered. Good luck! If you get stuck, you can feel free to ask.


That definitely will make things much easier for a straight. Thanks and I most likely will but Ill implement this part first.
 
Oceas Anderson
Greenhorn
Posts: 17
Android Chrome Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

For some reason for my second hand I'm getting some pairs declared for invalid hand combinations. Such as (7,Queen,2,Ace, 8), and (7,10,4,2,13) having a pair. I'm thinking during the loop a single card is compared to itself but I don't see how. Anyone else spot a reason?

Queen = 12
Ace = 1

 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oceas Anderson wrote:
Joel Christophel wrote:
^That is about to become your best friend. Once your array is in ascending order, things obviously become a lot easier. I'd even recommend you using this to redo your current method for finding one pair, and you can even combine it with finding two pairs. Straights also become much, much easier once the numbers are ordered. Good luck! If you get stuck, you can feel free to ask.


That definitely will make things much easier for a straight. Thanks and I most likely will but Ill implement this part first.

Joel gave you some excellent advice, I suggest you take it before "implementing this part first".

Think about how you would check for pairs, triples etc if the hand is in order.
 
Oceas Anderson
Greenhorn
Posts: 17
Android Chrome Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
Oceas Anderson wrote:
Joel Christophel wrote:
^That is about to become your best friend. Once your array is in ascending order, things obviously become a lot easier. I'd even recommend you using this to redo your current method for finding one pair, and you can even combine it with finding two pairs. Straights also become much, much easier once the numbers are ordered. Good luck! If you get stuck, you can feel free to ask.


That definitely will make things much easier for a straight. Thanks and I most likely will but Ill implement this part first.

Joel gave you some excellent advice, I suggest you take it before "implementing this part first".

Think about how you would check for pairs, triples etc if the hand is in order.


Alright that is actually my next step. However I found that my issue was that if the first hand was declared a pair in the result string. When the second hand was evaluated since it wasn't reset, even if a pair was not found it will still return the value pair from the first hand.
 
Oceas Anderson
Greenhorn
Posts: 17
Android Chrome Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Currently I have sorted the array to check to see if there is a straight, not I'm trying to do a two pair. However I'm not entirely sure on how to make sure I get to distinct pairs and not a three of a kind. Down bellow is currently what I have but still not finished




 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oceas Anderson wrote:However I'm not entirely sure on how to make sure I get to distinct pairs and not a three of a kind.


You don't have to separate four of a kind, three of a kind, pairs, and straight into different loops. In fact, they can all be determined with one loop (and without any nested loops).

Here's what I did:

As I loop through the cards, when I encounter a new number, I assign it to a currentValue variable. I have a counter variable that counts the number of cards matching currentValue. Once I arrive at a number that is not equal to the current value, I check my counter to see how many times the number appeared. If it is 2, I increment a numberOfPairs variable, if it is three, I set threeOfAKind variable to true, and if it is four, I set fourOfAKind to true. Then I reset currentValue to the new number and reset my counter to 1. Remember: this is all assuming that equivalent values are adjacent (as they should be after you've sorted your array). You can also handle straights in the same loop by comparing Hand[i] to Hand[i-1]. The straights are easier, but you'll also have to account for the fact that straights can loop (i.e. 1, 2, 3, 13, 14).

Once you're out of your loop, you can analyze your variables to determine your result.

Also, your array should only be of length 5. You won't be able to find straights properly with that empty index at the beginning of your array.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!