예제 #1
0
        HandScore ScoreHand(Card[] hand)
        {
            HandClass handClass = HandClass.HighCard;
            int classRank = 0;

            bool flush = hand.GroupBy(c => c.Suit).Max(g => g.Count()) == 5;
            bool straight = hand.Select(c => c.Rank).Distinct().Count() == 5 &&
                            hand.OrderBy(c => c.Rank).Max(c => c.Rank) - hand.OrderBy(c => c.Rank).Min(c => c.Rank) == 4;

            if (flush && straight && hand.OrderBy(c => c.Rank).Max().Rank == Ranks.Count-1)
            {
                handClass=HandClass.RoyalFlush;
                classRank = Ranks.Count - 1;
            }
            else if(flush&&straight)
            {
             handClass=HandClass.StraightFlush;
                classRank = hand.Max(c => c.Rank);
            }
            else if (hand.GroupBy(c => c.Rank).Max(g => g.Count()) == 4)
            {
                handClass = HandClass.FourKind;
                classRank = hand.GroupBy(c => c.Rank).OrderByDescending(g => g.Count()).First().First().Rank;
            }
            else if (hand.GroupBy(c => c.Rank).Select(g => g.Count()).All(c => c == 2 || c == 3))
            {
                handClass = HandClass.FullHouse;
                classRank = hand.GroupBy(c => c.Rank).OrderByDescending(g => g.Count()).First().First().Rank;
            }
            else if (flush)
            {
                handClass = HandClass.Flush;
                classRank = hand.Max(c => c.Rank);
            }
            else if (straight)
            {
                handClass = HandClass.Straight;
                classRank = hand.Max(c => c.Rank);
            }
            else if (hand.GroupBy(c => c.Rank).Max(g => g.Count()) == 3)
            {
                handClass = HandClass.ThreeKind;
                classRank = hand.GroupBy(c => c.Rank).OrderByDescending(g => g.Count()).First().First().Rank;
            }
            else if (hand.GroupBy(c => c.Rank).Count(g => g.Count() == 2) == 2)
            {
                handClass = HandClass.TwoPair;
                classRank = hand.GroupBy(c => c.Rank).Where(g => g.Count() == 2).OrderByDescending(g => g.First().Rank).First().First().Rank;
            }
            else if (hand.GroupBy(c => c.Rank).Any(g => g.Count() == 2))
            {
                handClass = HandClass.OnePair;
                classRank = hand.GroupBy(c => c.Rank).Where(g => g.Count() == 2).OrderByDescending(g => g.First().Rank).First().First().Rank;
            }

            return new HandScore {Class = handClass, ClassRank = classRank, HighRank = hand.Max(c => c.Rank)};
        }