public static Hand getTwoPair(Hand hand) { hand.sortByRank(); Hand twopair = new Hand(); twopair.setValue(3); int pairCount = 0; for (int i = 0; i <= hand.Count() - 2; i++) { if (hand.getCard(i) == hand.getCard(i + 1)) { twopair.setValue(hand.getCard(i).getRank()); twopair.Add(hand.getCard(i)); twopair.Add(hand.getCard(i + 1)); pairCount++; if (pairCount == 2) { break; } i++; //the pair has already been checked, i must be incremented an additional time to avoid using a card in this pair again. This prevents the program from identifying 3 of a kind as 2 pairs. } } if (pairCount == 2) { return(getKickers(hand, twopair)); } else { twopair.Clear(); } return(twopair); }
//get highest cards after sorting public static Hand getHighCard(Hand hand) { hand.sortByRank(); Hand highcard = new Hand(); highcard.setValue(1); highcard.Add(hand.getCard(0)); highcard.setValue(hand.getCard(0).getRank()); return(getKickers(hand, highcard)); }
//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); }
public static Hand getOnePair(Hand hand) { hand.sortByRank(); Hand onepair = new Hand(); onepair.setValue(2); for (int i = 0; i <= hand.Count() - 2; i++) { if (hand.getCard(i) == hand.getCard(i + 1)) { onepair.setValue(hand.getCard(i).getRank()); onepair.Add(hand.getCard(i)); onepair.Add(hand.getCard(i + 1)); break; } } return(getKickers(hand, onepair)); }
public static Hand getThreeOfAKind(Hand hand) { hand.sortByRank(); Hand threeofakind = new Hand(); threeofakind.setValue(4); for (int i = 0; i <= hand.Count() - 3; i++) { if (hand.getCard(i) == hand.getCard(i + 1) && hand.getCard(i) == hand.getCard(i + 2)) { threeofakind.setValue(hand.getCard(i).getRank()); threeofakind.Add(hand.getCard(i)); threeofakind.Add(hand.getCard(i + 1)); threeofakind.Add(hand.getCard(i + 2)); break; } } return(getKickers(hand, threeofakind)); }
public static Hand getFullHouse(Hand hand) { hand.sortByRank(); Hand fullhouse = new Hand(); fullhouse.setValue(7); bool threeofakind = false, pair = false; int threeofakindRank = 0; for (int i = 0; i <= hand.Count() - 3; i++) { if (hand.getCard(i) == hand.getCard(i + 1) && hand.getCard(i) == hand.getCard(i + 2)) { threeofakind = true; threeofakindRank = hand.getCard(i).getRank(); fullhouse.Add(hand.getCard(i)); fullhouse.Add(hand.getCard(i + 1)); fullhouse.Add(hand.getCard(i + 2)); fullhouse.setValue(hand.getCard(i).getRank()); break; } } for (int i = 0; i <= hand.Count() - 2; i++) { if (hand.getCard(i) == hand.getCard(i + 1) && hand.getCard(i).getRank() != threeofakindRank) { pair = true; fullhouse.Add(hand.getCard(i)); fullhouse.Add(hand.getCard(i + 1)); fullhouse.setValue(hand.getCard(i).getRank()); break; } } if (threeofakind == true && pair == true) { return(fullhouse); } else { fullhouse.Clear(); return(fullhouse); } }
//same as above except return the cards themselves public static Hand getFourOfAKind(Hand hand) { Hand fourofakind = new Hand(); fourofakind.setValue(8); hand.sortByRank(); for (int i = 0; i <= hand.Count() - 4; i++) { if (hand.getCard(i) == hand.getCard(i + 1) && hand.getCard(i) == hand.getCard(i + 2) && hand.getCard(i) == hand.getCard(i + 3)) { fourofakind.Add(hand.getCard(i)); fourofakind.Add(hand.getCard(i + 1)); fourofakind.Add(hand.getCard(i + 2)); fourofakind.Add(hand.getCard(i + 3)); fourofakind.setValue(hand.getCard(i).getRank()); break; } } return(getKickers(hand, fourofakind)); }
//get royal flush using recursion public static Hand getRoyalFlush(Hand hand) { hand.sortByRank(); Hand straightflush = new Hand(HandCombination.getStraightFlush(hand)); straightflush.setValue(10); if (straightflush.getCard(0).getRank() == 14) { return(straightflush); } else { straightflush.Clear(); return(straightflush); } }
//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); }
//use a counter to determine with suit forms a flush //then get all cards from the suit public static Hand getFlush(Hand hand) { hand.sortByRank(); Hand flush = new Hand(); flush.setValue(6); int diamondCount = 0, clubCount = 0, heartCount = 0, spadeCount = 0; for (int i = 0; i < hand.Count(); i++) { if ((SUIT)hand.getCard(i).getSuit() == SUIT.DIAMONDS) { diamondCount++; } else if ((SUIT)hand.getCard(i).getSuit() == SUIT.CLUBS) { clubCount++; } else if ((SUIT)hand.getCard(i).getSuit() == SUIT.HEARTS) { heartCount++; } else if ((SUIT)hand.getCard(i).getSuit() == SUIT.SPADES) { spadeCount++; } } if (diamondCount >= 5) { for (int i = 0; i < hand.Count(); i++) { if (hand.getCard(i).getSuit() == 1) { flush.Add(hand.getCard(i)); flush.setValue(hand.getCard(i).getRank()); } if (flush.Count() == 5) { break; } } //return flush; } else if (clubCount >= 5) { for (int i = 0; i <= hand.Count(); i++) { if (hand.getCard(i).getSuit() == 2) { flush.Add(hand.getCard(i)); flush.setValue(hand.getCard(i).getRank()); } if (flush.Count() == 5) { break; } } //return flush; } else if (heartCount >= 5) { for (int i = 0; i <= hand.Count(); i++) { if (hand.getCard(i).getSuit() == 3) { flush.Add(hand.getCard(i)); flush.setValue(hand.getCard(i).getRank()); } if (flush.Count() == 5) { break; } } //return flush; } else if (spadeCount >= 5) { for (int i = 0; i <= hand.Count(); i++) { if (hand.getCard(i).getSuit() == 4) { flush.Add(hand.getCard(i)); flush.setValue(hand.getCard(i).getRank()); } if (flush.Count() == 5) { break; } } //return flush; } return(flush); }
//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); }