Help on Toil and Trouble


In the level Toil and Trouble, my soldiers go their separate ways but they attack the ogres. Does anyone know why?


# Ogre Witches have some unpleasant surprises ready for you.

# Define a chooseTarget function which takes a friend argument
# Returns the a target to attack, depending on the type of friend.
# Soldiers should attack the witches, archers should attack nearest enemy.
while True:
    friends = hero.findFriends()
    for friend in friends:
        # Use your chooseTarget function to decide what to attack.
        enemy = hero.findNearest(hero.findEnemies())
        if enemy and enemy.type is "witch" and friend.type is "soldier":
            hero.command(friend, "attack", enemy)
        if enemy and friend.type is "archer":
            hero.command(friend, "attack", enemy)


You are defining enemy as all enemies and then in your command arguments, commanding both the archers and soldiers to attack enemy.

Look at the comments. Where is your chooseTarget function? Your code looks like it’s trying to lump too many conditions together at once. Break it up. Simplify.

Your current code essentially states that if soldiers and witches exist, attack all enemies. Your friends are doing exactly what you are telling them to do.