private bool HasFlush(List<StandartCard> cards, out Hand hand) { hand = new Hand(); var grouped = cards .GroupBy(x => x.Suit, x => x, (suit, enumerable) => new { Suit = suit, Cards = enumerable.ToList() }) .ToList(); if (grouped.Any(x => x.Cards.Count() >= 5)) { hand = new Hand() { Rank = HandRank.Flush, HandCards = grouped .First(x => x.Cards.Count() >= 5) .Cards .OrderByDescending(x => x.Rank) .Take(5) .ToList() }; return true; } return false; }
private bool HasThreeOfAKind(List<StandartCard> cards, out Hand hand) { hand = new Hand(); var grouped = cards.GroupBy(x => x.Rank, x => x, (rank, enumerable) => new {Rank = rank, Cards = enumerable.ToList()}) .OrderByDescending(x => x.Rank) .FirstOrDefault(x => x.Cards.Count >= 3); if (grouped != null) { hand = new Hand() { Rank = HandRank.ThreeOfAKind, HandCards = grouped.Cards }; return true; } return false; }
private bool HasTwoPair(List<StandartCard> cards, out Hand hand) { hand = new Hand(); var grouped = cards.GroupBy(x => x.Rank, x => x, (rank, enumerable) => new { Rank = rank, Cards = enumerable.ToList() }) .OrderByDescending(x => x.Rank) .Where(x => x.Cards.Count >= 2) .ToList(); if (grouped.Count >= 2) { hand = new Hand() { Rank = HandRank.TwoPair, HandCards = new List<StandartCard>( new [] { grouped[0].Cards[0], grouped[0].Cards[1], grouped[1].Cards[0], grouped[1].Cards[1], }) }; return true; } return false; }
private bool HasStraight(List<StandartCard> cards, out Hand hand) { hand = new Hand(); var sorted = cards.OrderByDescending(x => x.Rank).ToList(); for (int i = 0; i <= sorted.Count - 5; i++) { var c = sorted.Skip(i).Take(5).ToList(); if (c.Select(x => (int)x.Rank).AreInSequence()) { hand = new Hand() { Rank = HandRank.Straight, HandCards = c }; return true; } } return false; }
private bool HasStraightFlush(List<StandartCard> cards, out Hand hand) { hand = new Hand(); var grouped = cards .GroupBy(x => x.Suit, x=>x, (suit, enumerable) => new { Suit = suit, Cards = enumerable .OrderByDescending(x => x.Rank) .DistinctBy(x => x.Rank).ToList() }) .ToList(); foreach (var group in grouped) { for (int i = 0; i <= group.Cards.Count - 5; i++) { var c = group.Cards.Skip(i).Take(5).ToList(); if (c.Select(x => (int)x.Rank).AreInSequence() && c.All(x => x.Suit == c[0].Suit)) { hand = new Hand() { Rank = HandRank.StraightFlush, HandCards = c }; return true; } } } return false; }
private bool HasRoyalFlush(List<StandartCard> cards, out Hand hand) { hand = new Hand(); foreach (Suit suit in Enum.GetValues(typeof(Suit))) { var royalFlushHand = cards .Where(x => x.Suit == suit) .Where(x => x.Rank >= Rank.Ten) .DistinctBy(x => x.Rank).ToList().ToList(); if (royalFlushHand.Count == 5) { hand.Rank = HandRank.RoyalFlush; hand.HandCards = royalFlushHand; return true; } } return false; }
private bool HasPair(List<StandartCard> cards, out Hand hand) { hand = new Hand(); var grouped = cards.GroupBy(x => x.Rank, x => x, (rank, enumerable) => new { Rank = rank, Cards = enumerable.ToList() }) .OrderByDescending(x => x.Rank) .Where(x => x.Cards.Count >= 2) .ToList(); if (grouped.Count >= 1) { hand = new Hand() { Rank = HandRank.OnePair, HandCards = grouped.First().Cards.Concat(cards.Where(x => !grouped.First().Cards.Contains(x)).OrderByDescending(x => x.Rank).Take(1)).ToList() }; return true; } return false; }
private bool HasLowStraightFlush(List<StandartCard> cards, out Hand hand) { hand = new Hand(); foreach (Suit suit in Enum.GetValues(typeof(Suit))) { var lowStraightFlushHand = cards .Where(x => x.Suit == suit) .Where(x => x.Rank <= Rank.Five || x.Rank == Rank.Ace) .DistinctBy(x => x.Rank).ToList().ToList(); if (lowStraightFlushHand.Count == 5) { hand.Rank = HandRank.LowStraightFlush; hand.HandCards = lowStraightFlushHand; return true; } } return false; }
private bool HasLowStraight(List<StandartCard> cards, out Hand hand) { hand = new Hand(); var c = cards.Where(x => x.Rank <= Rank.Five || x.Rank == Rank.Ace) .DistinctBy(x => x.Rank).ToList(); if (c.Count == 5) { hand = new Hand() { HandCards = c, Rank = HandRank.LowStraight }; return true; } return false; }
private bool HasFullHouse(List<StandartCard> cards, out Hand hand) { hand = new Hand(); var grouped = cards.GroupBy(x => x.Rank, x => x, (rank, enumerable) => new { Rank = rank, Cards = enumerable.ToList() }) .OrderByDescending(x => x.Rank) .Where(x => x.Cards.Count >= 2) .ToList(); var pair = grouped.FirstOrDefault(x => x.Cards.Count == 2); var threeOfAKind = grouped.FirstOrDefault(x => x.Cards.Count == 3); if (pair != null && threeOfAKind != null) { hand = new Hand() { Rank = HandRank.FullHouse, HandCards = pair.Cards.Concat(threeOfAKind.Cards).ToList() }; return true; } return false; }