コード例 #1
0
        public IList <Card> GetPossibleCards(IBoardContext boardContext, IPlayerContext playerContext)
        {
            if (boardContext == null)
            {
                throw new ArgumentNullException(nameof(boardContext));
            }
            if (playerContext == null)
            {
                throw new ArgumentNullException(nameof(playerContext));
            }

            var discardEstimates =
                _strategy.EstimateDiscardProbability(boardContext);

            var knowAboutRankOrColorDict = new Dictionary <CardInHand, bool>();

            foreach (var cardInHand in playerContext.Hand)
            {
                knowAboutRankOrColorDict[cardInHand] = playerContext.KnowAboutRankOrColor(cardInHand);
            }

            // сперва ищем те, которые точно можно выкинуть
            var cardsToDiscard = discardEstimates
                                 .Where(entry => entry.Value == Probability.Maximum)
                                 .Select(dp => dp.Key.Card)
                                 .ToList();

            if (cardsToDiscard.Any())
            {
                return(cardsToDiscard);
            }

            Probability maxProbability;

            // не нашли.
            // тогда поищем среди тех карт, на которые не было прямых подсказок
            if (playerContext.Hand.Any(cardInHand => !knowAboutRankOrColorDict[cardInHand]))
            {
                maxProbability = discardEstimates
                                 .Where(entry => !knowAboutRankOrColorDict[entry.Key])
                                 .Max(entry => entry.Value);

                cardsToDiscard = discardEstimates
                                 .Where(entry => !knowAboutRankOrColorDict[entry.Key] && entry.Value == maxProbability)
                                 .Select(entry => entry.Key.Card).ToList();

                if (cardsToDiscard.Any())
                {
                    return(cardsToDiscard);
                }
            }


            // снова не нашли.
            // тогда ищем карту с наибольшей вероятностью

            maxProbability = discardEstimates.Max(entry => entry.Value);

            return(discardEstimates
                   .Where(entry => entry.Value == maxProbability)
                   .Select(entry => entry.Key.Card)
                   .ToList());
        }