3

I'm having trouble with my program recognizing a one pair ONLY AFTER it first checks for a two pair. When checking for the one pair first it finds it ok. But when I check for the two pair first then check for a 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);
}
}
Radu Murzea
  • 10,724
  • 10
  • 47
  • 69
MM-MikeSauce
  • 35
  • 1
  • 5
  • I would suggest adding a method to RankingEnum like: public boolean isRank(Set cards); That way each ranking is responsible for determining if a hand qualifies instead. – HiJon89 Jan 21 '13 at 21:47

3 Answers3

5

In checkTwoPair you are doing hand.removeAll(). This is removing it from the underlying ArrayList. This is why you are not finding it afterwards.

Jeff
  • 12,555
  • 5
  • 33
  • 60
Elliott Hill
  • 941
  • 7
  • 14
3

The problem is that you're modifying hand when looking for two pair:

   hand.removeAll(checkedPair);

Thus when getTwoPair() completes, the sole pair has been removed from the hand, and checkPair() no longer works.

You need to make getTwoPair() non-intrusive.

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

NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

When you run getTwoPair the first operation you do is checkPair(hand); This finds the pair (the one you want) removes it, then tries to check again. When this check fails, it returns.

After the return you're left with (1) a hand that has the pair removed and (2) a null return from getTwoPair. This result leaves you with a condition that causes the main flow to continue, yet fail on finding the single pair, since it was removed.

I suggest making a copy of the array list to search on. If you fail to find two pairs, simply return the original array. If you find tow pairs, return the modified array.

Grambot
  • 4,370
  • 5
  • 28
  • 43