Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Lancers by PythonLearner
class Warrior(object):
def __init__(self):
self.health = 50
self.attack = 5
self.defense = 0
self.has_addition_attack = False
@property
def is_alive(self):
return self.health > 0
def take_attack(self, unit):
unit.get_wounded(self.attack)
def get_wounded(self, attack):
self.health -= attack
class Knight(Warrior):
def __init__(self):
super().__init__()
self.attack = 7
class Defender(Warrior):
def __init__(self):
super().__init__()
self.health = 60
self.attack = 3
self.defense = 2
def get_wounded(self, attack):
if attack > self.defense:
self.health -= attack-self.defense
class Vampire(Warrior):
def __init__(self):
super().__init__()
self.max_health = 40
self.health = self.max_health
self.attack = 4
self.vampirism = 0.5
def take_attack(self, unit):
base_health = unit.health
unit.get_wounded(self.attack)
demage = base_health-unit.health
self.health = min(self.health+demage*self.vampirism, self.max_health)
class Lancer(Warrior):
def __init__(self):
super().__init__()
self.health = 50
self.attack = 6
self.has_addition_attack = True
self.addition_attack_coefficient = 0.5
self.addition_attack = 0
def take_attack(self, unit):
base_health = unit.health
unit.get_wounded(self.attack)
demage = base_health-unit.health
self.addition_attack = demage*self.addition_attack_coefficient
class Army(object):
def __init__(self):
self.army = []
self.casualties = 0
@property
def army_size(self):
return len(self.army)-self.casualties
@property
def is_anyone_alive(self):
return self.army_size > 0
def add_units(self, unit_class, number):
self.army.extend(unit_class() for _ in range(number))
def get_unit(self, index):
return self.army[index] if index < len(self.army) else None
def fight(unit1, unit2, neighbor1=None, neighbor2=None):
attacker, attacked = unit1, unit2
attacker_neighbor, attacked_neighbor = neighbor1, neighbor2
while unit1.is_alive and unit2.is_alive:
attacker.take_attack(attacked)
if attacker.has_addition_attack and attacked_neighbor:
attacked_neighbor.get_wounded(attacker.addition_attack)
attacker, attacked = attacked, attacker
attacker_neighbor, attacked_neighbor = attacked_neighbor, attacker_neighbor
return unit1.is_alive
class Battle(object):
def duel(self, unit1, unit2, neighbor1, neighbor2):
return fight(unit1, unit2, neighbor1, neighbor2)
def fight(self, army1, army2):
unit1_index = 0
unit2_index = 0
while army1.is_anyone_alive and army2.is_anyone_alive:
unit1 = army1.get_unit(unit1_index)
unit2 = army2.get_unit(unit2_index)
neighbor1 = army1.get_unit(unit1_index+1)
neighbor2 = army2.get_unit(unit2_index+1)
if self.duel(unit1, unit2, neighbor1, neighbor2):
army2.casualties += 1
if army2.is_anyone_alive:
unit2_index += 1
else:
army1.casualties += 1
if army1.is_anyone_alive:
unit1_index += 1
return army1.is_anyone_alive
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()
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
#battle tests
my_army = Army()
my_army.add_units(Defender, 2)
my_army.add_units(Vampire, 2)
my_army.add_units(Lancer, 4)
my_army.add_units(Warrior, 1)
enemy_army = Army()
enemy_army.add_units(Warrior, 2)
enemy_army.add_units(Lancer, 2)
enemy_army.add_units(Defender, 2)
enemy_army.add_units(Vampire, 3)
army_3 = Army()
army_3.add_units(Warrior, 1)
army_3.add_units(Lancer, 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(Lancer, 2)
battle = Battle()
assert battle.fight(my_army, enemy_army) == True
assert battle.fight(army_3, army_4) == False
print("Coding complete? Let's try tests!")
Sept. 7, 2018
Comments: