Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
OOP solution in Clear category for Straight Fight by mishasha13
class Warrior:
def __init__(self, health = 50, attack = 5):
self.health = health
self.attack = attack
self.max_health = health
@property
def is_alive(self) -> bool:
return self.health > 0
def take_attack(self, damage):
return damage
def make_attack_unit(self, unit):
unit.health -= unit.take_attack(self.attack)
def make_attack_army(self, units):
self.make_attack_unit(units[0])
class Knight(Warrior):
def __init__(self):
super().__init__(attack = 7)
class Defender(Warrior):
def __init__(self):
super().__init__(health = 60, attack = 3)
self.defense = 2
def take_attack(self, damage):
return max(damage - self.defense, 0)
class Vampire(Warrior):
def __init__(self):
super().__init__(health = 40, attack = 4)
self.vampirism = 0.5
def make_attack_unit(self, unit):
damage = unit.take_attack(self.attack)
unit.health -= damage
self.health = min( self.health + int(damage * self.vampirism), self.max_health)
class Lancer(Warrior):
def __init__(self):
super().__init__(health = 50, attack = 6)
def make_attack_army(self, units):
damage = units[0].take_attack(self.attack)
units[0].health -= damage
if len(units) > 1:
units[1].health -= units[1].take_attack(damage * 0.5)
class Healer(Warrior):
def __init__(self):
super().__init__(health = 60, attack = 0)
self.heal_power = 2
def heal(self, unit):
unit.health = min(unit.health + self.heal_power,unit.max_health)
def fight(unit_1, unit_2):
while unit_1.is_alive and unit_2.is_alive:
unit_1.make_attack_unit(unit_2)
if unit_2.is_alive:
unit_2.make_attack_unit(unit_1)
return unit_1.is_alive
class Army():
def __init__(self):
self.units = []
def add_units(self, unit, n: int):
self.units += [unit() for i in range(n)]
def bury(self):
for i in reversed(range(len(self.units))):
if not self.units[i].is_alive:
del(self.units[i])
@property
def is_alive(self) -> bool:
self.bury()
return len(self.units) > 0
def heal(self):
if (len(self.units) > 1 and self.units[1].__class__ == Healer().__class__):
self.units[1].heal(self.units[0])
class Battle():
def fight(self, arm1, arm2):
while arm1.is_alive and arm2.is_alive:
arm1.units[0].make_attack_army(arm2.units)
arm1.heal()
if arm2.units[0].is_alive:
arm2.units[0].make_attack_army(arm1.units)
arm2.heal()
return arm1.is_alive
def straight_fight(self, arm1, arm2):
while arm1.is_alive and arm2.is_alive:
for i in range(min(len(arm1.units), len(arm2.units))):
fight(arm1.units[i], arm2.units[i])
return arm1.is_alive
Aug. 22, 2018
Comments: