• Problem in the last assert

Question related to mission The Healers

 

My code:

class Warrior:
    def __init__(self):
        self.health = 50
        self.max_hp = 50
        self.attack = 5
        self.defens = 0
        self.vampirism = 0
        self.is_alive = True


class Knight(Warrior):
    def __init__(self):
        super().__init__()
        self.attack = 7


class Defender(Warrior):
    def __init__(self):
        super().__init__()
        self.health = 60
        self.max_hp = 60
        self.attack = 3
        self.defens = 2


class Vampire(Warrior):
    def __init__(self):
        super().__init__()
        self.health = 40
        self.max_hp = 40
        self.attack = 4
        self.vampirism = 50


class Lancer(Warrior):
    def __init__(self):
        super().__init__()
        self.attack = 6
        self.attack2 = 3


class Healer(Warrior):
    def __init__(self):
        super().__init__()
        self.attack = 0
        self.health = 60
        self.max_hp = 60
        self.up_hp = 2

    def heal(self, prev_unit):
        prev_unit.health += self.up_hp
        if prev_unit.health > prev_unit.max_hp:
            prev_unit.health = prev_unit.max_hp
        return prev_unit.health


class Army:
    def __init__(self):
        self.units = []

    def add_units(self, type_unit, solders):
        for i in range(solders):
            self.units.append(type_unit())
        return self.units


class Battle:
    def fight(self, army1, army2):
        x = 0
        y = 0

        while x < (len(army1.units)) and y < len(army2.units):
            if fight(army1.units[x], army2.units[y]):
                if type(army1.units[x]) is Lancer and y != (len(army2.units) - 1):
                    if army1.units[x].attack2 > army2.units[y + 1].defens:
                        army2.units[y + 1].health -= army1.units[x].attack2 - army2.units[y + 1].defens
                elif x != (len(army1.units) - 1) and type(army1.units[x+1]) is Healer:
                    army1.units[x].health += army1.units[x+1].heal(army1.units[x])
                y += 1
            else:
                x += 1
        return x < (len(army1.units))


def fight(unit_1, unit_2):
    while unit_1.is_alive and unit_2.is_alive:
        if unit_1.attack > unit_2.defens:
            unit_2.health -= unit_1.attack - unit_2.defens
            unit_1.health += int((unit_1.attack - unit_2.defens) * (unit_1.vampirism / 100))
            if unit_1.health > unit_1.max_hp:
                unit_1.health = unit_1.max_hp
        if unit_2.health <= 0:
            unit_2.is_alive = False
            break

        if unit_2.attack > unit_1.defens:
            unit_1.health -= unit_2.attack - unit_1.defens
            unit_2.health += int((unit_2.attack - unit_1.defens) * (unit_2.vampirism / 100))
            if unit_2.health > unit_2.max_hp:
                unit_2.health = unit_2.max_hp
        if unit_1.health <= 0:
            unit_1.is_alive = False

    return unit_1.is_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()
    priest = Healer()

    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
    assert freelancer.health == 14
    priest.heal(freelancer)
    assert freelancer.health == 16

    # battle tests
    my_army = Army()
    my_army.add_units(Defender, 2)
    my_army.add_units(Healer, 1)
    my_army.add_units(Vampire, 2)
    my_army.add_units(Lancer, 2)
    my_army.add_units(Healer, 1)
    my_army.add_units(Warrior, 1)

    enemy_army = Army()
    enemy_army.add_units(Warrior, 2)
    enemy_army.add_units(Lancer, 4)
    enemy_army.add_units(Healer, 1)
    enemy_army.add_units(Defender, 2)
    enemy_army.add_units(Vampire, 3)
    enemy_army.add_units(Healer, 1)

    army_3 = Army()
    army_3.add_units(Warrior, 1)
    army_3.add_units(Lancer, 1)
    army_3.add_units(Healer, 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(Healer, 1)
    army_4.add_units(Lancer, 2)

    battle = Battle()

    assert battle.fight(my_army, enemy_army) == False
    assert battle.fight(army_3, army_4) == True
    print("Coding complete? Let's try tests!")

All tests are correct except the last. I can’t understand where the problem is. Help, pls.

17
Ves