Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Lancers by Mikha_Kulakov
class Warrior:
def __init__(self):
self.health = 50
self.attack = 5
self.defense = 0
self.vampirism = 0
self.next_dmg = 0
self.brother_in_arms = None
@property
def is_alive(self):
alive = True
if self.health <= 0:
alive = False
return alive
def add_bro(self, unit: 'Warrior'):
self.brother_in_arms = unit
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
class Vampire(Warrior):
def __init__(self):
super().__init__()
self.health = 40
self.attack = 4
self.vampirism = 0.5
class Lancer(Warrior):
def __init__(self):
super().__init__()
self.health = 50
self.attack = 6
self.next_dmg = 0.5
def figt_turn(unit_1, unit_2):
dmg = unit_2.attack - unit_1.defense if unit_2.attack > unit_1.defense else 0
unit_1.health -= dmg
unit_2.health += dmg*unit_2.vampirism
if unit_2.next_dmg and unit_1.brother_in_arms:
unit_1.brother_in_arms.health -= dmg*unit_2.next_dmg
def fight(unit_1, unit_2):
turn = 0
result = True
while unit_1.health > 0 and unit_2.health > 0:
turn += 1
if not turn % 2:
figt_turn(unit_1, unit_2)
else:
figt_turn(unit_2, unit_1)
if unit_2.health > 0:
result = False
return result
class Army:
def __init__(self):
self.units = []
def add_units(self, type, amount):
for _ in range(amount):
new_unit = type()
if self.units:
self.units[-1].add_bro(new_unit)
self.units.append(new_unit)
class Battle:
def __init__(self):
pass
def fight(self, my, enemy):
my_iter = iter(my.units)
enemy_iter = iter(enemy.units)
unit = next(my_iter)
en_unit = next(enemy_iter)
while True:
try:
if fight(unit, en_unit):
en_unit = next(enemy_iter)
else:
unit = next(my_iter)
except StopIteration:
if (len([i for i in my.units if i.is_alive]) >
len([i for i in enemy.units if i.is_alive])):
res = True
else:
res = False
break
return res
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!")
Aug. 13, 2019