public bool IsValid() { IEnumerable <CardRank> values = Cards.Select(x => x.Rank); IEnumerable <CardRank> cardRanks = values as CardRank[] ?? values.ToArray(); IEnumerable <IGrouping <CardRank, CardRank> > grouped = cardRanks.GroupBy(x => x).ToArray(); if (grouped.Count() < 2) { return(false); } foreach (IGrouping <CardRank, CardRank> grouping in grouped) { int count = grouping.Count(); if (count != 3) { continue; } CardRank cardRank = grouping.Key; Rank = cardRank; ThreeOfAKind = Cards.Where(x => x.Rank == cardRank); OtherCards = Cards.Where(x => x.Rank != cardRank); HighestCard = OtherCards.OrderBy(x => x.Rank).Last(); return(true); } return(false); }
public bool IsValid() { IEnumerable <CardRank> values = Cards.Select(x => x.Rank); IEnumerable <CardRank> cardRanks = values as CardRank[] ?? values.ToArray(); IEnumerable <IGrouping <CardRank, CardRank> > grouped = cardRanks.GroupBy(x => x).ToArray(); if (grouped.Count() != 4) // todo test for this { return(false); } foreach (IGrouping <CardRank, CardRank> grouping in grouped) { int count = grouping.Count(); if (count != 2) { continue; } PairOfCards = Cards.Where(x => x.Rank == grouping.Key); OtherCards = Cards.Where(x => x.Rank != grouping.Key).ToArray(); HighestCard = OtherCards.OrderBy(x => x.Rank).Last(); } return(PairOfCards.Any() && OtherCards.Any()); }