Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Don't do it with loop, if you have maths solution in Speedy category for Army Battles by Alioshca
# Taken from mission The Warriors
class Warrior:
health = 50
damage = 5
is_alive = True
class Knight(Warrior):
damage = 7
def fight(unit_1, unit_2):
while True:
unit_2.health -= unit_1.damage
if unit_2.health < 1:
unit_2.is_alive = False
return True
unit_1.health -= unit_2.damage
if unit_1.health < 1:
unit_1.is_alive = False
return False
class Army:
def add_units(self, wtype, q):
self.warrior_health = wtype.health
self.army_health = wtype.health * q
self.warrior_damage = wtype.damage
self.quantity = q
class Battle:
def fight(self, a1, a2):
k = a1.warrior_damage * a2.quantity
if (a2.army_health - k) < a1.army_health:
return True
else:
return False
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
chuck = Warrior()
bruce = Warrior()
carl = Knight()
dave = Warrior()
mark = Warrior()
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
print("Coding complete? Let's try tests!")
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()
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
#battle tests
my_army = Army()
my_army.add_units(Knight, 3)
enemy_army = Army()
enemy_army.add_units(Warrior, 3)
army_3 = Army()
army_3.add_units(Warrior, 20)
army_3.add_units(Knight, 5)
army_4 = Army()
army_4.add_units(Warrior, 30)
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!")
May 19, 2020