Jack Armstrong
Jack Armstrong

Reputation: 1249

Boolean Methods with Arrays, coding issue

I would like some help coding these methods. Each method should check to see if a group of 5 cards, stored as an array, is certain hand in poker, like one pair, two pair, Full House, etc. This is what I have so far. Also, sort(), sort the cards from smallest to largest and in the order of Clubs, Diamonds, Hearts and then Spades. Lastly I apologize for any formatting issues with the brackets. Thank you.

private boolean isOnePair(){//checks if one pair exists---Done
    sort();
    for(int i=0;i<cards.length-2;i++){
        if(cards[i].compareTo(cards[i+1])==0){
            return true;
        }
        else{
            return false;
        }
    }
}

In theory this one checks to see if there One pair exists in the hand. I believe it is done and makes sense, but if it aint, please tell me why.

private boolean isTwoPair(){//check if two pair exists---
    sort();
    for(int i=0;i<cards.length-2;i++){
        if(cards[i].compareTo(cards[i+1])==0){
            return true;
        }
    }else{
        return false;
    }
}

This is where I would like help. It needs to check if there are two different pairs. I'm assuming I need something to check like I did in isOnePair. Also, Since Four of a Kind is technically(in real life, not card terms) considered Two pairs, would this be an issue?

private boolean isThreeOfAKind(){//check if three of a kind---Done
    sort(); 
    for(int i=0;i<cards.length-3;i++){
        if((cards[i].compareTo(cards[i+1])==0)
              &&(cards[i+1].compareTo(cards[i+2])==0)){
            return true;
        }else{
            return false;
        }
    }
}   

This checks to see if there are three kinds of one card. This I believe is good

private boolean isStraight(){//check if all in a row---Done
    sort();
    for(int i=0;i<cards.length-1;i++){
        if(cards[i]<cards[i+1]&&()cards[i+1]-cards[i])==1)){
            return true;
        }else{
            return false;
        }
    }
}

This checks to see if the cards are a Straight. So they are in numerical order since the difference between each card will have to be 1.

private boolean isFlush(){//check if all have same suit---
    sort();
    for(int i=0;i<cards.length-1;i++){
    }
}

This is also where I would like to most help. My trouble is just the thought behind how to check the suits. I know you guys dont have the actual code that tells you the suit value, but if someone could help me with the thought process of it.

private boolean isFourKind(){//check if four of a kind---Done
    sort();
    for(int i=0;i<cards.length-4;i++){
        if((cards[i].compareTo(cards[i+1])==0)
                &&(cards[i+1].compareTo(cards[i+2])==0)
                &&(cards[i+2].compareTo(cards[i+3])==0)){
            return true;
        }else{
            return false;
        }
    }
}

Check Four of a Kind. Should be all good.

private boolean isRoyalFlush(){//check if from 10-Ace and All of same suit---
    sort();
    for(int i=0;i<cards.length-1;i++){
    }
}

This should be a combo of Straight and Flush. But the last condition is that the card in slot card[0]==10.

Upvotes: 0

Views: 181

Answers (1)

NoseKnowsAll
NoseKnowsAll

Reputation: 4624

To check whether you have two pairs, simply create a counter that increments every time you find a pair - instead of instantly returning true. If your counter ever reaches two, you return true.

Also, your worries about two pairs and four of a kind being similar should all be handled outside these methods. As in, check to see if you have the most restrictive (or highest point) hand first, and only if you don't have it do you check the next restrictive hands.

Finally, you cannot program this without your Card class having a visible suit variable (or a getter function that returns the suit). Once you have that, then you can very easily check for flushes, flushes+straights, etc. After you implement this, it should look something like the following

int suit = cards[0].getSuit();
for (int i = 1; i < cards.length; ++i) {
    if (suit != cards[i].getSuit()) // checks to see if you have a card of a different suit
        return false;
}
return true; // suits of all cards are the same

PS: Don't really know the rules for Poker. Can a straight be composed of cards of different suits?

Upvotes: 1

Related Questions