Library Tactician Dec '15/ Apr '21

Well, that is a bit better, but still not quite right…alignment is way out of whack and some of the comments are missing the leading #.

Also, since this topic is such a long and old one, how about starting a brand new one? Be sure to select Level Help as the category.

I don’t know how to do this

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 = self.findEnemies()
    enemyIndex = 0
# 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
   enemyIndex += 1
# 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("archer")
for i in range(len(soldiers)):
    soldier = soldiers[i]
    commandSoldier(soldier, i, len(soldiers));
# use commandArcher() to command your archers
for j in range(len(archers)):
    archer = archers[j]
    commandArcher(archer, j, len(archers));

Hi darkrebel4567,

Welcome to the forum!

Could you try again with posting your code? You need to paste all of it between the ‘’’ marks, so that it is formatted. It’s a lot easier to check, and it shows if the indentation is right.

Thanks,
Jenny

Um, I had to tidy up your code - the indentation is all over the place (possibly from the copy & paste), and you look as though you’ve borrowed some lines from other people. But basically your code works for me (admittedly really slowly, but that might be my computer).

What happens when you run your code? Do you get an error message? Which of the allies do what they’re supposed to?

Jenny

1 Like

the soldiers keep dying and the archers don’t attack the stronger enemies like was supposed to

Sounds like your indentation is wrong. Do you want to post your code again, with all the indentation inside the </>?

Can someone send me the whole code in one chunk? I can’t get the code to work with me, and I’m at my wits end. I just want to get past this level, please!

hey, can I get a copy of your code for this level? Im so wiped out from trying to solve it, I just want to get it over with. can you help me out, buddy?

im dying. please help me

Hey and welcome to the forum! We’re not allowed to give you a code, as that would be cheating, but we can help you out. Could you post your code using the </> button, so we can see what you’ve got?

Can you show us your code? please format it tooo.

1 Like

sure.

while True:
    def commandSoldier(soldier, soldierIndex, numSoldiers):
        angle = Math.PI * 2 * soldierIndex/numSoldiers
        defendPos = {"x": 41, "y": 40}
        DefendPos = {"x": 42, "y": 40}
        defendPos.x += 10 * Math.cos(angle)
        defendPos.y += 10 * Math.sin(angle)
        if soldier.health > 40:
            hero.command(soldier, "defend", defendPos);
        else:
            hero.command(soldier, "defend", DefendPos)
    def findStrongestTarget():
        mostHealth = 0
        bestTarget = None
        enemies = hero.findEnemies()
        enemyIndex = 0
        while len(enemies) > enemyIndex:
            enemy = enemies[enemyIndex]
            if enemy:
                enemyIndex += 1
                for enemy in enemies:
                    if enemy.health > mostHealth:
                        mostHealth = enemy.health
                        bestTarget = enemy
        if bestTarget and bestTarget.health > 15:
            return bestTarget
        else:
            return None
    archerTarget = None
    def commandArcher(archer):
        nearest = archer.findNearestEnemy()
        if archerTarget:
            hero.command(archer, "attack", archerTarget)
        elif nearest:
            hero.command(archer, "attack", nearest)
    while True:
        if hero.gold > hero.costOf("soldier"):
            hero.summon("soldier")
        if not archerTarget or archerTarget.health <= 0:
            archerTarget = findStrongestTarget()
        friends = hero.findFriends()
        soldiers = hero.findByType("soldier")
        archers = hero.findByType("archers")
        for a, soldier in enumerate(soldiers):
            commandSoldier(soldier, a, len(soldiers));
        for a in range(len(archers)):
            archer = archers[a]
            commandArcher(archer)

here ya go.

Define you functions outside of the while loop, i think it’s easier to call them that way

Okay. I removed the first while true loop.

And waht’s the problem? is there an error or…?

1 Like

That’s not what i meant, i meant define all funcitons before the while loops

Every time I run it, It says the code is either looping of going too slow! kay. Ill redo the loops.

The while true is after the Def. parts, but what about this section?

def findStrongestTarget():
    mostHealth = 0
    bestTarget = None
    enemies = hero.findEnemies()
    enemyIndex = 0
    while len(enemies) > enemyIndex:
        enemy = enemies[enemyIndex]
        if enemy:
            enemyIndex += 1
            for enemy in enemies:
                if enemy.health > mostHealth:
                    mostHealth = enemy.health
                    bestTarget = enemy
    if bestTarget and bestTarget.health > 15:
        return bestTarget
    else:
        return None

It has a while in the middle of it. That can stay?

I think the while loop can stay

On the archerTarget = none, should that go inside a Def. or stay outside?