Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

[SOLVED] Help! Toil and Trouble

Code
def chooseTarget(friend):
    friends = hero.findFriends()
    for friend in friends:
        
        if friend.type == "soldier":
            return "attack-witch"
        elif friend.type == "archer":
            return "attack-nearest"

def attackWitch():
    soldiers = hero.findByType("soldier")
    for soldier in soldiers:
        witch = soldier.findNearest(hero.findByType("witch"))
        if witch:
            hero.command(soldier, "attack", witch)

def attackNearest():
    archers = hero.findByType("archer")
    for archer in archers:
        enemy = archer.findNearest(archer.findEnemies())
        hero.command(archer, "attack", enemy)

while True:
    friends = hero.findFriends()
    for friend in friends:
        # Use your chooseTarget function to decide what to attack.
        target = chooseTarget(friend)
        if target == "attack-witch":
            attackWitch()
        elif target == "attack-nearest":
            attackNearest()
        pass
Error

image
Everyone dies before all the ogres are dead. There were 2 ogres left when everyone died.

Link

https://codecombat.com/play/level/toil-and-trouble?

Lydia

Here’s something to get you started. This level only really takes 7 lines to beat it legitimately.
But I don’t want to make things too confusing, so we’ll go with the long method.

Step 1: Change the attackWitch function:
Right now your attackWitch function doesn’t take any parameters, but here’s the catch – You can take an argument “friend”. So instead of having just def attackWitch(), you can do def attackWitch(friend). This will save you lines of code.

Step 2: Edit the inside of the new attackWitch function:
First, remove the soldiers = hero.findByType("soldier") line, you won’t need it because we’ll have it included in the arguments Next, remove the for loop as it’s now obsolete to have – As I said before, you already received a unit to command. In the witch = soldier.findNearest(hero.findByType("witch")) line, replace soldier with whatever you named the argument in the function attackWitch(argument). After that, command that unit to attack the witch.

Step 3: Remove attackNearest():
Remove the attackNearest() function since it’s unneeded.

Step 4: Remove chooseTarget()
As above, it’s obsolete to have it, so feel free to delete it.

Step 5: Edit the while True loop. Part A:
Keep the findFriends() and also keep the for-loop, but instead of getting the target, check if the type of the friend is an archer. If so, then get the friend.findNearestEnemy() for the archer, and have them attack it using the hero.command() function.

Step 6: Edit the while True loop. Part B:
Next, check if the friend.type is soldier, if so, then run our attackWitch function with the argument friend in it, so we’ll have attackWitch(friend).

If you do the above steps, then you should be able to finish the level :+1:

7-line of code proof:

5 Likes

Has anyone managed to pass the level without casualties?
Two years ago, I lost a lot of time trying to do it with a free hero, but I couldn’t…

1 Like

@xython would using a subscriber hero count? :slight_smile:

1 Like

My main is a non subscriber, but I switch often to my son’s subscriber account.

1 Like

@xython there we go! No casualties! :slight_smile:

4 Likes

Oh yes, I figured out how to do it with a wizard! Thank you!


I had to retreat one of my soldiers, he got a little bit more damage

3 Likes

No problem! Thanks for the challenge. :star_struck: It’s pretty fun going over previous levels and aiming for the scoreboard or optimizing the code. Giving yourself “extensions” to aim for is definitely a good habit. (And helps you learn)

1 Like

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