Search code examples
javaif-statementmethodsdo-while

Trying to code a method to sort through a 5-card poker hand and evaluate if the hand has a single-pair, exclusively in Java


The method seems to work well at evaluating if there is the presence of a single-pair in the hand or not. However, I'm trying to figure out how to code in if another hand comes up as true (two pair, 4 of a kind, full house, etc) that the pair should return as false. Here's the code I have for the ruling IN of a pair while ruling out everything else. I essentially went hand-by-hand with the potential combinations of a potential pair and utilized the '^' operator to create the exclusivity.

    // if-else statements to rule in a pair exclusively
    if ((cards[0] == cards[1]) ^ (cards[0] == cards[2]) ^ (cards[0] == cards[3]) ^ (cards[0] == cards[4])){
        isPair = true;
    } else if ((cards[1] == cards[2]) ^ (cards[1] == cards[3]) ^ (cards[1] == cards[4])){
        isPair =true;
    } else if ((cards[2] == cards[3]) ^ (cards[2] == cards[4])){
        isPair = true;
    } else if (cards[3] == cards[4]){
        isPair = true;
    } else 
        isPair = false;
    return isPair;

I had thought with the use of '^' that it would have made it that so if it had found another pair within it, but it is still coming up as "true" when running hands that have a two pair. I tried adding another int variable tooManyPairs and incremented inside of the if-else statements every time it found a pair while in do-while loop. Here's what it looked like:

    public boolean isPair(){
    boolean isPair = false;
    int tooManyPairs = 0;
    int [] cards = new int [hand.length];
    for (int i = 0; i < hand.length; i++){
        cards[i] = hand[i].getRank();
    }
    // if-else loops to rule out 4 of a kind
    do {
    // if-else statements to rule in a pair exclusively
    if ((cards[0] == cards[1]) ^ (cards[0] == cards[2]) ^ (cards[0] == cards[3]) ^ (cards[0] == cards[4])){
        isPair = true;
        tooManyPairs++;
    } else if ((cards[1] == cards[2]) ^ (cards[1] == cards[3]) ^ (cards[1] == cards[4])){
        isPair =true;
        tooManyPairs++;
    } else if ((cards[2] == cards[3]) ^ (cards[2] == cards[4])){
        isPair = true;
        tooManyPairs++;
    } else if (cards[3] == cards[4]){
        isPair = true;
        tooManyPairs++;
    } else 
        isPair = false;
    }while (tooManyPairs < 1);
    if (tooManyPairs > 1)
        return isPair = false;
    return isPair;

The enter image description here

The output is still coming up as true for both instead of just the two pair. I thought I could repost the previous code for those methods into this method with modifications that would make them return the isPair method as false. That just seems like a lot of excess code and I feel like there should be a more simple way of doing it. Any guidance would be appreciated.


Solution

  • Because this is homework, I'll give pseudo code only...

    I think you're going about it the hard way. Try this:

    • collect the rank of cards in the hand into an array of ranks
    • sort the array of ranks
    • iterate over the array (once) and count the number of times adjacent ranks are the same
    • return true if the count is 1, false otherwise

    Consider that using this approach will calculate 3 of a kind as 2 pairs when it's actually 3 pairs (and 4 of a kind 6 pairs), but that won't affect correctness of this approach.