Esempio n. 1
0
        public void HighCard4()
        {
            var whiteHand = new Hand("3H 4D 5S 9C KD");
            var blackHand = new Hand("2C 4H 5S 9C KH");

            Assert.Greater(whiteHand, blackHand, "White should be the winner");
        }
Esempio n. 2
0
        public void HighCardWins()
        {
            var whiteHand = new Hand("2H 3D 5S 9C KD");
            var blackHand = new Hand("2C 3H 4S 8C AH");

            Assert.Greater(blackHand, whiteHand, "Black should be the winner");
        }
Esempio n. 3
0
        public void HighCardTie()
        {
            var whiteHand = new Hand("3H 4D 5S 9C KD");
            var blackHand = new Hand("3C 4H 5S 9C KH");

            Assert.AreEqual(0, whiteHand.CompareTo(blackHand), "Should be a tie");
        }
Esempio n. 4
0
        public void FourOfAKindBeatsFullHouse()
        {
            var low = new Hand("AH KS KC AD KH");
            var high = new Hand("2H 2C 2D 2S 5H");

            Assert.Greater(high, low, "White should be the winner");
        }
Esempio n. 5
0
        public IScore Score(Hand hand)
        {
            var Cards = hand.Cards;
            CompositeScore handScore = new CompositeScore();

            var sets = (from c in Cards
                        group c by c.Value
                        into g
                            orderby g.Key
                            select new ValueSet{ Value = g.Key, Count = g.Count() }).ToList();

            var suits = (from c in Cards
                         group c by c.Suit
                         into g
                             where g.Count() == 5
                             select new SuitSet {Suit = g.Key, Count = g.Count(), High = g.Max(c => c.Value)}).
                FirstOrDefault();

            //linq-terbation?
            var scores = (from m in HandEvaluationMap.Map
                         from s in sets
                         where m.Condition(s, suits, sets)
                         select m.ScoreFactory(s, suits, sets)).ToList();

            scores.ForEach(handScore.Add);

            return handScore;
        }
Esempio n. 6
0
        public void FlushBeatsStraight()
        {
            var high = new Hand("2H 3H 4H 5H 7H");
            var low = new Hand("10H JS QS KC AH");

            Assert.Greater(high, low);
        }
Esempio n. 7
0
 private static bool isFlush(Hand hand)
 {
     return hand.Cards
                .Select(c => c.Suit)
                .GroupBy(s => s)
                .Count() == 1;
 }
Esempio n. 8
0
        public static Score GetScore(Hand hand)
        {
            if (isRoyalFlush(hand))
                return Score.RoyalFlush;

            if (isStraightFlush(hand))
                return Score.StraightFlush;

            if (isFourOfAKind(hand))
                return Score.FourOfAKind;

            if (isFullHouse(hand))
                return Score.FullHouse;

            if (isFlush(hand))
                return Score.Flush;

            if (isStraight(hand))
                return Score.Straight;

            if (isThreeOfAKind(hand))
                return Score.ThreeOfAKind;

            if (isTwoPair(hand))
                return Score.TwoPair;

            if (isPair(hand))
                return Score.Pair;

            return Score.HighCard;
        }
Esempio n. 9
0
        public void FullHouseBeatsFlush()
        {
            var whiteHand = new Hand("2H 4S 4C 2D 4H");
            var blackHand = new Hand("2H 3H 4H 5H 7H");

            Assert.Greater(whiteHand, blackHand, "White should be the winner");
        }
Esempio n. 10
0
 private static GroupResult[] duplicated(Hand hand)
 {
     return hand.Cards
                .Select(c => c.NumberValue)
                .OrderByDescending(n => n)
                .GroupBy(n => n)
                .Where(g => g.Count() > 1)
                .Select(g => new GroupResult { Value = g.Key, Count = g.Count() })
                .ToArray();
 }
Esempio n. 11
0
        public void ThreeOfAKindBeatsTwoPair()
        {
            var high = new Hand("2H 2S 5C 2D AH");
            var low = new Hand("2S 5H 5D 2S JS");

            Assert.Greater(high, low);
        }
Esempio n. 12
0
        public void StraightFlushBeatsFourOfAKind()
        {
            var high = new Hand("2H 3H 4H 5H 6H");
            var low = new Hand("AH AC AD AS KH");

            Assert.Greater(high, low, "White should be the winner");
        }
Esempio n. 13
0
        public void StraightBeatsThreeOfAKind()
        {
            var high = new Hand("2H 3S 4S 5S 6S");
            var low = new Hand("2H 3S 7S 7C 7H");

            Assert.Greater(high, low);
        }
Esempio n. 14
0
 private static bool isStraight(Hand hand)
 {
     var ordered = orderedNumbers(hand);
     if (Enumerable.SequenceEqual(ordered, Enumerable.Range(ordered[0], 5)))
         return true;
     return isLowestStraight(ordered);
 }
Esempio n. 15
0
 private static int[] orderedNumbers(Hand hand)
 {
     return hand.Cards
                .Select(c => c.NumberValue)
                .OrderBy(n => n)
                .ToArray();
 }
Esempio n. 16
0
        public void TwoPairTakeHighestPair()
        {
            var whiteHand = new Hand("2H 5S 5C 2D JH");
            var blackHand = new Hand("2S 4S 4S 2H AS");

            Assert.Greater(whiteHand, blackHand, "White should be the winner");
        }
Esempio n. 17
0
 private static bool isFullHouse(Hand hand)
 {
     return isThreeOfAKind(hand) && isPair(hand);
 }
Esempio n. 18
0
 private static bool isFourOfAKind(Hand hand)
 {
     return duplicated(hand).SingleOrDefault(v => v.Count == 4) != null;
 }
Esempio n. 19
0
        public void Tie()
        {
            var whiteHand = new Hand("2H 3D 5S 9C KD");
            var blackHand = new Hand("2D 3H 5C 9S KH");

            Assert.AreEqual(0, whiteHand.CompareTo(blackHand), "This hand should be a tie");
        }
Esempio n. 20
0
        public void TwoPairBeatsOnePair()
        {
            var whiteHand = new Hand("2H 3S 3C 2D JH");
            var blackHand = new Hand("AS 8S 3H QS AS");

            Assert.Greater(whiteHand, blackHand, "White should be the winner");
        }
Esempio n. 21
0
 private static bool isThreeOfAKind(Hand hand)
 {
     var values = duplicated(hand);
     return values.Length > 0 && values.SingleOrDefault(v => v.Count == 3) != null;
 }
Esempio n. 22
0
        public void HighPairBeatsLowPair()
        {
            var whiteHand = new Hand("2H 4S 5C 2D 6H");
            var blackHand = new Hand("2S 8S AS QS 8S");

            Assert.Greater(blackHand, whiteHand, "Black should be the winner");
        }
Esempio n. 23
0
 private static bool isTwoPair(Hand hand)
 {
     var values = duplicated(hand);
     return values.Length == 2 && values.All(v => v.Count == 2);
 }
Esempio n. 24
0
        public void PairBeatsHighCard()
        {
            var whiteHand = new Hand("2H 4S 5C 2D 6H");
            var blackHand = new Hand("2S 8H AS QS 3S");

            Assert.Greater(whiteHand, blackHand, "White should be the winner");
        }
Esempio n. 25
0
 private static bool isStraightFlush(Hand hand)
 {
     return isStraight(hand) && isFlush(hand);
 }
Esempio n. 26
0
        public void SamePairTakeHighCard()
        {
            var whiteHand = new Hand("2H QS KC 2D JH");
            var blackHand = new Hand("2S 8S AS QS 2S");

            Assert.Greater(blackHand, whiteHand, "Black should be the winner");
        }
Esempio n. 27
0
        public void SameTwoPairTakeHighestCard()
        {
            var whiteHand = new Hand("2H 5S 5C 2D AH");
            var blackHand = new Hand("2S 5H 5D 2S JS");

            Assert.Greater(whiteHand, blackHand, "White should be the winner");
        }
Esempio n. 28
0
        public void NotAStraight()
        {
            var high = new Hand("2H 3S 3S 5S 6S");
            var low = new Hand("2H 3S 4S 5C 7H");

            Assert.Greater(high, low);
        }
Esempio n. 29
0
 private static bool isRoyalFlush(Hand hand)
 {
     return isStraightFlush(hand) && orderedNumbers(hand).Last() == ACE;
 }