//look for royal flush, removing pair using recursion public static bool isRoyalFlush(Hand hand) { hand.sortByRank(); Hand simplifiedhand1, simplifiedhand2; //to be set the same as hand - cards are removed from this hand to evaluate straights separately without the interference of pairs or three-of-a-kind for (int i = 0; i <= hand.Count() - 2; i++) { if (hand[i] == hand[i + 1]) { simplifiedhand1 = new Hand(hand); simplifiedhand1.Remove(i); simplifiedhand2 = new Hand(hand); simplifiedhand2.Remove(i + 1); if (HandCombination.isRoyalFlush(simplifiedhand1)) { return(true); } if (HandCombination.isRoyalFlush(simplifiedhand2)) { return(true); } } } int currentsuit = hand.getCard(0).getSuit(); if (hand.getCard(0).getRank() == 14 && hand.getCard(1).getRank() == 13 && hand.getCard(2).getRank() == 12 && hand.getCard(3).getRank() == 11 && hand.getCard(4).getRank() == 10 && hand.getCard(1).getSuit() == currentsuit && hand.getCard(2).getSuit() == currentsuit && hand.getCard(3).getSuit() == currentsuit && hand.getCard(4).getSuit() == currentsuit) { return(true); } else { return(false); } }
//explaination below, same as isStraight except return cards public static Hand getStraight(Hand hand) { hand.sortByRank(); Hand straight = new Hand(); straight.setValue(5); if (hand.getCard(0).getRank() == 14) { hand.Add(new Card((int)RANK.ACE, hand.getCard(0).getSuit())); } int straightCount = 1; straight.Add(hand.getCard(0)); for (int i = 0; i <= hand.Count() - 2; i++) { //if 5 cards are found to be straights, break out of the loop if (straightCount == 5) { break; } int currentrank = hand.getCard(i).getRank(); //if cards suit differ by 1, increment straight if (currentrank - hand.getCard(i + 1).getRank() == 1) { straightCount++; straight.Add(hand.getCard(i + 1)); } //specific condition for 2-A else if (currentrank == 2 && hand.getCard(i + 1).getRank() == 14) { straightCount++; straight.Add(hand.getCard(i + 1)); } //if cards suit differ by more than 1, reset straight to 1 else if (currentrank - hand.getCard(i + 1).getRank() > 1) { straightCount = 1; straight.Clear(); straight.setValue(5); straight.Add(hand.getCard(i + 1)); } //if card suits does not differ, do nothing } //depending on the straight count, return true or false if (hand.getCard(0).getRank() == 14) { hand.Remove(hand.Count() - 1); } if (straightCount != 5) { straight.Clear(); } straight.setValue(straight.getCard(0).getRank()); return(straight); }
//use recursion to get rid of pairs, then evaluate straight flush public static bool isStraightFlush(Hand hand) { hand.sortByRank(); Hand simplifiedhand1, simplifiedhand2; //to be set the same as hand - cards are removed from this hand to evaluate straights separately without the interference of pairs or three-of-a-kind for (int i = 0; i <= hand.Count() - 2; i++) { if (hand.getCard(i) == hand.getCard(i + 1)) { simplifiedhand1 = new Hand(hand); simplifiedhand1.Remove(i); simplifiedhand2 = new Hand(hand); simplifiedhand2.Remove(i + 1); if (HandCombination.isStraightFlush(simplifiedhand1)) { return(true); } if (HandCombination.isStraightFlush(simplifiedhand2)) { return(true); } } } for (int i = 0; i <= hand.Count() - 5; i++) { int currentrank = hand.getCard(i).getRank(), currentsuit = hand.getCard(i).getSuit(); if (currentrank == hand.getCard(i + 1).getRank() + 1 && currentrank == hand.getCard(i + 2).getRank() + 2 && currentrank == hand.getCard(i + 3).getRank() + 3 && currentrank == hand.getCard(i + 4).getRank() + 4 && currentsuit == hand.getCard(i + 1).getSuit() && currentsuit == hand.getCard(i + 2).getSuit() && currentsuit == hand.getCard(i + 3).getSuit() && currentsuit == hand.getCard(i + 4).getSuit()) { return(true); } } for (int i = 0; i <= hand.Count() - 4; i++) { int currentrank = hand.getCard(i).getRank(), currentsuit = hand.getCard(i).getSuit(); if (currentrank == 5 && hand.getCard(i + 1).getRank() == 4 && hand.getCard(i + 2).getRank() == 3 && hand.getCard(i + 3).getRank() == 2 && hand.getCard(0).getRank() == 14 && currentsuit == hand.getCard(i + 1).getSuit() && currentsuit == hand.getCard(i + 2).getSuit() && currentsuit == hand.getCard(i + 3).getSuit() && currentsuit == hand.getCard(0).getSuit()) { return(true); } } return(false); }
//get all remaining cards, if necessary, to form 5 cards private static Hand getKickers(Hand hand, Hand specialCards) { if (specialCards.Count() == 0) { return(specialCards); } for (int i = 0; i < specialCards.Count(); i++) { hand.Remove(specialCards.getCard(i)); } for (int i = 0; i < hand.Count(); i++) { if (specialCards.Count() >= 5) { break; } specialCards.Add(hand.getCard(i)); specialCards.setValue(hand.getCard(i).getRank()); } return(specialCards); }
//get straight flush using two pointer variable and taking care of all cases public static Hand getStraightFlush(Hand hand) { hand.sortByRank(); Hand straightflush = new Hand(); straightflush.setValue(9); if (hand.getCard(0).getRank() == 14) { hand.Add(new Card((int)RANK.ACE, hand.getCard(0).getSuit())); } //int straightflushCount = 1; straightflush.Add(hand.getCard(0)); int ptr1 = 0, ptr2 = 1; while (ptr1 < hand.Count() - 2 || ptr2 < hand.Count()) { if (straightflush.Count() >= 5) { break; } int rank1 = hand.getCard(ptr1).getRank(), rank2 = hand.getCard(ptr2).getRank(); int suit1 = hand.getCard(ptr1).getSuit(), suit2 = hand.getCard(ptr2).getSuit(); if (rank1 - rank2 == 1 && suit1 == suit2) { straightflush.Add(hand.getCard(ptr2)); ptr1 = ptr2; ptr2++; } else if (rank1 == 2 && rank2 == 14 && suit1 == suit2) { straightflush.Add(hand.getCard(ptr2)); ptr1 = ptr2; ptr2++; } else { if (rank1 - rank2 <= 1) { ptr2++; } else { straightflush.Clear(); straightflush.setValue(9); ptr1++; ptr2 = ptr1 + 1; straightflush.Add(hand.getCard(ptr1)); } } } if (hand.getCard(0).getRank() == 14) { hand.Remove(hand.Count() - 1); } straightflush.setValue(straightflush.getCard(0).getRank()); if (straightflush.Count() < 5) { straightflush.Clear(); } return(straightflush); }