Exemplo n.º 1
0
        public void HaveOneCardReturnsCardAsString()
        {
            var card = new Card(Rank.Ace, Suit.Clubs);

            var hand = new Hand();
            hand.AddCard(card);
            Assert.AreEqual(hand.ToString(), card.ToString());
        }
Exemplo n.º 2
0
 public void AddsFiveCardsHaveFiveCardsInTheHand()
 {
     var hand = new Hand();
     hand.AddCard(new Card(Rank.Ace, Suit.Clubs));
     hand.AddCard(new Card(Rank.Two, Suit.Clubs));
     hand.AddCard(new Card(Rank.Three, Suit.Clubs));
     hand.AddCard(new Card(Rank.Four, Suit.Clubs));
     hand.AddCard(new Card(Rank.Five, Suit.Clubs));
     Assert.AreEqual(hand.CardsCount, 5);
 }
Exemplo n.º 3
0
        public void HaveTwoCardsReturnsTwoCommaSeparatedCardsInAscendingOrderAsString()
        {
            var card1 = new Card(Rank.Three, Suit.Clubs);
            var card2 = new Card(Rank.Two, Suit.Clubs);

            var hand = new Hand();
            hand.AddCard(card1);
            hand.AddCard(card2);
            Assert.AreEqual(hand.ToString(), card2.ToString() + ", " + card1.ToString());
        }
Exemplo n.º 4
0
 /*
 * all cards are of the same suit
 */
 private static bool IsFlush(Hand h)
 {
     if (h[0].Suit == h[1].Suit &&
         h[1].Suit == h[2].Suit &&
         h[2].Suit == h[3].Suit &&
         h[3].Suit == h[4].Suit)
     {
         return true;
     }
     return false;
 }
Exemplo n.º 5
0
        /*
        * assumes:
        * -> the hand has five cards
        * -> the hand is ordered
        * -> the hand does not have duplicated cards
        */
        public static Score GetScore(Hand h)
        {
            if (h.CardsCount != 5) throw new ArgumentException("Hand must have five cards.");
            if (h[0] > h[1] || h[1] > h[2] || h[2] > h[3] || h[3] > h[4]) throw new ArgumentException("Hand must be ordered.");

            if (IsRoyalFlush(h))
            {
                return Score.RoyalFlush;
            }
            if (IsStraightFlush(h))
            {
                return Score.StraightFlush;
            }
            if (IsFourOfAKind(h))
            {
                return Score.FourOfAKind;
            }
            if (IsFullHouse(h))
            {
                return Score.FullHouse;
            }
            if (IsFlush(h))
            {
                return Score.Flush;
            }
            if (IsStraight(h))
            {
                return Score.Straight;
            }
            if (IsThreeOfAKind(h))
            {
                return Score.ThreeOfAKind;
            }
            if (IsTwoPairs(h))
            {
                return Score.TwoPair;
            }
            if (IsPair(h))
            {
                return Score.Pair;
            }
            return Score.HighCard;
        }
Exemplo n.º 6
0
 public void HandWithoutExactlyFiveCardsThrows()
 {
     var hand = new Hand();
     PokerLogic.GetScore(hand);
 }
Exemplo n.º 7
0
 private static Hand CreateHandWithTwoPairs()
 {
     Hand hand = new Hand();
     hand.AddCard(new Card(Rank.Nine, Suit.Clubs));
     hand.AddCard(new Card(Rank.Nine, Suit.Hearts));
     hand.AddCard(new Card(Rank.Ten, Suit.Clubs));
     hand.AddCard(new Card(Rank.Ten, Suit.Hearts));
     hand.AddCard(new Card(Rank.King, Suit.Clubs));
     return hand;
 }
Exemplo n.º 8
0
 private static Hand CreateHandWithThreeOfAKind()
 {
     Hand hand = new Hand();
     hand.AddCard(new Card(Rank.Nine, Suit.Clubs));
     hand.AddCard(new Card(Rank.Nine, Suit.Hearts));
     hand.AddCard(new Card(Rank.Nine, Suit.Diamonds));
     hand.AddCard(new Card(Rank.Queen, Suit.Clubs));
     hand.AddCard(new Card(Rank.King, Suit.Clubs));
     return hand;
 }
Exemplo n.º 9
0
 private static Hand CreateHandWithStraightFlush()
 {
     Hand hand = new Hand();
     hand.AddCard(new Card(Rank.Nine, Suit.Clubs));
     hand.AddCard(new Card(Rank.Ten, Suit.Clubs));
     hand.AddCard(new Card(Rank.Jack, Suit.Clubs));
     hand.AddCard(new Card(Rank.Queen, Suit.Clubs));
     hand.AddCard(new Card(Rank.King, Suit.Clubs));
     return hand;
 }
Exemplo n.º 10
0
 /*
 * whether the 1st and 2nd cards have the same rank
 * or the 2nd and 3rd
 * or the 3rd and 4th
 * or the 4th and 5th
 */
 private static bool IsPair(Hand h)
 {
     if (h[0].Rank == h[1].Rank)
     {
         return true;
     }
     if (h[1].Rank == h[2].Rank)
     {
         return true;
     }
     if (h[2].Rank == h[3].Rank)
     {
         return true;
     }
     if (h[3].Rank == h[4].Rank)
     {
         return true;
     }
     return false;
 }
Exemplo n.º 11
0
 /*
 * must be straight and flush
 */
 private static bool IsStraightFlush(Hand h)
 {
     if (IsStraight(h) && IsFlush(h))
     {
         return true;
     }
     return false;
 }
Exemplo n.º 12
0
 public void IndexerReturnsOnZeroBasedIndex()
 {
     var card = new Card(Rank.Ace, Suit.Clubs);
     var hand = new Hand();
     hand.AddCard(card);
     Assert.AreEqual(card, hand[0]);
 }
Exemplo n.º 13
0
 public void AddsOneCardHaveOneCardInTheHand()
 {
     var hand = new Hand();
     hand.AddCard(new Card(Rank.Ace, Suit.Clubs));
     Assert.AreEqual(hand.CardsCount, 1);
 }
Exemplo n.º 14
0
 private static Hand CreateHandWithAceHighStraight()
 {
     Hand hand = new Hand();
     hand.AddCard(new Card(Rank.Ten, Suit.Hearts));
     hand.AddCard(new Card(Rank.Jack, Suit.Clubs));
     hand.AddCard(new Card(Rank.Queen, Suit.Clubs));
     hand.AddCard(new Card(Rank.King, Suit.Clubs));
     hand.AddCard(new Card(Rank.Ace, Suit.Clubs));
     return hand;
 }
Exemplo n.º 15
0
 /*
 * whether the first four have the same rank
 * or the last four have the same rank
 */
 private static bool IsFourOfAKind(Hand h)
 {
     // first four
     if (h[0].Rank == h[1].Rank &&
         h[1].Rank == h[2].Rank &&
         h[2].Rank == h[3].Rank)
     {
         return true;
     }
     // last four
     if (h[1].Rank == h[2].Rank &&
         h[2].Rank == h[3].Rank &&
         h[3].Rank == h[4].Rank)
     {
         return true;
     }
     return false;
 }
Exemplo n.º 16
0
 /*
 * whether the first four cards make two pairs
 * or the first two and last two cards are both pairs
 * or the last four make two pairs
 */
 private static bool IsTwoPairs(Hand h)
 {
     // first four
     if (h[0].Rank == h[1].Rank &&
         h[2].Rank == h[3].Rank)
     {
         return true;
     }
     // first two and last two
     if (h[0].Rank == h[1].Rank &&
         h[3].Rank == h[4].Rank)
     {
         return true;
     }
     // last four
     if (h[1].Rank == h[2].Rank &&
         h[3].Rank == h[4].Rank)
     {
         return true;
     }
     return false;
 }
Exemplo n.º 17
0
 /*
 * whether the first three have the same rank
 * or the second three have the same rank
 * or the last three have the same rank
 */
 private static bool IsThreeOfAKind(Hand h)
 {
     // first three
     if (h[0].Rank == h[1].Rank &&
         h[1].Rank == h[2].Rank)
     {
         return true;
     }
     // second three
     if (h[1].Rank == h[2].Rank &&
         h[2].Rank == h[3].Rank)
     {
         return true;
     }
     // last three
     if (h[2].Rank == h[3].Rank &&
         h[3].Rank == h[4].Rank)
     {
         return true;
     }
     return false;
 }
Exemplo n.º 18
0
        public void IndexerReturnsCardsByIndex()
        {
            var higherCardAdded = new Card(Rank.King, Suit.Clubs);

            var hand = new Hand();
            hand.AddCard(higherCardAdded);
            hand.AddCard(new Card(Rank.Queen, Suit.Clubs));
            hand.AddCard(new Card(Rank.Jack, Suit.Clubs));
            Assert.AreEqual(higherCardAdded, hand[2]);
        }
Exemplo n.º 19
0
 /*
 * must be flush and straight and have certain cards
 */
 private static bool IsRoyalFlush(Hand h)
 {
     if (IsFlush(h) &&
         h[0].Rank == Rank.Ace &&
         h[1].Rank == Rank.Ten &&
         h[2].Rank == Rank.Jack &&
         h[3].Rank == Rank.Queen &&
         h[4].Rank == Rank.King)
     {
         return true;
     }
     return false;
 }
Exemplo n.º 20
0
        public void IndexerReturnsCardsInAscendingOrder()
        {
            var card1 = new Card(Rank.Three, Suit.Clubs);
            var card2 = new Card(Rank.Two, Suit.Clubs);

            var hand = new Hand();
            hand.AddCard(card1);
            hand.AddCard(card2);
            Assert.AreEqual(card2, hand[0]);
        }
Exemplo n.º 21
0
 public void IndexerFailsWhenHandIsEmpty()
 {
     var hand = new Hand();
     var card = hand[0];
 }
Exemplo n.º 22
0
 private static Hand CreateHandWithFlush()
 {
     Hand hand = new Hand();
     hand.AddCard(new Card(Rank.Ace, Suit.Clubs));
     hand.AddCard(new Card(Rank.Three, Suit.Clubs));
     hand.AddCard(new Card(Rank.Five, Suit.Clubs));
     hand.AddCard(new Card(Rank.Seven, Suit.Clubs));
     hand.AddCard(new Card(Rank.Nine, Suit.Clubs));
     return hand;
 }
Exemplo n.º 23
0
 /*
 * the rank vary by 1, because it is ordered
 */
 private static bool IsStraight(Hand h)
 {
     if (h[0].Rank == h[1].Rank - 1 &&
         h[1].Rank == h[2].Rank - 1 &&
         h[2].Rank == h[3].Rank - 1 &&
         h[3].Rank == h[4].Rank - 1)
     {
         return true;
     }
     // special case: A is less then 10
     if (h[1].Rank == Rank.Ten &&
         h[2].Rank == Rank.Jack &&
         h[3].Rank == Rank.Queen &&
         h[4].Rank == Rank.King &&
         h[0].Rank == Rank.Ace)
     {
         return true;
     }
     return false;
 }
Exemplo n.º 24
0
 public void ConstructorInitializesEmptyHand()
 {
     Hand hand = new Hand();
     Assert.AreEqual(hand.CardsCount, 0);
 }
Exemplo n.º 25
0
 private static Hand CreateHandWithFullHouse()
 {
     Hand hand = new Hand();
     hand.AddCard(new Card(Rank.Nine, Suit.Clubs));
     hand.AddCard(new Card(Rank.Nine, Suit.Hearts));
     hand.AddCard(new Card(Rank.Ten, Suit.Clubs));
     hand.AddCard(new Card(Rank.Ten, Suit.Hearts));
     hand.AddCard(new Card(Rank.Ten, Suit.Spades));
     return hand;
 }
Exemplo n.º 26
0
 private static Hand CreateHandWithNoCombination()
 {
     Hand hand = new Hand();
     hand.AddCard(new Card(Rank.Ace, Suit.Clubs));
     hand.AddCard(new Card(Rank.Three, Suit.Hearts));
     hand.AddCard(new Card(Rank.Five, Suit.Diamonds));
     hand.AddCard(new Card(Rank.Seven, Suit.Spades));
     hand.AddCard(new Card(Rank.Nine, Suit.Clubs));
     return hand;
 }
Exemplo n.º 27
0
 private static Hand CreateHandWithOnePair()
 {
     Hand hand = new Hand();
     hand.AddCard(new Card(Rank.Nine, Suit.Clubs));
     hand.AddCard(new Card(Rank.Nine, Suit.Hearts));
     hand.AddCard(new Card(Rank.Jack, Suit.Clubs));
     hand.AddCard(new Card(Rank.Queen, Suit.Clubs));
     hand.AddCard(new Card(Rank.King, Suit.Clubs));
     return hand;
 }
Exemplo n.º 28
0
 public void IndexerFailsWhenPassedIndexLargerThanOrEqualCountOfCardsAtHand()
 {
     var hand = new Hand();
     hand.AddCard(new Card(Rank.Ace, Suit.Clubs));
     var card = hand[1];
 }
Exemplo n.º 29
0
 /*
 * whether it is three of a kind followed by a pair
 * or a pair followed by three of a kind
 */
 private static bool IsFullHouse(Hand h)
 {
     // two then three
     if (h[0].Rank == h[1].Rank &&
         h[2].Rank == h[3].Rank &&
         h[3].Rank == h[4].Rank)
     {
         return true;
     }
     // three then two
     if (h[0].Rank == h[1].Rank &&
         h[1].Rank == h[2].Rank &&
         h[3].Rank == h[4].Rank)
     {
         return true;
     }
     return false;
 }