public override object ReadJson(JsonReader reader, Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { JObject jsonObject = JObject.Load(reader); Hand hand = new Hand(); Rank rank = (Rank)jsonObject["HighCard"].Value<int>("Rank"); Suit suit = (Suit)jsonObject["HighCard"].Value<int>("Suit"); Card highCard = new Card(rank, suit); Combination combination = (Combination)jsonObject["BestCombination"].Value<int>(); hand.BestCombination = combination; hand.HighCard = highCard; var cards = jsonObject["Cards"].Children(); foreach (JToken jt in cards) { Rank r = (Rank)jt["Rank"].Value<int>(); Suit s = (Suit)jt["Suit"].Value<int>(); Card c = new Card(r, s); hand.Add(c); } return hand; }
public static Hand GetUnassignedPlayerHand() { Hand emptyHand = new Hand(); emptyHand.Add(Card.GetUnnassignedCard()); emptyHand.Add(Card.GetUnnassignedCard()); emptyHand.HighCard = Card.GetUnnassignedCard(); emptyHand.BestCombination = Combination.Unassigned; return emptyHand; }
public static int Compare(Hand hand1, Hand hand2) { if (hand1.BestCombination == hand2.BestCombination) { switch (hand1.BestCombination) { case Combination.HighCard: case Combination.OnePair: case Combination.TwoPair: case Combination.ThreeOfAKind: case Combination.FourOfAKind: { if (GetHighestPairValue(hand1) == GetHighestPairValue(hand2)) { // the pair values are the same, check high card if (hand1.HighCard.Value == hand2.HighCard.Value) return 0; else if (hand1.HighCard > hand2.HighCard) return 1; else return -1; } else if (GetHighestPairValue(hand1) > GetHighestPairValue(hand2)) return 1; else return -1; } case Combination.Straight: { if (hand1.HighCard.Value == hand2.HighCard.Value) return 0; else if (hand1.HighCard > hand2.HighCard) return 1; else return -1; } case Combination.FullHouse: { return CompareFullHouses(hand1, hand2); } default: return 0; } } else if (hand1.BestCombination > hand2.BestCombination) return 1; else return -1; }
public static int CompareFullHouses(Hand fullHouse1, Hand fullHouse2) { fullHouse1.Sort(); fullHouse2.Sort(); Card fullHouse1ThreeOfAKind = NFIWhatToCallMethod(fullHouse1, Combination.ThreeOfAKind); Card fullHouse2ThreeOfAKind = NFIWhatToCallMethod(fullHouse2, Combination.ThreeOfAKind); if (fullHouse1ThreeOfAKind == fullHouse2ThreeOfAKind) { Card fullHouse1Pair = NFIWhatToCallMethod(fullHouse1, Combination.OnePair); Card fullHouse2Pair = NFIWhatToCallMethod(fullHouse2, Combination.OnePair); if (fullHouse1Pair.Value == fullHouse2Pair.Value) return 0; else if (fullHouse1Pair > fullHouse2Pair) return 1; else return -1; } else if (fullHouse1ThreeOfAKind > fullHouse2ThreeOfAKind) return 1; else return -1; }
public Hand GetBestHand() { checkCardList(); Hand bestPairedHand = new Hand(); bestPairedHand.BestCombination = Combination.HighCard; CardCollection winningCards; CheckHighestPairConfiguration(out bestPairedHand); if (IsRoyalFlush(out winningCards)) return new Hand(winningCards, Combination.RoyalFlush); if (IsStraightFlush(out winningCards)) return new Hand(winningCards, Combination.StraightFlush); if (bestPairedHand.BestCombination == Combination.FourOfAKind) return bestPairedHand; if (bestPairedHand.BestCombination == Combination.FullHouse) return bestPairedHand; if (IsFlush(out winningCards)) return new Hand(winningCards, Combination.Flush); if (IsStraight(out winningCards)) return new Hand(winningCards, Combination.Straight); // three of a kind, two pair, one pair and high card are all covered here return bestPairedHand; }
public bool CheckHighestPairConfiguration(out Hand hand) { checkCardList(); // make a copy //List<Card> cardList = new List<Card>(_cardList); CardCollection cardList = new CardCollection(_cardList); cardList.Sort(); // this is a list of cards that match the key value Dictionary<int, CardCollection> matches = new Dictionary<int, CardCollection>(); int numPairs = 0; int numThrees = 0; int numFours = 0; // Construct the dictionary foreach (Card card in cardList) { if (matches.ContainsKey(card.Value)) matches[card.Value].Add(card); else matches.Add(card.Value, new CardCollection(new Card[] { card })); //matches.Add(card.Value, new List<Card>(new Card[] { card })); } // Determine the number of pairs, threes, and fours //foreach (List<Card> cardPairs in matches.Values) foreach (CardCollection cardPairs in matches.Values) { if (cardPairs.Count == 2) numPairs++; if (cardPairs.Count == 3) numThrees++; if (cardPairs.Count == 4) numFours++; } //List<Card> resultCards = new List<Card>(); CardCollection resultCards = new CardCollection(); // Okay, here comes the real logic // One Pair and One Pair Only if (numPairs == 1 && numThrees == 0 && numFours == 0) { // Continue with the assumption that we'll only find one pair foreach (int cardValue in matches.Keys) { if (matches[cardValue].Count == 2) // this is our pair { resultCards.AddRange(matches[cardValue]); break; } } // Also add the next highest card for (int highCard = cardList.Count - 1; highCard >= 0; highCard--) { if (!resultCards.Contains(cardList[highCard])) { // Add the next highest card from the sorted list and break resultCards.Add(cardList[highCard]); break; } } // Construct the Best Hand for one pair and return true //hand = new BestHand(resultCards, HandType.OnePair); hand = new Hand(resultCards, Combination.OnePair); return true; } // Two Pair and two pair only else if (numPairs >= 2 && numThrees == 0 && numFours == 0) { int numPairsFound = 0; // Continue with the assumption that we'll only find two pairs foreach (int cardValue in new Stack<int>(matches.Keys)) { if (matches[cardValue].Count == 2) // this is our pair { resultCards.AddRange(matches[cardValue]); numPairs++; if (numPairsFound >= 2) break; } } // Also add the next highest card for (int highCard = cardList.Count - 1; highCard >= 0; highCard--) { if (!resultCards.Contains(cardList[highCard])) { // Add the next highest card from the sorted list and break resultCards.Add(cardList[highCard]); break; } } // Construct the Best Hand for one pair and return true //hand = new BestHand(resultCards, HandType.TwoPair); hand = new Hand(resultCards, Combination.TwoPair); return true; } // Three of a kind only else if (numPairs == 0 && numThrees >= 1 && numFours == 0) { foreach (int cardValue in new Stack<int>(matches.Keys)) { if (matches[cardValue].Count == 3) // this is our highest 3 { resultCards.AddRange(matches[cardValue]); break; } } // Also add the next highest card for (int highCard = cardList.Count - 1; highCard >= 0; highCard--) { if (!resultCards.Contains(cardList[highCard])) { // Add the next highest card from the sorted list and break resultCards.Add(cardList[highCard]); break; } } // Construct the Best Hand for one pair and return true //hand = new BestHand(resultCards, HandType.ThreeOfAKind); hand = new Hand(resultCards, Combination.ThreeOfAKind); return true; } // four of a kind only else if (numPairs <= 1 && numThrees <= 1 && numFours == 1) { foreach (int cardValue in new Stack<int>(matches.Keys)) { if (matches[cardValue].Count == 4) // this is the 4 { resultCards.AddRange(matches[cardValue]); break; } } // Also add the next highest card for (int highCard = cardList.Count - 1; highCard >= 0; highCard--) { if (!resultCards.Contains(cardList[highCard])) { resultCards.Add(cardList[highCard]); break; } } // Construct the Best Hand for one pair and return true //hand = new BestHand(resultCards, HandType.FourOfAKind); hand = new Hand(resultCards, Combination.FourOfAKind); return true; } // Full house only else if (numPairs >= 1 && numThrees == 1 && numFours == 0) { foreach (int cardValue in new Stack<int>(matches.Keys)) { if (matches[cardValue].Count == 3) // this is the 3 in the full house resultCards.AddRange(matches[cardValue]); if (matches[cardValue].Count == 2) // this is the 2 resultCards.AddRange(matches[cardValue]); if (resultCards.Count >= 5) break; } // Construct the Best Hand for one pair and return true //hand = new BestHand(resultCards, HandType.FullHouse); hand = new Hand(resultCards, Combination.FullHouse); return true; } else { // Just return the high card //List<Card> highCard = new List<Card>(); CardCollection highCard = new CardCollection(); highCard.Add(cardList[cardList.Count - 1]); //hand = new BestHand(highCard, HandType.HighCard); hand = new Hand(highCard, Combination.HighCard); return false; } }
public static Card NFIWhatToCallMethod(Hand fullHouse, Combination type) { // This method is used for finding the highest three-of-a-kind or highest // pair of a fullhouse hand. How would I name this method??..NFI? Exactly! if (fullHouse.BestCombination != Combination.FullHouse) throw new InvalidOperationException("This method is only designed to be used by Full House hands."); fullHouse.Sort(); Card highest = new Card(Rank.Unassigned, Suit.Unassigned); switch (type) { case Combination.ThreeOfAKind: { int count = 0; foreach (Card c in fullHouse) { if (c.Value == highest.Value) count++; else { highest = c; count = 1; } if (count == 3) break; } break; } case Combination.OnePair: { int count = 0; Card ignore = NFIWhatToCallMethod(fullHouse, Combination.ThreeOfAKind); foreach (Card c in fullHouse) { if (c.Value == ignore.Value) continue; else if (c.Value == highest.Value) count++; else { highest = c; count = 1; } if (count == 2) break; } break; } default: throw new InvalidOperationException("This method only accetps 'type' parameters of Combination.OnePair and Combination.ThreeOfAKind."); } return highest; }
public static int GetHighestPairValue(Hand hand) { Dictionary<int, int> cardCounts = new Dictionary<int, int>(); foreach (Card card in hand) { if (!cardCounts.ContainsKey(card.Value)) cardCounts.Add(card.Value, 1); else cardCounts[card.Value]++; } int highestPairValue = 0; foreach (int pairValue in cardCounts.Keys) { int realValue = pairValue; if (realValue == 1) realValue = Card.ACE_HIGH; if (cardCounts[pairValue] > 1 && realValue > highestPairValue) // this is the next highest pair highestPairValue = realValue; } return highestPairValue; }