コード例 #1
0
 internal static int SortByComplexity(ManaPaymentPart a, ManaPaymentPart b)
 {
     return NumBits(a.color).CompareTo(NumBits(b.color));
 }
コード例 #2
0
        bool TryTakeMana(Dictionary<MTGColor, int> manaRemaining)
        {
            parts.Sort(ManaPaymentPart.SortByComplexity);

            ManaPaymentPart[] partsCopy = new ManaPaymentPart[parts.Count];
            parts.CopyTo(partsCopy);

            foreach(ManaPaymentPart part in partsCopy)
            {
                int partRemaining = part.amount;
                while (partRemaining > 0)
                {
                    bool didPay = false;
                    foreach (KeyValuePair<MTGColor, int> kv in manaRemaining)
                    {
                        if ((part.color & kv.Key) != 0 && kv.Value > 0)
                        {
                            // this type of mana can pay this cost; assume we don't have hybrid costs, so we can just greedily take it
                            if (kv.Value <= partRemaining)
                            {
                                partRemaining -= kv.Value;
                                manaRemaining.Remove(kv.Key);
                            }
                            else
                            {
                                manaRemaining[kv.Key] -= partRemaining;
                                partRemaining = 0;
                            }
                            didPay = true;
                            break;
                        }
                    }

                    if( !didPay )
                    {
                        return false;
                    }
                }
            }

            return true;
        }