Added hybrid and snow symbol support to mana costs

This commit is contained in:
Correl Roush 2010-09-02 11:01:43 -04:00
parent 234a77cdbc
commit d3c4ae433e

32
mtg.py
View file

@ -63,42 +63,62 @@ class Mana:
return sum(self.mana.values())
class ManaCost:
hybridPattern = '[({](.*?)\/(.*?)[)}]'
def __init__(self, cost=None):
self.any = 0
self.mana = Mana()
self.snow = 0
self.hybrid = []
if cost:
result = self + cost
self.any = result.any
self.mana = result.mana
self.hybrid = result.hybrid
self.snow = result.snow
def __add__(self, other):
result = ManaCost()
if isinstance(other, str):
result.mana = Mana(other)
symbols = []
hybrid = re.findall(ManaCost.hybridPattern, other)
# Remove the hybrid costs from the mana cost string before continuing
other = re.sub(ManaCost.hybridPattern, '', other)
# Clear any other unecessary tokens
other = re.sub('[({})]', '', other)
result.hybrid = self.hybrid + [(ManaCost(a), ManaCost(b)) for (a,b) in hybrid]
result.mana = self.mana + Mana(other)
value = ''
for c in other:
if c not in '0123456789': break
value = value + c
result.snow = self.snow + len([c for c in other.lower() if c == 's'])
value = int(value) if len(value) > 0 else 0
result.any = value
result.any = self.any + value
elif isinstance(other, Mana):
result.mana = other
result.mana = self.mana + other
elif isinstance(other, ManaCost):
result.any = self.any + other.any
result.mana = self.mana + other.mana
result.hybrid = self.hybrid + other.hybrid
result.snow = self.snow + other.snow
return result
def __sub__(self, other):
result = ManaCost()
if isinstance(other, ManaCost):
result.any = self.any - other.any
result.mana = self.mana - other.mana
result.hybrid = self.hybrid - other.hybrid
result.snow = self.snow - other.snow
return result
def __repr__(self):
return '{0}{1}'.format(
self.any if self.any > 0 or self.mana.converted() == 0 else '',
return '{0}{1}{2}{3}'.format(
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]),
self.mana if self.mana.converted() > 0 else ''
)
def converted(self):
return self.mana.converted() + self.any
hybrid = sum([min(a, b).converted() for a, b in self.hybrid])
return self.mana.converted() + self.any + hybrid + self.snow
class Player:
def __init__(self, name, game, deck=None):