[SOLVED] Level:borrowed sword help pls

Hello, I’m doing exactly what the instructions told me to do for the level but my archers just aren’t strong enough for the job. What do I do?

loop{
var enemies = this.findEnemies();
var counter = 0;
var mostHealth = enemies[counter];
while(counter < enemies.length){
if(enemies[counter].health > mostHealth.health){
mostHealth = enemies[counter];
}
counter++;
}

var friends = this.findFriends();
var i = 0;
while(i < friends.length){
    
    this.command(friends[i],"attack",mostHealth);
i++;
}

}

3 Likes

Please format your code as described in the FAQ. This makes it more likely that others will help you as they don’t have to do the formatting by themselves.


If I see that right I win by using (almost) the same code. I just have a check that mostHealth actually exists.

Have you tried to submit again? Submitting gives you a new random seed, possibly resulting in another outcome.

2 Likes

[Please don’t post successful solutions.]

1 Like

I got it with this code (only partially shown to avoid spoiling the level), even though there was an error.

loop{
....
while(counter < enemies.length){
if(enemies[counter].health > mostHealth.health){
mostHealth = enemies[counter];
}
counter++;
}

var friends = this.findFriends();
var i = 0;
while(i < friends.length && enemies){

    this.command(friends[i],"attack",mostHealth);
i++;
}
}


1 Like
# For this level, your hero doesn't fight.
# Command your archers to focus fire on the enemy with the most health!
while True:
    friend = hero.findFriends()
    enemies = hero.findEnemies()
    highestHp =None 
    bestHealth=0
    for enemy in enemies:
        if enemy.health >= bestHealth:
            highestHp = enemy
            bestHealth = enemy.health
            
    if highestHp:
        friend = hero.findFriends()
        
        hero.command(friend, "attack", highestHp)

If someone would be so kind as to tell me how to tell the archers how to attack the highestHp enemy … i get the error that , that is an array … and i’m really really stuck

1 Like

It’s because you’re trying to command an array: friend = hero.findFriends() even though you’ve defined friend instead of friends which is a bit confusing. What you need to do is make a for loop and command each individual friend in hero.findFriends()

2 Likes

Sir I just wanted to say thank you yet again :smiley: It worked !

1 Like
while(true) {
  var enemies = hero.findEnemies();
  var friend = hero.findNearest(hero.findFriends());
  var highestHealth = null;
  var health = 0;
for(var i = 0; i < enemies.health; i ++) {
    var enemy = enemies[i];
    if(enemy.health >= health) {
        highestHealth = enemy;
        health = enemy.health;
        if(highestHealth) {
            hero.command(friend, "attack", highestHealth);
       }
    }
} 
}

Hey.
Can you pls tell me what am I doing wrong here?

1 Like

Mod edit: Please do not post final solutions.

Hi @zython and welcome to the forum! :partying_face:

Is that a working code? If it is, can you delete it because here on the forum we do not want posted solutions? But if you need help at this level, can you format your code as it is described here?

Andrei

nope, idk ???

I don’t know if that was right or wrong, I just guess.

I think you need to change if(enemy.health >= health) { to if(enemy.health > health) { because >= means equal or greater than. So, it could think that a dead enemy was the greatest.

Please do not revive dead topics unless you have a similar issue.

Andrei

you change for enemy in enemies: to for i in enemies: then, you add enemy = enemies[i]
and friend = friends[i].

# For this level, your hero doesn't fight.
# Command your archers to focus fire on the enemy with the most health!
while True:
    friend = hero.findFriends()
    enemies = hero.findEnemies()
    best = None 
    maxHealth = 0
    for i in len(enemies):
        enemy = enemies[i]
        friend = friends[i]
        if enemy.health >= bestHealth:
            best = enemy
            maxHealth = enemy.health
            
    if best:
        hero.command(friend, "attack", best)


does this work?

I tried this

# For this level, your hero doesn't fight.
# Command your archers to focus fire on the enemy with the most health!
while True:
    friend = hero.findFriends()
    enemies = hero.findEnemies()
    best = None 
    maxHealth = 0
    for i in len(enemies):
        enemy = enemies[i]
        friend = friends[i]
        if enemy.health >= bestHealth:
            best = enemy
            maxHealth = enemy.health
            
    if best:
        hero.command(friend, "attack", best)

But it says that it have a error: Need an object. What the heck does that means anyway?

What about you maxHealth variable.

You can’t command the archers like this. You’ve put the command line outside the for loop. You should create another for loop to command the archers.
Danny

1 Like

it still doesn’t work:

# For this level, your hero doesn't fight.
# Command your archers to focus fire on the enemy with the most health!
while True:
    friend = hero.findFriends()
    enemies = hero.findEnemies()
    best = None 
    maxHealth = 0
    for i in len(enemies):
        enemy = enemies[i]
        friend = friends[i]
        if enemy.health >= bestHealth:
            best = enemy
            maxHealth = enemy.health
            
    if best:
        pass

hero.command(friend, "attack", best)

No, he does not. He said to command each archer to attack individualy.

Andrei