Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Straight Fight by book1978
class Warrior:
health_base = 50
def __init__(self):
self.health = 50
self.attack = 5
self.defense = 0
@property
def is_alive(self):
return self.health > 0
class Knight(Warrior):
health_base = 50
def __init__(self):
super().__init__()
self.attack = 7
self.defense = 0
class Defender(Warrior):
health_base = 60
def __init__(self):
super().__init__()
self.attack = 3
self.defense = 2
self.health = 60
class Vampire(Warrior):
health_base = 40
def __init__(self):
super().__init__()
self.attack = 4
self.vampirism = 0.5
self.health = 40
self.defense = 0
class Lancer(Warrior):
health_base = 50
def __init__(self):
self.attack = 6
self.defense = 0
self.health = 50
class Healer(Warrior):
health_base = 60
def __init__(self):
self.attack = 0
self.defense = 0
self.health = 60
def heal(self, x):
x.health = x.health + 2 if x.health + 2 <= x.health_base else x.health_base
class Army:
def __init__(self):
self.squad = []
def add_units(self, t, q):
for i in range(q):
self.squad.append(t())
def check_health(self):
self.squad = list(filter(lambda x: x.health > 0, self.squad))
class Battle:
def fight(self, a1, a2):
while a1.squad and a2.squad:
fight(*[a1.squad[0], a2.squad[0]], *a1.squad[1:2] + a2.squad[1:2])
a1.check_health()
a2.check_health()
return bool(a1.squad)
def straight_fight(self, a1, a2):
while a1.squad and a2.squad:
for i in range(min(len(a1.squad), len(a2.squad))):
fight(*[a1.squad[i], a2.squad[i]])
a1.check_health()
a2.check_health()
return bool(a1.squad)
def fight(unit_1, unit_2, unit_1_1=None, unit_2_1=None):
while unit_1.is_alive and unit_2.is_alive:
unit_2.health -= a if (a := unit_1.attack - unit_2.defense) > 0 else 0
if unit_1_1 and isinstance(unit_1_1, Healer):
unit_1_1.heal(unit_1)
if isinstance(unit_1, Vampire):
unit_1.health += a * unit_1.vampirism
if unit_1.health > 40:
unit_1.health = 40
if isinstance(unit_1, Lancer) and unit_2_1:
unit_2_1.health -= a if (a := (unit_1.attack - unit_2.defense) * 0.5 - unit_2_1.defense) > 0 else 0
if unit_2.is_alive:
unit_1.health -= a if (a := unit_2.attack - unit_1.defense) > 0 else 0
if unit_2_1 and isinstance(unit_2_1, Healer):
unit_2_1.heal(unit_2)
if isinstance(unit_2, Vampire):
unit_2.health += a * unit_2.vampirism
if unit_2.health > 40:
unit_2.health = 40
if isinstance(unit_2, Lancer) and unit_1_1:
unit_1_1.health -= a if (a := (unit_2.attack - unit_1.defense) * 0.5 - unit_1_1.defense) > 0 else 0
return unit_1.is_alive
Aug. 24, 2023