わかりました、私が開発しているカードゲームは、誰かがそれを知っていれば、スコパにかなり似ています. デッキには 40 枚のカードが含まれており、それぞれ 10 枚のカードの 4 つの異なるスートに分割されています (エース => 値 1、2 => 値 2、3 = ...、4、5、6、7、ナイフ、クイーン、キング => 値 10)。 )。2 人のプレイヤー (実際には AI と人間のプレイヤー) がいて、手札に 4 枚のカードがあります。
テーブルには 4 枚のフリー カードがあり、プレイヤーは次のルールに従ってのみそれらを受け取ることができます。テーブルからクイーンを取る)。2) 数字カード (エースから 7) は、同じ数字のカードまたはそれより小さい数字のカードを合計で取ることができます (たとえば、7 を持っている場合、7 または {エース、6} または {3、4) を取ることができます。 } または { エース、スリー ツー })。
AI がターン中に最終的に取ることができるカードを見つける時が来ました。
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);
}
}
}
このアルゴリズムを何らかの方法で改善できると思いますか? このコードをできる限り短くして、デバッグと保守を容易にしようとしています...また、誰かが重複した組み合わせを避けるためのよりエレガントなソリューションを持っている場合は、本当に感謝します(私は{ ハートのエース、スペードの 2 つ } と { スペードの 2 つ、ハートのエース } の両方を取得したくありません... そのうちの 1 つだけです)。
よろしくお願いします!