Here is my code.
class Warrior:
def __init__(self):
self.health = 50
self.attack = 5
@property
def is_alive(self):
return self.health>=0
class Knight(Warrior):
def __init__(self):
self.health = 50
self.attack = 7
def fight(unit_1, unit_2):
while unit_1.is_alive and unit_2.is_alive:
unit_2.health = unit_2.health - unit_1.attack
if unit_2.health > 0:
unit_1.health = unit_1.health - unit_2.attack
return unit_1.is_alive
class Army:
def __init__(self):
self.units = []
def add_units(self,unit,quantity):
for i in range(quantity):
self.units.append(unit())
def del_units(self):
self.units = self.units[1:]
class Battle:
def __init__(self):
pass
def fight(self, army1, army2):
print([HP.health for HP in army1.units],[HP.health for HP in army2.units])
if fight(army1.units[0], army2.units[0]):
army2.del_units()
else:
army1.del_units()
if len(army1.units)>0 and len(army2.units)>0:
self.fight(army1, army2)
else:
print(len(army1.units)==0)
return len(army1.units)==0
While checking with
army_1.add_units(Warrior, 1) and army_2.add_units(Warrior, 2)
I got the assertion error: result:null, should be False. However, explicitly checking the return result of the battle.fight function I do see False. What is actually happening?
Created at: 2022/11/20 11:23; Updated at: 2022/11/20 16:25
The question is resolved.