Toil and Trouble help


#1

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

TOIL AND TROUBLE in the mountain
#2

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;


#3

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.


#4

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


#5

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.


#6

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


#7

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


#8

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


#9

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


#10

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?


#11

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.


#12

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


#13

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


#14

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.


#15

Thanks! It works now!


#16

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.