Exemple #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");
        }
Exemple #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");
        }
Exemple #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");
        }
Exemple #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");
        }
Exemple #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;
        }
Exemple #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);
        }
Exemple #7
0
 private static bool isFlush(Hand hand)
 {
     return hand.Cards
                .Select(c => c.Suit)
                .GroupBy(s => s)
                .Count() == 1;
 }
Exemple #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;
        }
Exemple #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");
        }
Exemple #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();
 }
Exemple #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);
        }
Exemple #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");
        }
Exemple #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);
        }
Exemple #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);
 }
Exemple #15
0
 private static int[] orderedNumbers(Hand hand)
 {
     return hand.Cards
                .Select(c => c.NumberValue)
                .OrderBy(n => n)
                .ToArray();
 }
Exemple #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");
        }
Exemple #17
0
 private static bool isFullHouse(Hand hand)
 {
     return isThreeOfAKind(hand) && isPair(hand);
 }
Exemple #18
0
 private static bool isFourOfAKind(Hand hand)
 {
     return duplicated(hand).SingleOrDefault(v => v.Count == 4) != null;
 }
Exemple #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");
        }
Exemple #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");
        }
Exemple #21
0
 private static bool isThreeOfAKind(Hand hand)
 {
     var values = duplicated(hand);
     return values.Length > 0 && values.SingleOrDefault(v => v.Count == 3) != null;
 }
Exemple #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");
        }
Exemple #23
0
 private static bool isTwoPair(Hand hand)
 {
     var values = duplicated(hand);
     return values.Length == 2 && values.All(v => v.Count == 2);
 }
Exemple #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");
        }
Exemple #25
0
 private static bool isStraightFlush(Hand hand)
 {
     return isStraight(hand) && isFlush(hand);
 }
Exemple #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");
        }
Exemple #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");
        }
Exemple #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);
        }
Exemple #29
0
 private static bool isRoyalFlush(Hand hand)
 {
     return isStraightFlush(hand) && orderedNumbers(hand).Last() == ACE;
 }