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 void Test_CardArraySerialization_IsCorrect() { Card[] arr = new Card[] { new Card(Rank.Eight, Suit.Clubs), new Card(Rank.Jack, Suit.Hearts) }; byte[] jsonBytes = serializer.GetBytes(arr); Card[] arr2 = serializer.GetObject<Card[]>(jsonBytes); Assert.IsTrue(arr2.Length == 2 && arr2[0].Rank == Rank.Eight && arr2[1].Rank == Rank.Jack); //Rank[] r = new Rank[]{Rank.Jack, Rank.Queen}; //byte[] jsonBytes = serializer.GetBytes(r); //Rank[] r2 = serializer.GetObject<Rank[]>(jsonBytes); //Assert.IsTrue(r2[0] == Rank.Jack && r2[1] == Rank.Queen); //Suit[] r = new Suit[] { Suit.Diamonds, Suit.Spades }; //byte[] jsonBytes = serializer.GetBytes(r); //Suit[] r2 = serializer.GetObject<Suit[]>(jsonBytes); //Assert.IsTrue(r2[0] == Suit.Diamonds && r2[1] == Suit.Spades); //Card c = new Card(Rank.Queen, Suit.Spades); //byte[] jsonBytes = serializer.GetBytes(c); //Card c2 = serializer.GetObject<Card>(jsonBytes); //Assert.IsTrue(c2.Rank == Rank.Queen && c2.Suit == Suit.Spades); }
public static string GetString(Card[] cards) { StringBuilder sb = new StringBuilder(); foreach (Card c in cards) sb.Append(c.ToString() + ", "); return sb.ToString().Trim().TrimEnd(','); }
public Hand(CardCollection cards, Combination HandType) : base(cards) { if (cards == null || cards.Count <= 0) throw new InvalidOperationException("You cannot add an empty collection of cards to a hand."); m_cmbBestCombination = HandType; base.Sort(); m_cdHighCard = base[base.Count - 1]; }
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 bool IsOneLessThan(Card otherCard) { //-- If this card is a King and the other is an Ace, return true. if (this.Rank == Rank.King && otherCard.Rank == Rank.Ace) return true; return (this.Value == otherCard.Value - 1); }
public bool IsOneGreaterThan(Card otherCard) { //-- If this card is an ace, and the other card is a king, return true. if (this.Rank == Rank.Ace && otherCard.Rank == Rank.King) return true; return (this.Value == otherCard.Value + 1); }
public Hand() { m_cdHighCard = new Card(Rank.Unassigned, Suit.Unassigned); m_cmbBestCombination = Combination.Unassigned; }