Texas Hold'em recognizes one pair

I am having problems with my program recognizing one pair ONLY AFTER it first checks for two pairs. The first time it checks one pair, it finds it ok. But when I check two pairs first, then check one pair, it doesn't find it. Any help would be appreciated.

package card.game.simulator;

import java.util.ArrayList;

public class RankingUtility {
private RankingEnum rank;

public String getRankOfHand(ArrayList<Card> hand) {
    System.out.printf("%s\n", hand.toString());
    ArrayList<Card> rankingCards = getTwoPair(hand);
    if(rankingCards != null) {
        return "Two Pair!";
    }
    System.out.printf("%s\n", hand.toString());
    rankingCards = getOnePair(hand);
    if(rankingCards != null) {
        return "One Pair!";
    }
    System.out.printf("%s\n", hand.toString());
    return "You got nuthin...";
}

public boolean isSameSuit(ArrayList<Card> hand) {
    CardSuitEnum suit = hand.get(0).getSuit();
    for(Card card : hand) {
        if(card.getSuit() != suit) {
            return false;
        }
    }
    return true;
}

public ArrayList<Card> checkPair(ArrayList<Card> hand) {
    ArrayList<Card> checkedPair = new ArrayList<>();
    for(Card card1 : hand) {
        checkedPair.add(card1);
        for(Card card2 : hand) {
            if(!card1.equals(card2) && card1.getFace().equals(card2.getFace())) {
                checkedPair.add(card2);
                return checkedPair;
            }
        }
        checkedPair.clear();
    }
    return null;
}

public ArrayList<Card> getTwoPair(ArrayList<Card> hand) {
    ArrayList<Card> twoPair = new ArrayList<>();
    ArrayList<Card> checkedPair = checkPair(hand);
    if(checkedPair != null) {
        twoPair.addAll(checkedPair);
        hand.removeAll(checkedPair);
    }
    checkedPair = checkPair(hand);
    if(checkedPair != null) {
        twoPair.addAll(checkedPair);
        return twoPair;
    }
    return null;
}

public ArrayList<Card> getOnePair(ArrayList<Card> hand) {
    return checkPair(hand);
}
}

      

+3


source to share


3 answers


When you run getTwoPair

, the first operation you do is checkPair(hand);

. With this, the pair (the one you want) deletes and then tries to check again. When this check fails, it will return.

Upon return, you are left with (1) the hand that had the pair removed, and (2) zero return from getTwoPair

. This result leaves you with a condition that causes the main thread to continue, but cannot find one pair because it was deleted.



I suggest making a copy of the list of arrays for search. If you can't find two pairs, just return the original array. If you find pairs of tugs, return the modified array.

0


source


In checkTwoPair

you do hand.removeAll()

. This removes it from the base ArrayList

. That is why you will not recognize him afterwards.



+5


source


The problem is that you are changing hand

when looking for two pairs:

   hand.removeAll(checkedPair);

      

Thus, when getTwoPair()

completed, the single pair is removed from the hand and checkPair()

no longer works.

You need to make it getTwoPair()

non-intrusive.

Alternatively, you can combine the two methods into one, returning zero, two, or four cards depending on how many pairs were found.

+3


source







All Articles