Mana and Mana Cost
This commit is contained in:
parent
6b2427c3a1
commit
e02a22fe23
1 changed files with 90 additions and 0 deletions
90
mtg.py
90
mtg.py
|
@ -7,6 +7,96 @@ class Game:
|
|||
def __init__(self):
|
||||
self.players = []
|
||||
|
||||
class Mana:
|
||||
types = {
|
||||
'W': 'White',
|
||||
'U': 'Blue',
|
||||
'B': 'Black',
|
||||
'R': 'Red',
|
||||
'G': 'Green',
|
||||
}
|
||||
def __init__(self, mana=None):
|
||||
self.mana = {}
|
||||
for type in Mana.types.keys():
|
||||
self.mana[type] = 0
|
||||
if mana:
|
||||
# Can't just set self to result... ?
|
||||
result = self + mana
|
||||
self.mana = result.mana
|
||||
def __add__(self, other):
|
||||
result = Mana()
|
||||
if isinstance(other, str):
|
||||
for m in [c for c in other.upper() if c in Mana.types.keys()]:
|
||||
result.mana[m] = result.mana[m] + 1
|
||||
elif isinstance(other,Mana):
|
||||
for (key, value) in other.mana.iteritems():
|
||||
result.mana[key] = self.mana[key] + value
|
||||
else:
|
||||
# TODO: raise exception
|
||||
raise Exception()
|
||||
pass
|
||||
return result
|
||||
def __sub__(self, other):
|
||||
result = Mana()
|
||||
if isinstance(other, Mana):
|
||||
for (key, value) in other.mana.iteritems():
|
||||
result.mana[key] = self.mana[key] - value
|
||||
if result.mana[key] < 0:
|
||||
raise Exception('Insufficient {0} Mana'.format(Mana.types[key]))
|
||||
elif isinstance(other,ManaCost):
|
||||
for (key, value) in other.mana.mana.iteritems():
|
||||
result.mana[key] = self.mana[key] - value
|
||||
if result.mana[key] < 0:
|
||||
raise Exception('Insufficient {0} Mana'.format(Mana.types[key]))
|
||||
remaining = other.any
|
||||
for (key, value) in result.mana.iteritems():
|
||||
while result.mana[key] > 0 and remaining > 0:
|
||||
result.mana[key] = result.mana[key] - 1
|
||||
remaining = remaining - 1
|
||||
if remaining > 0:
|
||||
raise Exception('Insufficient Mana')
|
||||
return result
|
||||
def __repr__(self):
|
||||
return ''.join([type * count for (type, count) in self.mana.iteritems()]) if self.converted() > 0 else '0'
|
||||
def converted(self):
|
||||
return sum(self.mana.values())
|
||||
|
||||
class ManaCost:
|
||||
def __init__(self, cost=None):
|
||||
self.any = 0
|
||||
self.mana = Mana()
|
||||
if cost:
|
||||
result = self + cost
|
||||
self.any = result.any
|
||||
self.mana = result.mana
|
||||
def __add__(self, other):
|
||||
result = ManaCost()
|
||||
if isinstance(other, str):
|
||||
result.mana = Mana(other)
|
||||
value = ''
|
||||
for c in other:
|
||||
if c not in '0123456789': break
|
||||
value = value + c
|
||||
value = int(value) if len(value) > 0 else 0
|
||||
result.any = value
|
||||
elif isinstance(other, Mana):
|
||||
result.mana = other
|
||||
elif isinstance(other, ManaCost):
|
||||
result.any = other.many
|
||||
result.mana = other.many
|
||||
return result
|
||||
def __sub__(self, other):
|
||||
if isinstance(other, ManaCost):
|
||||
result.any = self.any - right.any
|
||||
result.mana = self.mana - right.mana
|
||||
def __repr__(self):
|
||||
return '{0}{1}'.format(
|
||||
self.any if self.any > 0 or self.mana.converted() == 0 else '',
|
||||
self.mana if self.mana.converted() > 0 else ''
|
||||
)
|
||||
def converted(self):
|
||||
return self.mana.converted() + self.any
|
||||
|
||||
class Player:
|
||||
def __init__(self, name, deck=None):
|
||||
self.name = name
|
||||
|
|
Loading…
Reference in a new issue