Library Tactician - Code Help

Hello Everyone,
I feel like my code is right on the edge of being right but was hoping someone could give me a hint because I just don’t know what to do. It seems like everyone is targeting and attacking properly, yet one of my humans always dies.

Any hints are appreciated!

# 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!

# 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
        else:
            return None
    
    # Only focus archers' fire if there is a big ogre.
    if bestTarget and bestTarget.health > 15:
        archerTarget = enemy
        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 and nearest.health > 15:
        hero.command(archer, "attack", archerTarget)
    elif nearest:
        hero.command(archer, "attack", nearest)

archerTarget = None

while True:
    # If archerTarget is defeated 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("archer")
    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]
        commandArcher(archer);


Ok. So if you look at your findStrongestTarget() function, the # Only focus archers' fire if there is a big ogre., you are redefining enemy with archerTarget. But you want findStrongestTarget to return the strongest target to make the archers target the strongest enemy and not the closest one. Can you see if you can fix this? Repost your code if it still doesn’t work

1 Like

Delete this:

and this:

And you should be good!
Lydia

2 Likes

This totally worked and I have no idea why. Was it because with the else statement I was setting the target to none at times?

If this works, please don’t post solutions on these forums.:smile:
Even though you say it works, I looked at your code and you still have not fixed the findStrongestTarget() function. Maybe look at it again.