Getting wrong answer
I would like to give some feedback about ...
From: https://py.checkio.org/mission/army-battles/solve/
HTTP_USER_AGENT:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36
Below is my code. For some reason it says i am getting the wrong answer for the test:
army_1 = Army()
army_2 = Army()
army1.addunits(Warrior, 20)
army2.addunits(Warrior, 21)
battle = Battle()
battle.fight(army1, army2)
I get false, but it says it should be true. I don't see how this should be the case.
class Warrior:
def __init__(self):
self.health = 50
self.attack = 5
self.is_alive = True
class Knight(Warrior):
def __init__(self):
super().__init__()
self.attack = 7
class Army:
def __init__(self):
self.soldiers = []
def add_units(self,soldier, amt):
for _ in range(amt):
self.soldiers.append(soldier())
class Battle:
def __init__(self):
pass
def fight(self,a1,a2):
while len(a1.soldiers) > 0 and len(a2.soldiers) > 0:
soldier_alive = fight(a1.soldiers[0],a2.soldiers[0])
if soldier_alive:
a2.soldiers.pop(0)
else:
a1.soldiers.pop(0)
if len(a1.soldiers) > 0:
return True
else:
return False
def fight(unit_1, unit_2):
while unit_1.is_alive and unit_2.is_alive:
unit_2.health -= unit_1.attack
if unit_2.is_alive:
unit_1.health -= unit_2.attack
if unit_2.health <= 0:
unit_2.is_alive = False
return True
if unit_1.health <= 0:
unit_1.is_alive = False
return False
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!")