Skip to content

Instantly share code, notes, and snippets.

@XanderVi
Last active July 2, 2018 06:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save XanderVi/0d671662f9266642f774a1f7fbd26c5e to your computer and use it in GitHub Desktop.
Save XanderVi/0d671662f9266642f774a1f7fbd26c5e to your computer and use it in GitHub Desktop.
class Army:
def __init__(self, units=[]):
self.units = []
def add_unit(self, unit_type, amount):
self.units = [unit_type() for i in range(amount)]
class Warrior:
def __init__(self):
self.health = 50
self.attack = 5
def hit(self, enemy):
vampirism = 0
defense = 0
try:
vampirism = self.vampirism
except:
pass
try:
defense = enemy.defense
except:
pass
enemy.health = enemy.health - self.attack + defense
self.health += vampirism
class Defender(Warrior):
def __init__(self):
self.health = 60
self.attack = 3
self.defense = 2
class Vampire(Warrior):
def __init__(self):
self.health = 40
self.attack = 4
self.vampirism = 2
def fight(unit_1, unit_2):
while unit_1.health > 0 and unit_2.health > 0:
unit_1.hit(unit_2)
if unit_2.health <= 0:
break
unit_2.hit(unit_1)
if unit_1.health > 0:
return True
return False
def battle(army_1, army_2):
while len(army_1.units) > 0 and len(army_2.units) > 0:
result = fight(army_1.units[0], army_2.units[0])
if result:
del army_2.units[0]
else:
del army_1.units[0]
if len(army_1.units) > 0:
return True
return False
#test
army_1 = Army()
army_2 = Army()
army_1.add_unit(Warrior, 1)
army_2.add_unit(Vampire, 2)
print(battle(army_1, army_2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment