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.
equalson yourCardclass, since here you're not usingcompareTofor anything other than equality checking.