示例#1
0
        /// <summary>Creates a poker hand score from 5 cards.</summary>
        /// <param name="cards">The 5 cards to get a poker hand from.</param>
        /// <remarks>
        /// See: http://en.wikipedia.org/wiki/List_of_poker_hands
        /// </remarks>
        public static PokerHand CreateFrom5(IEnumerable <Card> cards)
        {
            if (cards == null || cards.Count() != 5 || cards.Any(c => c.IsEmpty()))
            {
                throw new ArgumentException("Five cards are required.", "card");
            }

            var straight5432Ace = false;

            var score = PokerHandType.HighCard;

            var pairs = CardPairs.Create(cards);
            var sets  = pairs.Max;

            var o = cards
                    .OrderByDescending(c => c)
                    .OrderByDescending(c => pairs[c.Height])
                    .ToList();

            switch (sets)
            {
            case 4: score = PokerHandType.FourOfAKind; break;

            case 3: score = o[3].Height == o[4].Height ? PokerHandType.FullHouse : PokerHandType.ThreeOfAKind; break;

            case 2: score = o[2].Height == o[3].Height ? PokerHandType.TwoPair : PokerHandType.OnePair; break;

            default:
                var isFlush = o.All(c => o[0].Suit == c.Suit);

                // Ace, 5,4,3,2
                straight5432Ace = o[0].Height == 14 && o[1].Height == 5;
                var straight = straight5432Ace || (o[0].Height - o[4].Height == 4);

                if (straight)
                {
                    score = isFlush ? PokerHandType.StraightFlush : PokerHandType.Straight;
                }
                else if (isFlush)
                {
                    score = PokerHandType.Flush;
                }
                break;
            }

            return(new PokerHand()
            {
                tp = score,
                c0 = o[straight5432Ace ? 1 : 0],
                c1 = o[straight5432Ace ? 2 : 1],
                c2 = o[straight5432Ace ? 3 : 2],
                c3 = o[straight5432Ace ? 4 : 3],
                c4 = o[straight5432Ace ? 0 : 4],
            });
        }
		/// <summary>Creates card pairs based on set of cards.</summary>
		/// <param name="cards">
		/// The cards to determine the pair for.
		/// </param>
		public static CardPairs Create(IEnumerable<Card> cards)
		{
			var pairs = new CardPairs()
			{
				// [2] = 2, [14] = Ace
				lookup =  new int[15]
			};

			foreach (var c in cards)
			{
				pairs.lookup[c.Height]++;
			}

			return pairs;
		}
示例#3
0
        /// <summary>Creates card pairs based on set of cards.</summary>
        /// <param name="cards">
        /// The cards to determine the pair for.
        /// </param>
        public static CardPairs Create(IEnumerable <Card> cards)
        {
            var pairs = new CardPairs()
            {
                // [2] = 2, [14] = Ace
                lookup = new int[15]
            };

            foreach (var c in cards)
            {
                pairs.lookup[c.Height]++;
            }

            return(pairs);
        }