From d5206fc6df410836b0b24d1e8d9eae34e4a1ea65 Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Wed, 22 Sep 2010 00:12:06 -0400 Subject: [PATCH] Updated with support for variable (X/Y/Z) mana costs --- mtg.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/mtg.py b/mtg.py index be8bb2f..59b71c9 100644 --- a/mtg.py +++ b/mtg.py @@ -66,8 +66,12 @@ class Mana: class ManaCost: hybridPattern = '[({](.*?)\/(.*?)[)}]' + symbolPattern = r'X?Y?Z?\d*S*(\((\d+|[{0}])/(\d+|[{0}])\))*[{0}]*'.format(''.join(Mana.types.keys())) def __init__(self, cost=None): self.any = 0 + self.x = False + self.y = False + self.z = False self.mana = Mana() self.snow = 0 self.hybrid = [] @@ -77,6 +81,9 @@ class ManaCost: self.mana = result.mana self.hybrid = result.hybrid self.snow = result.snow + self.x = result.x + self.y = result.y + self.z = result.z def __add__(self, other): result = ManaCost() if isinstance(other, unicode): @@ -91,7 +98,13 @@ class ManaCost: result.hybrid = self.hybrid + [(ManaCost(a), ManaCost(b)) for (a,b) in hybrid] result.mana = self.mana + Mana(other) value = '' - for c in other: + for c in other.upper(): + if c == 'X': + result.x = True + if c == 'Y': + result.y = True + if c == 'Z': + result.z = True if c not in '0123456789': break value = value + c result.snow = self.snow + len([c for c in other.lower() if c == 's']) @@ -104,6 +117,9 @@ class ManaCost: result.mana = self.mana + other.mana result.hybrid = self.hybrid + other.hybrid result.snow = self.snow + other.snow + result.x = self.x or other.x + result.y = self.y or other.y + result.z = self.z or other.z return result def __sub__(self, other): result = ManaCost() @@ -114,7 +130,8 @@ class ManaCost: result.snow = self.snow - other.snow return result def __repr__(self): - return '{0}{1}{2}{3}'.format( + return '{0}{1}{2}{3}{4}'.format( + 'X' if self.x else '' + 'Y' if self.y else '' + 'Z' if self.z else '', self.any if self.any > 0 or (self.mana.converted() == 0 and not self.hybrid and not self.snow) else '', 'S' * self.snow, ''.join(['({0}/{1})'.format(a, b) for a,b in self.hybrid]),