[SOLVED] Toil and trouble Impossible!

Here is my code:

def chooseTarget(friend):
    enemies = hero.findEnemies()
    if len(enemies) is not 0:
        enemy = hero.findNearest(enemies)
        if friend and friend.type is "soldier" and enemy and enemy.type is "witch":
                hero.command(friend, "attack", "witch")
        elif enemy:
            hero.command(friend, "attack", enemy)
        if friend and friend.type is "archer" and enemy:
            hero.command(friend, "attack", enemy)

while True:
    friends = hero.findFriends()
    for friend in friends:
        # Используй функцию `chooseTarget` для принятия решения об атаке.
        chooseTarget(friend)

I can’t do anything

This line has lots of things that need to be true. If any of them aren’t true (and at the start of the level the nearest enemy is not going to be a witch), then the code will just move on to the elif statement - so the friend will attack the nearest enemy.

Instead you need to write a for loop to go through all the enemies and find the witches, and set the soldiers to attack these first.

Do somthing like this and read comments:

def soldierAttackWitches():
    soldiers = hero.findByType("soldier")
    witches = hero.findByType("witch")
    for i in range(len(soldier)):
        soldier = soldier[i]
        witch = witches[i]
        soldierEnemy = soldier.findNearestEnemy)
        if witch and soldier:
            hero.command(# Whatever you commmand)
        else:
            hero.command(soldier, "attack", # Your target)

'''
The function with the soldier uses soldier instead of friend, so you're only
telling soldiers to attack witches. If you use friends, you're telling every 
friend to attack the witches or else the nearest enemy, resulting the soldiers
killing the witches and ogre-fs killing everyone or otherwise the opposite
'''

def archerAttackEveryone():
    archers = # ???
    archer = hero.findNearest(# archers)

Define the rest of the archer function, then you’ll know the difference.

Thanks for trying to help me .I solved it!

3 Likes

Then congratulations for completing the level! :partying_face:

Andrei

1 Like

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.