//--------------------------------------------------------------------- internal BestHandGFlower(HandRankTypeGFlower rankType, ICollection <CardData> cards) { if (cards.Count != HandCardLength) { //throw new ArgumentException("Cards collection should contains exactly 5 elements", nameof(cards)); } this.Cards = cards.ToList(); this.RankType = rankType; }
//--------------------------------------------------------------------- // <summary> // Finds the best possible hand given a player's cards and all revealed comunity cards. // </summary> // <param name="cards">A player's cards + all revealed comunity cards (at lesat 5 in total)</param> // <returns>Returns an object of type BestHandGFlower</returns> public BestHandGFlower GetBestHand(IEnumerable <Card> cards) { var cardSuitCounts = new int[CardSuitCousts]; var cardTypeMap = new Dictionary <CardType, List <Card> >(); foreach (var card in cards) { cardSuitCounts[(int)card.Suit]++; CardType card_type = (CardType)card.Type; List <Card> listTypeCard = null; cardTypeMap.TryGetValue(card_type, out listTypeCard); if (listTypeCard == null) { listTypeCard = new List <Card>(); } listTypeCard.Add(card); cardTypeMap[card_type] = listTypeCard; } List <CardData> list_card = cards.OrderByDescending(x => x.Type).Select(x => x.GetCardData()).ToList(); // Flushes if (cardSuitCounts.Any(x => x >= ComparableCards)) { // Straight flush bool is_straight = this.IsStraightCards(cards.ToList(), cardTypeMap); if (is_straight) { return(new BestHandGFlower(HandRankTypeGFlower.StraightFlush, list_card, list_card)); } return(new BestHandGFlower(HandRankTypeGFlower.Flush, list_card, list_card)); } { // Straight bool is_straight = this.IsStraightCards(cards.ToList(), cardTypeMap); if (is_straight) { return(new BestHandGFlower(HandRankTypeGFlower.Straight, list_card, list_card)); } } // 3 of a kind var threeOfAKindTypes = this.GetTypesWithNCards(cardTypeMap, 3); if (threeOfAKindTypes.Count > 0) { var bestThreeOfAKindType = threeOfAKindTypes[0]; var bestCards = cards.Where(x => x.Type != bestThreeOfAKindType.type) .Select(x => x.GetCardData()) .OrderByDescending(x => x.type) .Take(ComparableCards - 3).ToList(); bestCards.AddRange(threeOfAKindTypes); HandRankTypeGFlower type = HandRankTypeGFlower.BaoZi; //if (bestThreeOfAKindType.type == (byte)CardType.Ace) //{ // type = HandRankTypeGFlowerH.RoyalBaoZi; //} return(new BestHandGFlower(type, bestCards, threeOfAKindTypes)); } // Pair var pairTypes = this.GetTypesWithNCards(cardTypeMap, 2); if (pairTypes.Count == 2) { var bestCards = new List <CardData>(); bestCards.AddRange(pairTypes); bestCards.AddRange(cards.Where(x => x.Type != pairTypes[0].type). OrderByDescending(x => x.Type).Select(x => x.GetCardData()).Take(3).ToList()); return(new BestHandGFlower(HandRankTypeGFlower.Pair, bestCards, pairTypes)); } else { // High card var bestCards = cards.Select(x => x.GetCardData()).OrderByDescending(x => x.type).Take(3).ToList(); return(new BestHandGFlower(HandRankTypeGFlower.HighCard, bestCards, bestCards)); } }