internal static int SortByComplexity(ManaPaymentPart a, ManaPaymentPart b) { return NumBits(a.color).CompareTo(NumBits(b.color)); }
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; }