Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Refactored and a lot of debugging solution in Clear category for The Healers by wcass77
#
# Taken from mission The Lancers
# Taken from mission The Vampires
# Taken from mission The Defenders
class Warrior:
max_health = 50
attack = 5
defense = 0
vampirism = 0. # Fraction between 0 and 1
splash_damage = 0 # Fraction between 0 and 1
healing = 0
def __init__(self, army=None):
self._health = self.max_health
self.army = army
def __repr__(self):
return str(type(self).__name__) + f' {self.health}/{self.max_health}'
@property
def is_alive(self):
return self.health > 0
@property
def health(self):
return self._health
@health.setter
def health(self, points):
self._health = points
if self._health > self.max_health:
self._health = self.max_health
def _attack_step(self, enemy):
if self.attack > enemy.defense:
damage = self.attack - enemy.defense
enemy.health -= damage
self.health += self.vampirism * damage
try:
enemy.army.second_warrior.health -= damage * self.splash_damage
except AttributeError:
pass # expected when there is no Army or this is the last soldier
try:
self.army.second_warrior.heal(self)
except AttributeError:
pass # Expected if there is no second warrior or if it's not a healer
def fight(self, enemy):
while True:
if not self.is_alive:
return False
if not enemy.is_alive:
return True
self._attack_step(enemy)
if self.is_alive and enemy.is_alive:
enemy._attack_step(self)
class Knight(Warrior):
attack = 7
class Defender(Warrior):
max_health = 60
attack = 3
defense = 2
class Vampire(Warrior):
max_health = 40
attack = 4
vampirism = 0.5
class Lancer(Warrior):
attack = 6
splash_damage = 0.5
class Healer(Warrior):
max_health = 60
attack = 0
healing = 2
def heal(self, target):
target.health += self.healing
class Army:
def __init__(self):
self._troops = []
def add_units(self, unit_type, number):
for _ in range(number):
self._troops.append(unit_type(self))
@property
def first_warrior(self):
return self._next_warrior(0)
@property
def second_warrior(self):
return self._next_warrior(1)
@property
def defeated(self):
return not bool(self._troops)
def _remove_dead(self):
self._troops = list(filter(lambda x: x.is_alive, self._troops))
def _next_warrior(self, n):
self._remove_dead() # make sure that warriors are still alive
try:
return self._troops[n]
except IndexError:
return None
class Battle:
def fight(self, army_A, army_B):
while not army_A.defeated and not army_B.defeated:
army_A.first_warrior.fight(army_B.first_warrior)
return not army_A.defeated # True is army_A wins
def fight(unit_1, unit_2):
return unit_1.fight(unit_2)
if __name__ == '__main__':
# These "asserts" using only for self-checking and not necessary for auto-testing
# fight tests
chuck = Warrior()
bruce = Warrior()
carl = Knight()
dave = Warrior()
mark = Warrior()
bob = Defender()
mike = Knight()
rog = Warrior()
lancelot = Defender()
eric = Vampire()
adam = Vampire()
richard = Defender()
ogre = Warrior()
freelancer = Lancer()
vampire = Vampire()
priest = Healer()
assert fight(chuck, bruce) == True
assert fight(dave, carl) == False
assert chuck.is_alive == True
assert bruce.is_alive == False
assert carl.is_alive == True
assert dave.is_alive == False
assert fight(carl, mark) == False
assert carl.is_alive == False
assert fight(bob, mike) == False
assert fight(lancelot, rog) == True
assert fight(eric, richard) == False
assert fight(ogre, adam) == True
assert fight(freelancer, vampire) == True
assert freelancer.is_alive == True
assert freelancer.health == 14
priest.heal(freelancer)
assert freelancer.health == 16
# battle tests
my_army = Army()
my_army.add_units(Defender, 2)
my_army.add_units(Healer, 1)
my_army.add_units(Vampire, 2)
my_army.add_units(Lancer, 2)
my_army.add_units(Healer, 1)
my_army.add_units(Warrior, 1)
enemy_army = Army()
enemy_army.add_units(Warrior, 2)
enemy_army.add_units(Lancer, 4)
enemy_army.add_units(Healer, 1)
enemy_army.add_units(Defender, 2)
enemy_army.add_units(Vampire, 3)
enemy_army.add_units(Healer, 1)
army_3 = Army()
army_3.add_units(Warrior, 1)
army_3.add_units(Lancer, 1)
army_3.add_units(Healer, 1)
army_3.add_units(Defender, 2)
army_4 = Army()
army_4.add_units(Vampire, 3)
army_4.add_units(Warrior, 1)
army_4.add_units(Healer, 1)
army_4.add_units(Lancer, 2)
battle = Battle()
assert battle.fight(my_army, enemy_army) == False
assert battle.fight(army_3, army_4) == True
print("Coding complete? Let's try tests!")
Sept. 17, 2018
Comments: