Beispiel #1
0
        int[] ThreeOfAkind(Card[] hand)
        {
            var ordered = hand.OrderBy(c => c.Value).Select(c => c.Value).ToList();
            if (ordered[0] == ordered[2] || ordered[1] == ordered[3] || ordered[2] == ordered[4])
            {
                var result = new[] { 4, ordered[2] };
                return result.Concat(GetOrderedCardValues(hand.Where(c => c.Value != ordered[2]))).ToArray();

            }
            return null;
        }
Beispiel #2
0
 private int[] TwoPairs(Card[] hand)
 {
     var ordered = hand.OrderBy(c => c.Value).Select(c => c.Value).ToList();
     if (ordered.GroupBy(c => c).Count(c => c.Count() > 1) == 2)
     {
         IEnumerable<int> result = new[] {3};
         return result.Concat(
             ordered.GroupBy(c => c).Where(c => c.Count() > 1).Select(c => c.Key).OrderByDescending(c => c))
             .Concat(ordered.GroupBy(c => c).Where(c => c.Count() == 1).Select(c => c.Key)).ToArray();
     }
     return null;
 }
Beispiel #3
0
 int[] Straight(Card[] hand)
 {
     if (IsStraight(hand))
     {
         var result = new[] { 5 };
         if (hand.Select(c => c.Value).Contains(ACE) && hand.Select(c => c.Value).Contains(2))
             return result.Concat(new int[] {5}).ToArray();
         return result.Concat(new int[] {hand.Select(c=>c.Value).Max()}).ToArray();
     }
     return null;
 }
Beispiel #4
0
        int[] StraightFlush(Card[] hand)
        {
            if (IsFlush(hand) && IsStraight(hand))
                return new[] { 9, hand.Select(c => c.Value).Where(v => v!=ACE).Max() };

            return null;
        }
Beispiel #5
0
 private int[] OnePair(Card[] hand)
 {
     var ordered = hand.OrderBy(c => c.Value).Select(c => c.Value).ToList();
     if (ordered.GroupBy(c => c).Count(c => c.Count() > 1) == 1)
     {
         var result = new[] { 2 };
         return result.Concat(
             ordered.GroupBy(c => c).Where(c => c.Count() > 1).Select(c => c.Key).OrderByDescending(c => c)).
             Concat(ordered.GroupBy(c => c).Where(c => c.Count() == 1).OrderByDescending(c=>c.Key).Select(c=>c.Key)).ToArray();
     }
     return null;
 }
Beispiel #6
0
        int[] RoyalFlush(Card[] hand)
        {
            if (IsFlush(hand) && IsStraight(hand) && hand.Select(c => c.Value).Max() == ACE && hand.Select(c => c.Value).Contains(KING))
                return new[] {10};

            return null;
        }
Beispiel #7
0
 bool IsFlush(Card[] hand)
 {
     return hand.Select(c => c.Color).Distinct().Count() == 1;
 }
Beispiel #8
0
        bool IsStraight(Card[] hand)
        {
            var ordered = hand.OrderBy(c => c.Value).Select(c=>c.Value).ToList();
            if (ordered[0] + 1 == ordered[1] && ordered[1] + 1 == ordered[2] && ordered[2] + 1 == ordered[3] &&
                ordered[3] + 1 == ordered[4])
            {
                return true;
            }

            if (ordered[0] == 2 && ordered[1] == 3 && ordered[2] == 4 && ordered[3] == 5 && ordered[4] == ACE)
            {
                return true;
            }

            return false;
        }
Beispiel #9
0
        int[] FullHouse(Card[] hand)
        {
            var ordered = hand.OrderBy(c => c.Value).Select(c => c.Value).ToList();
            if (ordered[0] == ordered[2] && ordered[3] == ordered[4])
                return new[] { 7, ordered[0], ordered[3] };

            if (ordered[0] == ordered[1] && ordered[2] == ordered[4])
                return new[] { 7, ordered[2], ordered[0] };

            return null;
        }
Beispiel #10
0
 private int[] HighCard(Card[] hand)
 {
     return (new int[] {1}).Concat(GetOrderedCardValues(hand)).ToArray();
 }
Beispiel #11
0
        int[] FourOfKind(Card[] hand)
        {
            var ordered = hand.OrderBy(c => c.Value).Select(c => c.Value).ToList();
            if (ordered[0] == ordered[3] || ordered[1] == ordered[4])
                return new[] { 8, ordered[1] };

            return null;
        }
Beispiel #12
0
 int[] Flush(Card[] hand)
 {
     if (IsFlush(hand))
     {
         var result = new[] {6};
         return result.Concat(GetOrderedCardValues(hand)).ToArray();
     }
     return null;
 }
Beispiel #13
0
        public int CompareHands(string line)
        {
            var cards = line.Split(' ');
            var hand1 = new Card[]
            {new Card(cards[0]), new Card(cards[1]), new Card(cards[2]), new Card(cards[3]), new Card(cards[4])};
            var hand2 = new Card[] { new Card(cards[5]), new Card(cards[6]), new Card(cards[7]), new Card(cards[8]), new Card(cards[9]) };

            return Compare(CalculateHandValue(hand1), CalculateHandValue(hand2));
        }
Beispiel #14
0
 public int[] CalculateHandValue(Card[] hand)
 {
     return Ranks.Select(r => r(hand)).First(r => r != null);
 }
Beispiel #15
0
 public static int Compare(Card a, Card b)
 {
     return a.value.CompareTo(b.value);
 }