纸牌游戏 - 子集总和 - 可以优化以下算法吗?

时间:2012-02-14 21:13:59

标签: c# algorithm optimization recursion subset-sum

好吧,我正在开发的纸牌游戏与Scopa非常相似,如果有人知道的话。 牌组包含40张牌,分为4种不同的套装,每套10张牌(ace =>值1,2 =>值2,3 = ......,4,5,6,7,knave,queen,king => ;价值10)。 有2名玩家(实际上是AI和人类玩家),他们手中有4张牌。

桌面上有4张免费牌,玩家只能遵守以下规则: 1)法院卡(knave,queen和king)只能使用相同的法庭卡(例如,如果我有一个女王,我只能从桌子上取一个女王)。 2)数字卡(从ace到7)可以使用相同的数字卡或较小的数字卡(例如,如果我有七个我可以拿七个或{一个ace,一个六个}或{一个三个,一个四个}或{一个ace,三个}}。

现在是时候找到人工智能在轮到的时候最终可以使用的卡片了:

    private List<List<Card>> CalculateAITake()
    {
        List<Int32> handValues = new List<Int32>();
        List<List<Card>> takes = new List<List<Card>>();

        /* here i take every hand card value, in a unique way
         * in order to avoid processing two or more times the
         * same value
         */
        foreach (Card card in m_AIHand)
        {
            Int32 cardValue = (Int32)card.Rank;

            if (!handValues.Contains(cardValue))
                handValues.Add(cardValue);
        }

        /* for each hand card value now, I calculate the
         * combinations of cards I can take from table
         */
        foreach (Int32 handValue in handValues)
        {
            // it's a court card, let's use a direct and faster approach
            if (handValue >= 8)
            {
                foreach (Card card in m_CardsOnTable)
                {
                    if ((Int32)card.Rank == handValue)
                    {
                        List<Card> take = new List<Card>();
                        take.Add(card);

                        takes.Add(take);
                    }
                }
            }
            else 
                // it's a numeric card, let's use recursion
                CalculateAITakeRecursion(takes, (new List<Card>(m_CardsOnTable)), 0, (new List<Card>()), handValue, 0);
        }

        return takes;
    }

    private void CalculateAITakeRecursion(List<List<Card>> takes, List<Card> cardsExcluded, Int32 cardsExcludedIndex, List<Card> cardsIncluded, Int32 sumWanted, Int32 sumPartial)
    {
        for (Int32 i = cardsExcludedIndex; i < cardsExcluded.Count; ++i)
        {
            Card cardExcluded = cardsExcluded[i];
            Int32 sumCurrent = sumPartial + (Int32)cardExcluded.Rank;

            /* the current sum is lesser than the hand card value
             * so I keep on recursing
             */
            if (sumCurrent < sumWanted)
            {
                List<Card> cardsExcludedCopy = new List<Card>(cardsExcluded);
                cardsExcludedCopy.Remove(cardExcluded);

                List<Card> cardsIncludedCopy = new List<Card>(cardsIncluded);
                cardsIncludedCopy.Add(cardExcluded);

                CalculateAITakeRecursion(takes, cardsExcludedCopy, ++cardsExcludedIndex, cardsIncludedCopy, sumWanted, sumCurrent);
            }
            /* the current sum is equal to the hand card value
             * we have a new valid combination!
             */
            else if (sumCurrent == sumWanted)
            {
                cardsIncluded.Add(cardExcluded);

                Boolean newTakeIsUnique = true;
                Int32 newTakeCount = cardsIncluded.Count;

                /* problem: sometimes in my results i can find both
                 * { ace of hearts, two of spades }
                 * { two of spades, ace of hearts }
                 * not good, I don't want it to happens because there
                 * is still a lot of work to do on those results!
                 * Contains() is not enought to guarantee unique results
                 * so I have to do this!
                 */
                foreach (List<Card> take in takes)
                {
                    if (take.Count == newTakeCount)
                    {
                        Int32 matchesCount = 0;

                        foreach (Card card in take)
                        {
                            if (cardsIncluded.Contains(card))
                                matchesCount++;      
                        }

                        if (newTakeCount == matchesCount)
                        {
                            newTakeIsUnique = false;
                            break;
                        }
                    }
                }

                if (newTakeIsUnique)
                    takes.Add(cardsIncluded);
            }
        }
    }

你认为这个算法可以以某种方式改进吗? 我试图尽可能地缩短这些代码,以便它易于调试和易于维护......而且,如果有人有一个更优雅的解决方案来避免重复组合我真的会非常感激它(我我不想同时获得{ace of ace,两个黑桃}和{两个黑桃,心中的ace} ......只有其中一个。)

很多,非常感谢提前!

1 个答案:

答案 0 :(得分:1)

我不会考虑你手中的每张数字卡并寻找总数的免费卡,而是考虑每张可能的免费卡总数,并在手中寻找与之匹配的数字卡。您可以使用某种bitset来加速检查手中匹配的卡片,如果您按升序对免费卡片进行排序,则可以避免添加与您跳过的卡片相匹配的卡片,并且您可以停止添加卡片你超过了手中最高的数字卡。

编辑:伪代码跟随(抱歉,我不擅长命名变量):

call find_subset_sum(1, List<int>, 0)
// Passing the total because it's easy to calculate as we go
sub find_subset_sum(int value, List<int> play, total)
  if total > max_hand_card
    return // trying to pick up too many cards
  if total in hand_set
    call store_play(play)
  if value > max_free_card
    return // no more cards available to pick up
  // try picking up higher value cards only
  find_subset_sum(value + 1, play, total)
  // now try picking up cards of this value
  for each free card
    if card value = value // only consider cards of this value
      total += value
      play.append(card)
      find_subset_sum(value + 1, play, total)
  // you could remove all the added cards here
  // this would avoid having to copy the list each time
  // you could then also move the first recursive call here too

看起来有点奇怪,但这是为了确保如果您只需要一张具有特定价值的卡,则不必考虑购买该值的每张可用卡。

您可以通过按升序对数组进行排序来进一步优化它。