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