[SOLVED]Really struggling with Library Tactician

What have I done wrong? I’ve filled in the lines of code the level prompted me to but I’m still having soldiers dying.

# 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.
    bestTarget = findStrongestTarget(enemies)
    # 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 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("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]
        commandArcher(archer, i, len(archers));

Hello and welcome to codecombat discourse! This is a cozy forum where you can share ideas, share fan art, get assistance for code, etc! Before you proceed, we hope that you review this topic, which shows all essentials of this board! Thanks!

Try deleting the , i, len(archers) in commandArcher(archer, i, len(archers));

Welcome to the forum @KiwiGamer! :partying_face:

Isn’t it supposed to be self.findEnemies, or self.command(soldier, "defend", defendPos);?

They still don’t survive… Even before the archers were targeting the enemies, something just always died…

I don’t actually understand the commandArchers() function since it isn’t in the documentation bar to the left of the code, so I just copied the supplied code for the soldiers.

You can use either as far as I’m aware, both reference the same object so either should work.

hero. is actually in the code that’s given at the start of the level so I assume it’s OK to use. Plus the code seems to be working as it’s supposed to, my soldiers and just dying causing me to fail.

Sorry :sweat_smile: :sweat_smile:

No worries at all, thanks for replying!!

Hi KiwiGamer,

Welcome to the forum!

I looked at your code for the level and thought it was a complicated way to write it. I then looked at my code, and discovered that mine is almost identical! I think we both followed the instructions, which are a bit hard to follow. If you’re confused at the moment then don’t worry too much - things become clearer as you do more levels!

The one thing you need to sort out is this bit in the findStrongestTarget function:

# Figure out which enemy has the most health, and set bestTarget to be that enemy.

You need to put in a for loop here to go through each enemy, and check whether they have greater health than the previous maximum. Have you done this before?

Post again if you want more explanation.

Cheers,

Jenny

2 Likes
  1. Go through the enemies by using a for-loop
  2. Check if the enemy’s health is bigger than the mostHealth
  3. Set that enemy’s health to most health
  4. Set that enemy to bestTarget

Hope this helps,
Lydia

1 Like

Hi Jenny,

thanks for the help! I actually had a loop to start with (see below) but it wasn’t working so I looked at the hints and findStrongestTarget() is what it told me to use, and it seems to be working?
image

The code I had before that wasn’t working either was

bestTarget = enemies[0]
    for enemy in enemies:
        if enemy.health > bestTarget.health:
            bestTarget = enemy

Oh right I didn’t realise the hint was talking about the function I was writting and I was calling it recursively haha! I’m suprised it worked at all then.

Anyway, see my other reply for the code I’m using now instead… I still don’t have any luck getting further in the level though :frowning:

1 Like

This here, you don’t really need it for this level.

This should be mostHealth because you are looking for the best Target to attack aka the strongest.
Then you would set mostHealth to this enemy’s health and make the bestTarget this enemy(which you’ve done!)
Lydia

Hi Lydia,

what you’re suggesting is going to do exactly the same as what my code already does, no? bestTarget.health will give us the health of the strongest target, ie the same as mostHealth?

And I needed bestTarget = enemies[0] because otherwise bestTarget is undefined the first time through the loop so an error is thrown.

Anyway, I gave the different implementation you suggested a go but am still not having any luck.

for enemy in enemies:
        if enemy.health > mostHealth:
            bestTarget = enemy
            mostHealth = enemy.health

Can you post your most recent code please?

Yes and no?
Can you post your entire findStrongestEnemy() function?
Lydia

return  #Commented out to stop infinite loop.
return  #Commented out to stop infinite loop.
# 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:
            bestTarget = enemy
            mostHealth = enemy.health
    # 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 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("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]
        commandArcher(archer, i, len(archers));

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:
            bestTarget = enemy
            mostHealth = enemy.health
    # Only focus archers' fire if there is a big ogre.
    if bestTarget and bestTarget.health > 15:
        return bestTarget
    else:
        return None

change that to

for archer in archers:
        commandArcher(archer)

Have done so, humans still die :frowning: