Updated with support for variable (X/Y/Z) mana costs

This commit is contained in:
Correl Roush 2010-09-22 00:12:06 -04:00
parent 657904c985
commit d5206fc6df

21
mtg.py
View file

@ -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]),