# Toil and Trouble help

**URL:** <https://discourse.codecombat.com/t/toil-and-trouble-help/5366>\
**Category:** Uncategorized\
**Created:** [October 22, 2015, 7:57pm UTC](https://discourse.codecombat.com/t/toil-and-trouble-help/5366 "2015-10-22T19:57:22Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![eab201903977](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/eab201903977/32/3149_2.png) [@eab201903977](https://discourse.codecombat.com/u/eab201903977)\
**Post date:** [October 22, 2015, 7:57pm UTC](https://discourse.codecombat.com/t/toil-and-trouble-help/5366/1 "2015-10-22T19:57:22Z")

</div>

I have this code in Toil and Trouble. However, I still fail. Does anyone have any ideas why?  
Edit 1: Posted updated code, still doesn’t work. What is wrong?

```
// 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.

this.chooseTarget = function(friend){
    if(friend.type=="archer"){
        var enemy = friend.findNearest(this.findEnemies());
        return enemy;
    }else{
        var witches = this.findByType("witch");
        enemy = friend.findNearest(witches);
        return enemy;
    }
};

loop {
    
    var friends = this.findFriends();
    for(var i=0; i < friends.length; i++) {
        var friend = friends[i];
        // Use your chooseTarget function to decide what to attack.
        var target = this.chooseTarget(friend);
        if(target){
        this.command(friend, "attack", target);
        }
    }
}
```

---

<div class="post-metadata">

**Author:** ![Paul\_Lum](https://avatars.discourse-cdn.com/v4/letter/p/ea5d25/32.png) [@Paul\_Lum](https://discourse.codecombat.com/u/Paul_Lum)\
**Post date:** [October 22, 2015, 8:13pm UTC](https://discourse.codecombat.com/t/toil-and-trouble-help/5366/2 "2015-10-22T20:13:16Z")

</div>

maybe try and change the **else** in your chooseTarget function. instead of **else** , try

```python

     }else if (friend.type=="soldier"){
         var enemy = friend.findNearest(this.findByType("witch"));
              if (enemy){
                   return enemy;

```

---

<div class="post-metadata">

**Author:** ![ChronistGilver](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chronistgilver/32/3778_2.png) [@ChronistGilver](https://discourse.codecombat.com/u/ChronistGilver)\
**Post date:** [October 22, 2015, 9:09pm UTC](https://discourse.codecombat.com/t/toil-and-trouble-help/5366/3 "2015-10-22T21:09:52Z")

</div>

Your problem is that all your soldiers got to attack one witch. This shouldn’t happen. Instead, They should attack the nearest witch, while your archers attack the nearest enemy.

---

<div class="post-metadata">

**Author:** ![eab201903977](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/eab201903977/32/3149_2.png) [@eab201903977](https://discourse.codecombat.com/u/eab201903977)\
**Post date:** [October 22, 2015, 9:33pm UTC](https://discourse.codecombat.com/t/toil-and-trouble-help/5366/4 "2015-10-22T21:33:13Z")

</div>

How would I implement this?  
Would this idea work?  
Implement a variable of nearestenemy  
and then check if that is a witch.

---

<div class="post-metadata">

**Author:** ![ChronistGilver](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chronistgilver/32/3778_2.png) [@ChronistGilver](https://discourse.codecombat.com/u/ChronistGilver)\
**Post date:** [October 22, 2015, 10:25pm UTC](https://discourse.codecombat.com/t/toil-and-trouble-help/5366/5 "2015-10-22T22:25:24Z")

</div>

No, that would be your soldiers wouldn’t move until the nearest enemy is a witch. I was thinking more along the lines of `this.findByType("witch")`. This method returns all units of the type inputted. Do you have that method yet? If not, I strongly suggest you get at least the Hardened Steel Glasses.

---

<div class="post-metadata">

**Author:** ![Msakr](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@Msakr](https://discourse.codecombat.com/u/Msakr)\
**Post date:** [October 23, 2015, 7:59pm UTC](https://discourse.codecombat.com/t/toil-and-trouble-help/5366/6 "2015-10-23T19:59:10Z")

</div>

Implementing it should be easy (but apologies, I’ve been on Python, not javascript, so some of the following may be a bit tricky):

1. Create a distance variable in your chooseTarget function - like dToWitch. Set the initial distance to 9999 or so. [dToWitch = 99999] before you go looping through the enemies.

2. As you loop through all enemies (like you’re doing already, actually), test 2 things prior to updating enemy (i.e. add an additional condition to your existing if statement): (a) if enemies[i] is a witch [you do this already] and (b) if friend.distanceTo(enemies[i]) \< dToWitch. When you update enemy, also update dToWitch. dToWitch = friend.distanceTo(enemies[i])

3. Next, only “return enemy” after you’ve looped through all enemies. (i.e. return enemy should no longer be in the for statement).

---

<div class="post-metadata">

**Author:** ![eab201903977](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/eab201903977/32/3149_2.png) [@eab201903977](https://discourse.codecombat.com/u/eab201903977)\
**Post date:** [October 26, 2015, 8:09pm UTC](https://discourse.codecombat.com/t/toil-and-trouble-help/5366/7 "2015-10-26T20:09:36Z")

</div>

I posted my edited code, it still isn’t working.  
Thanks for any tips you can give!

---

<div class="post-metadata">

**Author:** ![ChronistGilver](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chronistgilver/32/3778_2.png) [@ChronistGilver](https://discourse.codecombat.com/u/ChronistGilver)\
**Post date:** [October 26, 2015, 10:56pm UTC](https://discourse.codecombat.com/t/toil-and-trouble-help/5366/8 "2015-10-26T22:56:53Z")

</div>

Your code is sound. The only thing now is to keep submitting.

---

<div class="post-metadata">

**Author:** ![eab201903977](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/eab201903977/32/3149_2.png) [@eab201903977](https://discourse.codecombat.com/u/eab201903977)\
**Post date:** [October 26, 2015, 10:59pm UTC](https://discourse.codecombat.com/t/toil-and-trouble-help/5366/9 "2015-10-26T22:59:58Z")

</div>

I’ve submitted more than 10 times. What do you think?

---

<div class="post-metadata">

**Author:** ![ChronistGilver](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chronistgilver/32/3778_2.png) [@ChronistGilver](https://discourse.codecombat.com/u/ChronistGilver)\
**Post date:** [October 26, 2015, 11:02pm UTC](https://discourse.codecombat.com/t/toil-and-trouble-help/5366/10 "2015-10-26T23:02:19Z")

</div>

Hm, that’s very strange. I’m using your code right now, and I’m winning again and again. What happens when you run your code?

---

<div class="post-metadata">

**Author:** ![eab201903977](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/eab201903977/32/3149_2.png) [@eab201903977](https://discourse.codecombat.com/u/eab201903977)\
**Post date:** [October 26, 2015, 11:03pm UTC](https://discourse.codecombat.com/t/toil-and-trouble-help/5366/11 "2015-10-26T23:03:19Z")

</div>

It runs fine, my soldiers walk towards the witches, and the bottom one always dies, but the top one doesn’t, and then my soldiers and archers all die from various causes.

---

<div class="post-metadata">

**Author:** ![eab201903977](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/eab201903977/32/3149_2.png) [@eab201903977](https://discourse.codecombat.com/u/eab201903977)\
**Post date:** [October 26, 2015, 11:05pm UTC](https://discourse.codecombat.com/t/toil-and-trouble-help/5366/12 "2015-10-26T23:05:02Z")

</div>

Do the witches in yours use a green ball thing spell? That seems to cause a lot of damage.

---

<div class="post-metadata">

**Author:** ![ChronistGilver](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chronistgilver/32/3778_2.png) [@ChronistGilver](https://discourse.codecombat.com/u/ChronistGilver)\
**Post date:** [October 26, 2015, 11:08pm UTC](https://discourse.codecombat.com/t/toil-and-trouble-help/5366/13 "2015-10-26T23:08:50Z")

</div>

Well, that is the witches’ normal attack. I don’t understand why your level would be like this. Let’s call @nick.

---

<div class="post-metadata">

**Author:** ![nick](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/nick/32/784_2.png) [@nick](https://discourse.codecombat.com/u/nick)\
**Post date:** [October 27, 2015, 7:34pm UTC](https://discourse.codecombat.com/t/toil-and-trouble-help/5366/14 "2015-10-27T19:34:45Z")

</div>

For your archer targets, change `var enemy = friend.findNearest(this.findEnemies());` to `var enemy = friend.findNearest(friend.findEnemies());`. I’ll also make it so that the hero can always see the bottom ogres.

---

<div class="post-metadata">

**Author:** ![eab201903977](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/eab201903977/32/3149_2.png) [@eab201903977](https://discourse.codecombat.com/u/eab201903977)\
**Post date:** [October 27, 2015, 7:39pm UTC](https://discourse.codecombat.com/t/toil-and-trouble-help/5366/15 "2015-10-27T19:39:07Z")

</div>

Thanks! It works now!

---

<div class="post-metadata">

**Author:** ![csd](https://avatars.discourse-cdn.com/v4/letter/c/46a35a/32.png) [@csd](https://discourse.codecombat.com/u/csd)\
**Post date:** [November 7, 2015, 6:55am UTC](https://discourse.codecombat.com/t/toil-and-trouble-help/5366/16 "2015-11-07T06:55:28Z")

</div>

I never really spent very much time on this level because I was using a wizard hero that does cast summon fangrider which made me heat it.
