Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Pokey Java Game  RSS feed

 
Aron Simbaba
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone, I am having trouble with this problem described below:


In the game of Pokey, players draw 5 cards and place bets based on their hands. Write a
program that prints the best hand you can make. Here are the types of hands that exist, in
order from worst hand to best hand:

Zilch ­ No cards are the same and all five cards are not in sequence, meaning there is no
Straight.
Pair ­ Two cards are the same.
Triple ­ Three of four cards are the same.
Straight ­ All five cards are in sequence. For example: 8 9 10 J Q.
Full House ­ Three cards are the same, and the other two cards are the same

Cards do not loop, meaning K A 2 3 4 is not a straight. A is high.

Input:
The first line will contain the number of lines of input to follow. The following lines each
contain a hand of 5 cards. Each card will be separated by a single space. A card is
represented by a number 2 to 10, jack, queen, king, or ace (represented by J, Q, K, or A,
respectively).

Output:
Output the best type of hand the five cards make up. For example, in every triple there will
also be a pair. However, “triple” should be printed because it is a better hand. Each hand
should be on its own line.

Example Input:

4
8 2 6 6 7
7 8 9 10 J
4 4 10 4 10
2 3 5 9 Q

Example Output
PAIR
STRAIGHT
FULL HOUSE
ZILCH

Here is the link for to where this problem comes from this is not homework just practice

http://pscsta.org/wp-content/uploads/2014/12/novice_packet.pdf


My question is how am I supposed to compare parts of the user input with the other parts of user input? For Example, if the user inputs 1 2 2 3 4 then how am I supposed to search it to where it finds the one pair in the sequence of numbers?

Here is my code so far in the problem





Thanks and have a nice day!
-Aron Simbaba

 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a great fondness for state machines, if you know what those are, then you can easily apply that to coding this problem:

basically if you are trying to get matches
initialize a variable to 0;
read a card and increment the counter to 1;
check the next card, if it matches then increment the counter to 2 but if the card does not match, then set the counter back to 1;
continue this way through the input set, at the end you will have n-1 matches.

(yes, that does assume that you read the set in and order it first)
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would also probably try an enumerated set (2 through Ace) so I could get the sorting right.
 
Piet Souris
Rancher
Posts: 1980
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In 'Project Euler' there is a nice problem were you are given
500 pairs of poker hands, with the question how many times
the first player wins. Full poker rules.

If memory serves me right, I started by making a frequency
count of the cards, and from the number of different cards
I was able to deduce whether I was dealing with one pair,
two pairs, et cetera, or perhaps with 5 different cards.
 
Campbell Ritchie
Marshal
Posts: 55715
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aron Simbaba wrote:. . .
. . .
I don't know anything about that game, but I can see two things which will not work. One is the use of nextLine after nextXXX. As you will see here, there is a risk that you will get an empty String as your input the first time you enter that loop. As well as showing the problem, that link suggests a possible solution.
The other thing is that your if can never be fulfilled. You have written a contradiction. Maybe you meant || instead of &&. Maybe you meant != 5 and you can miss out part of the test. But why should you break out of your loop because you happen not to have 5 letters?

Another thing (that makes 3): if you enter 5, your loop will run 6× I think you need < rather than <=.

And welcome to the Ranch
 
Aron Simbaba
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
I don't know anything about that game, but I can see two things which will not work. One is the use of nextLine after nextXXX. As you will see here, there is a risk that you will get an empty String as your input the first time you enter that loop. As well as showing the problem, that link suggests a possible solution.
The other thing is that your if can never be fulfilled. You have written a contradiction. Maybe you meant || instead of &&. Maybe you meant != 5 and you can miss out part of the test. But why should you break out of your loop because you happen not to have 5 letters?

Another thing (that makes 3): if you enter 5, your loop will run 6× I think you need < rather than <=.

And welcome to the Ranch



So firstly, I dropped the if statement because I will be able to fix that later(because its the easiest part), and I fixed the nextLine issue. The main thing I'm trying to rap my head around is comparing the user input. I don't know how I should find duplicate elements in the users input. I can set up an enumerated type, but then how will i compare that to the users input of say 1 2 2 3 4 and have it know there is a pair of twos(or duces)? Here's the code I have left not much has changed:



I don't even know where to begin on this problem, so a outline or explanation would be greatly appreciated
 
Aron Simbaba
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
UPDATE

I changed the code to where when a user inputs numbers they are stored in an array. I have successfully got that to work, but when I try to search for pairs in the array, it continues to output 0 pairs.
Here is my new code:





Why is it still printing zero when I'm comparing all the elements in the array?

Thanks again,
-Aron Simbaba
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaron,

When you use the "==" operators on Objects, like Strings, you are not comparing the values you see "This is a string" to "This is a string", you are comparing the references not values. In this case you are reading in new Strings each time you read, and as such, the references wil not be the same. Change your "==" operator to use the String.equals() method and see how things go.
 
Aron Simbaba
Greenhorn
Posts: 26
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:Aaron,

When you use the "==" operators on Objects, like Strings, you are not comparing the values you see "This is a string" to "This is a string", you are comparing the references not values. In this case you are reading in new Strings each time you read, and as such, the references wil not be the same. Change your "==" operator to use the String.equals() method and see how things go.


Oh my goodness! That was such an obvious mistake lol I should have been able to catch that, but thanks very much!

I will be back when I have questions

Thanks Les
 
Aron Simbaba
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My new problem is finding a three numbers in the users input, I have it printing triple correctly, but it also prints a pair because there is 2 of the same numbers as well.



My Output:

How many hands will you deal? 2
8 2 7 7 6
2 5 5 5 3
Pair Pair Triple

Expected output:

How many hands will you deal? 2
8 2 7 7 6
2 5 5 5 3
Pair Triple


Here is my code:




Any help appreciated, thanks for all the help so far

 
Partheban Udayakumar
Ranch Hand
Posts: 499
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aron Simbaba,

For your inputs,

8 2 7 7 6 - 7 7 is pair

2 5 5 5 3 - 2 2 is a pair and then 5 5 5 is a triple.

So that is why it is displaying Pair Pair Triple. Change your conditions accordingly.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would check for triples first and if I got one, I would not check for pairs.
 
Aron Simbaba
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Partheban Udayakumar wrote:Aron Simbaba,

For your inputs,

8 2 7 7 6 - 7 7 is pair

2 5 5 5 3 - 2 2 is a pair and then 5 5 5 is a triple.

So that is why it is displaying Pair Pair Triple. Change your conditions accordingly.



Well, I have been trying to figure out the certain conditions for the code.I'm trying to see where I went wrong in my code, and if anybody could point me in the right direction. If I knew how to fix the problem, I wouldn't have asked the question in the first place.
 
Campbell Ritchie
Marshal
Posts: 55715
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once you get to that sort of thing, you probably cannot sort it out with code. You probably have to go back to work it out on paper. You need paper, a soft pencil and an eraser, the last being the most important because you have to destroy the evidence (Writing on a text editor will work, too.) When you have worked out the correct algorithm on paper, you can easily turn it into code. If you are not picking up the 2 5 5 5 3 successfully, you must have something wrong with the algorithm. No, we won't go through your code to work out what algorithm you are using. Please write out how you intend to identifiy pairs and triplets.

We have some FAQs about that: 1 and 2.
 
Aron Simbaba
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:I would check for triples first and if I got one, I would not check for pairs.



Thanks Knute! That strategy helped me achieve the triple If I have more questions I will post again thanks.
 
Campbell Ritchie
Marshal
Posts: 55715
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way: how are you entering tens? Does that match the length equals 5 stipulation from earlier?
 
Aron Simbaba
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Once you get to that sort of thing, you probably cannot sort it out with code. You probably have to go back to work it out on paper. You need paper, a soft pencil and an eraser, the last being the most important because you have to destroy the evidence (Writing on a text editor will work, too.) When you have worked out the correct algorithm on paper, you can easily turn it into code. If you are not picking up the 2 5 5 5 3 successfully, you must have something wrong with the algorithm. No, we won't go through your code to work out what algorithm you are using. Please write out how you intend to identifiy pairs and triplets.

We have some FAQs about that: 1 and 2.



I'm not looking for anybody to solve my problem, see this quote here?
Knute Snortum wrote:I would check for triples first and if I got one, I would not check for pairs.


That was not giving me the answer or checking my code. That was just a strategy to help solve the problem, that's all I'm looking for

Thanks for your response, and I will check out the links you provided.
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at my original post to you: You are treating pairs and triples as if they are separate conditions, they are not. You can have a pair, a triple, or 4 of a kind. If you go with the state machine as I mentioned, you get something along this line for decoding:

Aron Simbaba wrote:
Partheban Udayakumar wrote:Aron Simbaba,

For your inputs,

8 2 7 7 6 - 7 7 is pair

2 5 5 5 3 - 2 2 is a pair and then 5 5 5 is a triple.

So that is why it is displaying Pair Pair Triple. Change your conditions accordingly.



Well, I have been trying to figure out the certain conditions for the code.I'm trying to see where I went wrong in my code, and if anybody could point me in the right direction. If I knew how to fix the problem, I wouldn't have asked the question in the first place.
 
Aron Simbaba
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:By the way: how are you entering tens? Does that match the length equals 5 stipulation from earlier?


?? sorry i dont follow im only 14
 
Aron Simbaba
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:Look at my original post to you: You are treating pairs and triples as if they are separate conditions, they are not. You can have a pair, a triple, or 4 of a kind. If you go with the state machine as I mentioned, you get something along this line for decoding:

ive changed my code since your original post and am incorporating that already, thanks



ok gotta go catch my bus now ill check the forums at school during my ap comp sci class
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aron,
The way your solution is coded still has the same problem--you are not counting matches, but still treating double, triples, and 4 of a kind as different cases. You have to do the calculation first, then decode the result.

One of the most significant things a new programmer has to do is to learn to simplify a problem.

Stop for a minute and look at your input: if you have "Q K Q K Q" you have to compare the first to the next 4 to see if there are any matches, then you have to do the same all the way down the line until you get to the 4th place. IMO, that is a pain. So if you order the results first, you get "Q Q Q K K". All you have to do is compare the next card and you can easily see if it matches, you can easily implement a simple counter for a +1 each time it matches. You are going to need a similar approach when you look at a straight--"9 Q 10 J K" or "9 10 J Q K" which would you prefer to check for a straight?

Learning how to prepare a problem to fit how you want to process it, is learning to program--the language is just swinging a hammer and almost anyone can hit a nail on the head enough times to pound it in, but few can build a wall and even fewer can build a house.

Aron Simbaba wrote:
Les Morgan wrote:Look at my original post to you: You are treating pairs and triples as if they are separate conditions, they are not. You can have a pair, a triple, or 4 of a kind. If you go with the state machine as I mentioned, you get something along this line for decoding:

ive changed my code since your original post and am incorporating that already, thanks



ok gotta go catch my bus now ill check the forums at school during my ap comp sci class
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
btw: if you are trying to avoid ordering the input because it seems difficult, look at Classes the implement Collection, they order very well--one command.
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One trick you can use is to add all your items to a set and then check the length of the set in order to see how many different values there are. From there you only have to decide between a few different hand values. For example if you only have two different values you either have a full house or four of a kind (which should really be treated as a completely separate value not part of "triple"), so all you have to do is check how many instances you have of one of those values. If its 2 or 3 you have a full house, otherwise its four of a kind.
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, apparently I am having problems reading today, this version of your code does actually treat the matches separately. I'm going to have to wake my brain, I misread something at work this morning too.

Still has the problem--I misread my misread--I was mistaken.

Aron Simbaba wrote:
Les Morgan wrote:Look at my original post to you: You are treating pairs and triples as if they are separate conditions, they are not. You can have a pair, a triple, or 4 of a kind. If you go with the state machine as I mentioned, you get something along this line for decoding:

ive changed my code since your original post and am incorporating that already, thanks



ok gotta go catch my bus now ill check the forums at school during my ap comp sci class
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice simplification for the matching. You can also use it for a straight--length of 5, then check for minimum and maximum values and you have if((maximum-minimum)==4) myHand=="Straight"; Very elegant and no sorting, just some nice Set properties to take advantage of.

Tyson Lindner wrote:One trick you can use is to add all your items to a set and then check the length of the set in order to see how many different values there are. From there you only have to decide between a few different hand values. For example if you only have two different values you either have a full house or four of a kind (which should really be treated as a completely separate value not part of "triple"), so all you have to do is check how many instances you have of one of those values. If its 2 or 3 you have a full house, otherwise its four of a kind.
 
Piet Souris
Rancher
Posts: 1980
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you could have given my first reply some thoughts.

But you do need a frequency count. If your frequency list
has two elements, then you need to know if one of these
frequencies is 4 or not.
 
Aron Simbaba
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so now I have sorted the inputs the user has which will make it easier like Les said. Thank you all for the help, and I will comeback if I have any questions
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!