-6

I'm trying to evaluate a hand to see if they have a pair and I feel like this is right but i keep getting errors. Any idea on what im doing wrong?

public boolean isPair() {
    String[] values = new String[5];
    int counter = 0;    

    //Put each cards numeric value into array
    for(int i = 0; i < cards.length; i++){
        values[i] = cards[i].toString();
    }

    //Loop through the values. Compare each value to all values
    //If exactly two matches are made - return true
    for(int j = 0; j < values.length; j++){
        for(int k = 0; k < cards.length; k++){
            if(values[j].equals(cards[k].toString()))
                counter++;
            if(counter == 2)
                return true;

        }
        counter = 0;
    }

    return false;  
}
user1803551
  • 12,965
  • 5
  • 47
  • 74
Winticles
  • 1
  • 4
  • 3
    What error you get? – Jens Apr 27 '17 at 12:27
  • 3
    Please provide the errors that you get, otherwise we won't be able to help you. – CodedStingray Apr 27 '17 at 12:28
  • 2
    Please provide an [mcve] or use a debugger to find out what is happening – Jens Apr 27 '17 at 12:29
  • Well now i keep just getting a NullPointerException – Winticles Apr 27 '17 at 12:46
  • I think the NullPointerException is in my main when i try to test a hand of five cards and see if the pair method is working. Sorry I'm very new to Java as you can probably tell – Winticles Apr 27 '17 at 12:49
  • If you are getting an NPE, either `cards` is `null` or one of its elements is `null`. – Stephen C Apr 27 '17 at 12:50
  • 1
    Actually ... I think you would be better off trying to debug this code **without** asking questions. You will **learn more** that way, and that is (presumably) your real goal in writing this program. – Stephen C Apr 27 '17 at 12:52
  • This is much. much easier if you sort the hand by rank first. Then, just check for the patterns XXabc, aXXbc, abXXc, abcXX. Also, you should check for hands in reverse-value order. That is, check for straight flushes first, then quads, then full houses, etc. – Lee Daniel Crocker Apr 27 '17 at 17:02

2 Answers2

0

The first obvious error I can see, is that you re comparing the first card with itself here :

for(int j = 0; j < values.length; j++){
        for(int k = 0; k < cards.length; k++){

Your index k should not be verified if it equals j.

Secondly, why are you using a your variable "hand" in the comparison when you bothered to create a array of String containing your hand ?

values[j].equals(cards[k].toString())

You can write :

values[j].equals(values[k])

I dont think it is responsible for any errors, but it s much easier to understand.

And finally, your counter is false. A pair, is by definition two cards of the same value. So, you have to check if a value is present only two times(that means 1 equality) in your hand.

So you'll have :

for(int j = 0; j < values.length; j++){
    for(int k = 0; k < cards.length; k++){
        if(k!=j){
            if(values[j].equals(values[k]))
                counter ++;
        }
    }
    if (counter ==1 ) //Counter==1 means the a value matched with an other value in your hand only once, involving one pair.
        return true;
    else counter = 0;

}

return false;
Asew
  • 374
  • 2
  • 13
  • A pair is exactly two cards the same. Not two or more the same. (See comment in original code ... or a Poker rule-book :-) ) – Stephen C Apr 27 '17 at 12:49
  • When I say "two differents cards", I mean" two _physically_ differents cards". In a proper deck, you can not have 2 copy of the "2 of hearts" card. The only thing that matters in a pair, is the value of the cards being the same (2 of hearts and 2 of spades for example). – Asew Apr 27 '17 at 12:51
  • 1
    1) You don't mean **physically** different. In a deck of cards, all cards are **physically** different. Even if they are all the ace of spaces! 2) You have missed my point. In poker three aces is not a pair. Your code will incorrectly identify a hand with three aces as a pair. – Stephen C Apr 27 '17 at 12:58
  • Oh, I see what you mean in your second point, let me correct my code. – Asew Apr 27 '17 at 13:06
  • @StephenC "*ace of spaces*" :) – user1803551 Apr 27 '17 at 13:25
  • I didn't mean that. But its beautiful :-) – Stephen C Apr 27 '17 at 13:28
-1
String[] values = new String[5];

should be

String[] values = new String[cards.length];

However, even better would be to not use values anyway, since it's almost a copy of cards.

Steve Smith
  • 2,244
  • 2
  • 18
  • 22