Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Clean and documented. solution in Clear category for The Healers by Celshade
class Warrior(object):
"""Establish the base Warrior class.
Intended to be subclassed by specialized Warriors with enhanced
attributes and capabilities.
Attributes:
health: The Warrior's health (default=50)
attack: The Warrior's attack (default=5)
"""
def __init__(self, health: int=50, attack: int=5) -> None:
self.health = health
self.attack = attack
self.MAX_HP = health
@property
def is_alive(self) -> bool:
"""Return True if the Warrior is alive, else False."""
return self.health > 0
class Knight(Warrior):
"""Establish the Knight class.
The Knight will extend the attributes and properties of the Warrior,
having an increased attack (default=7).
"""
def __init__(self, health: int=50, attack: int=7) -> None:
super().__init__(health, attack)
class Defender(Warrior):
"""Establish the Defender class.
The Defender will extend the attributes and properties of the Warrior,
having increased health (default=60) and the unique 'defense' attribute -
but a lowered attack (default=3).
Attributes:
defense: Mitigates damage recieved (default=2).
"""
def __init__(self, health: int=60, attack: int=3, defense: int=2) -> None:
super().__init__(health, attack)
self.defense = defense
class Vampire(Warrior):
"""Establish the Vampire class.
The Vampire will extend the attributes and properties of the Warrior,
having the unique 'vampirism' attribute - but lowered health
(default=40) and a lowered attack (default=4).
Attributes:
vampirism: Percentage of lifesteal from damage dealt (default=.50).
"""
def __init__(self, health: int=40, attack: int=4,
vampirism: float=.50) -> None:
super().__init__(health, attack)
self.vampirism = vampirism
class Lancer(Warrior):
"""Establish the Lancer class.
The Lancer will extend the attributes and properties of the Warrior,
having increased attack (default=6) and the unique 'splash' attribute.
Attributes:
splash: Percentage of splash damage dealt to the enemy behind the
target, based off damage done (default=.50).
"""
def __init__(self, health: int=50, attack: int=6,
splash: float=.50) -> None:
super().__init__(health, attack)
self.splash = splash
class Healer(Warrior):
"""Establish the Healer classs.
The Healer will extend the attributes and properties of the Warrior,
having increased health (default=60) - but no attack power (default=0).
The Healer provides the unique 'heal()' method, which heals an ally
each time they attack an enemy.
Public Methods:
heal(): Heal the target ally every time they attack an enemy.
"""
def __init__(self, health: int=60, attack: int=0):
super().__init__(health, attack)
def heal(self, target: Warrior, heal: int=2) -> None:
"""Heal an ally unit.
Args:
target: The target ally unit.
heal: The amount of health returned to the target (default=2).
"""
target.health += heal
if target.health > target.MAX_HP:
target.health = target.MAX_HP
class Army(object):
"""Establish an Army of Warriors.
Attributes:
barracks (list): A list containing the Army units.
Public Methods:
add_units(): Add units to the Army.
"""
def __init__(self) -> None:
self.barracks = []
def add_units(self, unit_type: Warrior, quantity: int) -> None:
"""Add Warriors to the Army.
Args:
unit_type: The type of Warrior.
quantity: The quantity of units to add.
"""
[self.barracks.append(unit_type()) for i in range(quantity)]
class Battle(object):
"""Handle the battling of two armies.
Public Methods:
fight(): Fight two Armies.
"""
# Army vs Army
def fight(self, army_1: Army, army_2: Army) -> bool:
"""Fight Warrior's from each army until none remain.
Args:
army_1: The first Army.
army_2: The second Army.
Returns:
Return True if army_1 survives, else False.
"""
first = army_1.barracks
second = army_2.barracks
while len(first) > 0:
if fight(first[0], second[0], army_1, army_2) is True:
del second[0]
else:
del first[0]
if len(second) is 0:
return True
else:
return False
# 1v1
def fight(unit_1: Warrior, unit_2: Warrior,
army_1: Army=None, army_2: Army=None) -> bool:
"""Fight two Warriors in a turn-based duel.
Args:
unit_1: The first Warrior.
unit_2: The second Warrior.
army_1: The first Warrior's Army (default=None).
army_2: The second Warrior's Army (default=None).
Returns:
victory: True if unit_1 survives, else False.
"""
def tactics(attacker: Warrior, atk_army: Army,
on_guard: Warrior, def_army: Army) -> None:
"""Condense fight() code and insert units where necessary.
Args:
attacker: The attacking unit.
atk_army: The Army of the attacking unit.
on_guard: The unit being attacked.
def_army: The Army of the unit being attacked.
"""
damage = 0
# Defender mitigates damage
if type(on_guard) is Defender:
if attacker.attack > on_guard.defense:
damage = (attacker.attack - on_guard.defense)
else:
damage = attacker.attack
on_guard.health -= damage
# Vampire lifesteals health
if type(attacker) is Vampire:
attacker.health += (damage * attacker.vampirism)
# Lancer causes 'splash' damage if a secondary enemy exists.
elif type(attacker) is Lancer:
if def_army is not None and len(def_army.barracks) > 1:
def_army.barracks[1].health -= (damage * attacker.splash)
# Attacker is healed if a Healer is standing behind him.
if atk_army is not None and len(atk_army.barracks) > 1:
if type(atk_army.barracks[1]) is Healer:
atk_army.barracks[1].heal(attacker)
victory = None
while victory is None:
if unit_1.is_alive:
tactics(unit_1, army_1, unit_2, army_2)
else:
victory = False
if unit_2.is_alive:
tactics(unit_2, army_2, unit_1, army_1)
else:
victory = True
return victory
Sept. 21, 2018
Comments: