• Post Reply Bookmark Topic Watch Topic
  • New Topic

Another Card Game Problem  RSS feed

 
Mimi Udasco
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone!!! So I have any card game problem that I'm having a lot of trouble with!  I basically want to make sure that out of 5 cards, four cards are the same in terms of hands.  The suits can be different!
Here is the question:

A bit trickier question is to determine whether the given five-card poker hand contains four of a kind, that is, four cards of the same rank. Write a method boolean hasFourOfAKind(String hand) to determine this. (Now the suits don’t matter, and the ranks are given in positions 0, 2, 4, 6 and 8 of the parameter string.)

Here is my code:


 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Mimi,

what might be handy in valuating a Card hand, is to do a frequency count of that hand. You would be using the ranks of the cards for this. The possible frequencies are, given a hand of 5, 5-0 (someone cheating?), 4-1, then you hve your answer, 3-2, 3-1-1, et cetera. That would make the valuation much easier. You could use a Map for this, or a simple array.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mimi Udasco wrote:Hi everyone!!! So I have any card game problem that I'm having a lot of trouble with!

What trouble are you having?  Your code seems to work.  What inputs does it fail with?
 
Mimi Udasco
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everyone!! Thanks for all the help!!! I finally got it to work!!


boolean hasFourOfAKind(String hand){
        
     String temp = "";
     int count = 0;
       
        for (int i=0; i<hand.length(); i+=2) {
           
            temp = hand.substring(i,i+1);
            System.out.println("Question 2: string temp: " + hand.substring(i,i+1));
           
            for (int j=i+2; j<hand.length();j+=2){
           
                if (temp.equals(hand.substring(j,j+1))){
                   
                    System.out.println("Question 2: j " + hand.substring(j,j+1));
                   
                    count++;
   
            }
           
           
        }
  
        if (count == 6){
           
                    return true;
           
                  }
    }
   
    return false;   

}
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mimi Udasco wrote:Hey everyone!! Thanks for all the help!!! I finally got it to work!!

Getting it to work is good, but it's only the first step. It took me a while to understand what you were doing in that code and how you could claim that it worked.

This was the part that immediately stuck out to me as possibly wrong:

Only after some testing and analysis did I realize that your algorithm is actually a bit naive in that it doesn't stop scanning the string for matches when it could/should. This results in double counting of some of the matches, hence your need to compare count to 6 instead of 4, which would be more intuitive given that the intent is to find four of the same, not six. In programmer-speak, it's a kludge. In football-speak, "It ain't pretty but it's a W" (Works).
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags (that's a link) when you're posting code.  It looks a lot better and people will be more likely to help you.
 
ashkan ahmadi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,
I didn't understand whey do we need/have to use 6 in the following codes for the counter? can you explain why we cannot use 4. And finally, if it was for three of kinds, what number for counter is reasonable? 

i
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ashkan,

suppose the 5 card hand does contain 4 equal cards. In the first outer loop, it compares the first card to the other four cards; it will find three matches and so count will be three. That would be enough to conclude that we indeed do have four of a kind. However, the code  continues the outer loop with card 1. It will find two equal cards now, and so count will become 5.

Can you now reason why count == 6 indeed signals success, and that it also works when the four equal cards are in position 1 to 5?

As you can tell, this code is not very efficient when it comes to analysing a hand of cards. Anyway, can you figure out how this routine should be adapted to determine whether we have full house (3-2) or not (say 3-1-1) (or something completely different)?
 
ashkan ahmadi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately, i did not understand why we put count==6 yet. But, lets talk about a full house.Maybe i can figure out by this problem.
In my idea, for full house, when the cards are not sorted, we need to put 2 counter, and 3 for loops, and counters must reach to 2 and 1, then we can find the cards in full house.This is my codes. but i feel something is wrong.
Maybe the for loop orders or the number of the counter. I'll be happy if you correct me from a wrong.

 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I tried your method, but I got the wrong outcome. Here is he code I used, and note that my cards are in the form "2D", "TH", "AS", et cetera (so value first, then suit)

output:7DQH7C7SQS is full house: false

To be frank: it is hard to see how you can change the original code from Mimi such that it correctly identifies the hand. In my first reply I suggested to do a frequency count of the hand. That was not followed up by any comment, but that is still the way I recommend. Now, if we were allowed to use a Map and java 8, a frequency table would just be one line of code. However, that seems beyond  where the course is now. So, let's use an array.

For this, I created an array 'int[] cards = new int[15]';
and I created this method:

With this, and a given hand, it is easy to fill this array.

This method is also handy:

If the array contains three times a value of 7, then this method would give 3 when called with 'int freq = arrayContains(cards, 7);

With these methods, it becomes quite easy to tell if a hand contains four of a kind, full house, two pairs, et cetera. For insance, say you had this code as well:

could you now identify a hand with ease?

If you really want to delve into evaluating poker hands, I can recoomend Project Euler, exercise 54.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!