Toil and Trouble help

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);
        }
    }
}

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


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

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.

1 Like

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

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.

1 Like

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).

1 Like

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

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

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

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?

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.

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

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

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.

Thanks! It works now!

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.