Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Simple solution in Clear category for The Lancers by Pouf
from dataclasses import dataclass
@dataclass
class Warrior:
_health: int = 50
attack: int = 5
defense: int = 0
vampirism: int = int()
piercing: int = int()
army = None
@property
def is_alive(self) -> bool:
return self._health > 0
@property
def health(self) -> int:
return self._health
@health.setter
def health(self, value: int):
self._health = value
if not self.is_alive and self.army is not None:
self.army.remove_unit(self)
def hit(self, target):
target.health -= (damage := max(0, self.attack - target.defense))
self.health += int(damage * self.vampirism / 100)
if self.piercing and target.army and len(target.army) > 1:
target.army[1].health -= int(damage * self.piercing / 100)
@dataclass
class Knight(Warrior):
attack: int = 7
@dataclass
class Defender(Warrior):
_health: int = 60
attack: int = 3
defense: int = 2
@dataclass
class Vampire(Warrior):
_health: int = 40
attack: int = 4
vampirism: int = 50
@dataclass
class Lancer(Warrior):
attack: int = 6
piercing: int = 50
class Army(list):
def add_units(self, unit_class, quantity: int):
for i in range(quantity):
unit = unit_class()
unit.army = self
self.append(unit)
def remove_unit(self, unit):
self.remove(unit)
unit.army = None
class Battle():
@staticmethod
def fight(army_1, army_2):
while army_1 and army_2:
fight(army_1[0], army_2[0])
return bool(army_1)
def fight(unit_1, unit_2):
while 1:
unit_1.hit(unit_2)
if not unit_2.is_alive:
return True
unit_2.hit(unit_1)
if not unit_1.is_alive:
return False
July 10, 2020
Comments: