TOIL AND TROUBLE in the mountain

> # Soldiers should attack the witches, archers should attack nearest enemy.
> def chooseTarget(friend):
>     if friend.type=="archer":
>         return "nearest"
>     else:
>         return "witch"

> while True:
>     friends = hero.findFriends()
>     for friend in friends:
>         # Use your chooseTarget function to decide what to attack.
>         E=chooseTarget(friend)
>         enemies=hero.findByType("witch",hero.findEnemies())
>         a=False
>         if enemies:
>             hero.command(friend,"attack",enemies[0])
>         else:
>             enemies=friend.findNearest(friend.findEnemies())
>             if enemies:
>                 hero.command(friend,"attack",enemies)

This is my code in the exercice Toil and Trouble. I do what I must do and I do what was script in the Hints.
Than, why can I not finish this exercice?
Thank you.
X95
  1. Have you determined if you glasses can see far enough?
  2. Try counting the number of enemies returned with hero.findEnemies()
  3. If the witches are causing the most trouble, how can you reach them to defeat them as a priority?
  • In other words what would you have to add to the code to ensure you are attacking the witches first?

Thank you, I finish the exercice.

At one point while codinng, I made a mistake (guess I was tired) but it succeeded, and after fixing it, it no longer did work.

Here’s the wrong code (even with error) that made me win, while code that made the archers correctly target their closest enemy and the soldiers target the closest witch failed terrible (due to area of attack damage, I’m guessing). The final version after reading these posts (i.e. using their vision, not the hero’s) and splitting the soldiers worked like a charm too I just thought it was interesting that the below code worked at all, given that “enemies” is never defined. :smiley:

[code removed as it does give a solution]

Any thoughts on why this is still working? is it because both use target and thus the archer’s definition of the enemy is used for the command?


I resisted reviving this and this - and still got the “really revive?” message on this one… Is it preferable to open a new one, or given that it’s all about the same level, write in an old thread?

@julix I pulled the code as it does give a solution that someone else could just cut and paste. But this is rather like the chicken and the egg, where it is hard to ask such a question without posting the code.

To answer you question, it is because of what is called scope. When you declare the value var target = hero.findNearestEnemy(); it creates a variable to be used for all of the code from that point forward.

If a soldier happens to be the first one in the friends[] array then the target variable would be created prior to the first check for an archer and all units would then have the ability to target something.

I added this code into your code to see if the first friend was indeed a soldier and that is what came up on my end.

    if (i===0) {
        hero.say( friends[i].type );
    }