From d3c4ae433ef1290e2c7f6c86707366a8bf2b3e31 Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Thu, 2 Sep 2010 11:01:43 -0400 Subject: [PATCH] Added hybrid and snow symbol support to mana costs --- mtg.py | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/mtg.py b/mtg.py index d9ab88c..e41cd6d 100644 --- a/mtg.py +++ b/mtg.py @@ -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):