I need help on Library Tactician

# Hushbaum has been ambushed by ogres!
# She is busy healing her soldiers, you should command them to fight!
# The ogres will send more troops if they think they can get to Hushbaum or your archers, so keep them inside the circle!
console.log()
# Soldiers spread out in a circle and defend.
def commandSoldier(soldier, soldierIndex, numSoldiers):
    angle = Math.PI * 2 * soldierIndex / numSoldiers
    defendPos = {"x": 41, "y": 40}
    defendPos.x += 10 * Math.cos(angle)
    defendPos.y += 10 * Math.sin(angle)
    hero.command(soldier, "defend", defendPos);

# Find the strongest target (most health)
# This function returns something! When you call the function, you will get some value back.
def findStrongestTarget():
    mostHealth = 0
    bestTarget = None
    enemies = hero.findEnemies()
    # Figure out which enemy has the most health, and set bestTarget to be that enemy.
    for enemy in enemies:
        if enemy.health > mostHealth:
            mostHealth = enemy.health
            bestTarget = enemy
    # Only focus archers' fire if there is a big ogre.
    if bestTarget and bestTarget.health > 15:
        return bestTarget
    else:
        return None


# If the strongestTarget has more than 15 health, attack that target. Otherwise, attack the nearest target.
def commandArcher(archer):
    nearest = archer.findNearestEnemy()
    if archerTarget:
        hero.command(archer, "attack", archerTarget)
    elif nearest:
        hero.command(archer, "attack", nearest)

archerTarget = None

while True:
    # If archerTarget is dead or doesn't exist, find a new one.
    if not archerTarget or archerTarget.health <= 0:
        # Set archerTarget to be the target that is returned by findStrongestTarget()
        archerTarget = findStrongestTarget()
    
    friends = hero.findFriends()
    soldiers = hero.findByType("soldier")
    # Create a variable containing your archers.
    archers = hero.findByType("archers")
    for i in range(len(soldiers)):
        soldier = soldiers[i]
        commandSoldier(soldier, i, len(soldiers));
    # use commandArcher() to command your archers
    for i in range(len(archers)):
        archer = archers[i]
        hero.commandArcher(archer, i, len(archers));

Here is my code let me know what I’m doing wrong, I’ve been working on this level for a week. I cant figure out what is wrong with my code, Please help.

hey there @Lukyme , welcome to the forum please read these rules and enjoy your stay

I’ve read the rules, Do you know if you can help me @cheddarcheese? The line in my code that is messing me up is 55.

is it this line?
200

Yes, I cant seem why it is not going through. It stops there and does not go with the rest of my code.

Try changing it to

for i in enumerate(archers):

Ok, I will try that.

It did not work, I tried it and it didn’t work. It is also because of a soldier dying. I’m just so confused. @qwerty

Can you send a screenshot of it?
p.s. sorry 4 late reply

I think the for i in range (len(archers) is fine. Maybe like check if there is an archer? (Also can you post a link cause im lazy to go find it and kinda lost in the levels)

you can also do for archer in archers: and then use the var archer

Ok sure I will post a link to it.

Ok thanks for the suggestion.

CodeCombat - Coding games to learn Python and JavaScript?

you can also do instead of defense points do hushBaum = hero.findNearest(hero.findByType("librarian")) hero.command(friend, "defend", hushBaum)

1 Like

Im pretty sure you need to define hushBaum before. maybe something like
wizard = hero.findNearest(hero.findByType("librarian"))?
But defending the points work best.

1.Also, in your code, you have ; which is in JS.
2.At line 61, commandArcher(archer) is just fine.
3. You can del the line 59
If you have any other problemes in your code pls tell us :slight_smile:

3 Likes

It does not work. Do you have any other suggestions that might help?

Can u send a screen shot/current code? Make sure to use this


image
button when you post your whole code :smile:.