Why does my code not force all the Archers to defend?


#1

This is my current code from summit’s gate. It sometimes sends 1 archer, sometimes 2, sometimes 3, to defend at the specified point. The part where the error occurs is on line 72 (if part == 4)

#Stage 1 - Catapults
##head directly for the catapults (they will take out their own team)
##catapults can destroy themselves (sacrifice a soldier to each)
#Stage 2 - beam towers
##tank in the middle (shield with hero and heal him)
##archers attack towers (make sure the beams are focused on the hero)
#Stage 3 - warlocks
##hack and slash (but not the door)
#Stage 4 - chieftain
##focus fire on the chieftain immediately
def heal():
    friends = self.findFriends()
    for friend in friends:
        if friend.type == "paladin":
            if friend.canCast("heal"):
                self.command(friend, "cast", "heal", self)
def jump(arg):
    if arg == "forward":
        self.jumpTo({"x": self.pos.x+20, "y": self.pos.y})
    if arg == "backward":
        self.jumpTo({"x": self.pos.x-20, "y": self.pos.y})
def commandHide():
    for guy in self.findFriends():
        top = guy.pos.y > self.pos.y
        if top:
            self.command(guy, "move", {"x": 1, "y": 45})
        else:
            self.command(guy, "move", {"x": 1, "y": 20})
def moveTroops():
        for guy in self.findFriends():
            self.command(guy, "move", self.pos)
def boop():
    friends = self.findFriends()
    for friend in friends:
        enemy = friend.findNearest(self.findEnemies())
        if friend.type == "archer":
            self.command(friend, "defend", {"x": 115, "y": 35})
        elif friend.type == "paladin":
            self.command(friend, "cast","heal", self)
        elif friend.type == "soldier":
            self.command(friend, "move", {"x": 90, "y": 50})
commandHide()
part = 1
loop:
    friends = self.findFriends()
    enemies = self.findEnemies()
    missiles = self.findEnemyMissiles()
    if part == 1:
        if missiles:
            missile = self.findNearest(missiles)
        if enemies:
            enemy = self.findNearest(enemies)
        if self.health < self.maxHealth - 200:
            heal()
            self.shield()
        if enemy:
            while enemy.health > 0:
                self.attack(enemy)
        else:
            part = 2
        if missile and self.distanceTo(missile) < 10 and self.isReady("jump"):
            self.jumpTo({"x": missile.pos.x, "y": missile.pos.y-10})
    if part == 2:
        self.moveXY(90, 35)
        moveTroops()
        part = 3
    if part == 3:
        if self.health < self.maxHealth:
            heal()
        elif self.health == self.maxHealth:
            part = 4
    if part == 4:
        self.moveXY(130, 35)
        enemies = self.findEnemies()
        tower = self.findNearest(self.findByType("tower", enemies))
        self.shield()
        boop()
        if tower != True:
            part =5
    if part == 5:
        if self.health < self.maxHealth:
            heal()
        elif self.health == self.maxHealth:
            enemies = self.findEnemies()
            door = self.findNearest(self.findByType("door", enemies))
            while door.health > 0:
                self.attack(door)
            part = 6
    if part == 6:
        self.moveXY(140, 35)
        enemies = self.findEnemies()
        enemy = self.findNearest(enemies)
        for friend in self.findFriends():
            self.command(friend, "move", {"x": 130, "y": 35})
        if enemy:
            while enemy.health > 0:
                self.attack(enemy)
        else:
            part = 7
        if self.health < self.maxHealth:
            heal()
    if part == 7:
        for friend in friends:
            self.command(friend, "defend", self.pos)
        self.move({"x": 287, "y": 35})
        enemies = self.findEnemies()
        door = self.findNearest(self.findByType("door", enemies))
        if door:
            self.attack(door)
        else:
            part = 8
    


#2

Still can’t figure it out, so I am bumping this up