Play Home Contribute GitHub Join Us Discourse Staff Members GitHub Contribution Guides Team

Stuck on Library Tactician Python


#1

Hi, I’m stuck with the below…a couple of issues that I’ve noticed:

Firstly, it keeps telling me there’s a problem with line 33 TypeError.
nearest = archer.findNearestEnemy()

Second, I don’t think my archers are focussing on the ogres so it doesn’t look like my findStrongestTarget() function isn’t working.

# 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()
    enemy = hero.findNearestEnemy()
    # Figure out which enemy has the most health, and set bestTarget to be that enemy.
    while enemy < len(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, archerIndex, numArchers):
    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()
    
    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(soldiers)):
        archer = archers[i]
        commandArcher(archer, i, len(soldiers));

Thanks in advance


#2

I’ve updated the findStrongestTarget function to the below but the archers aren’t focussing on the enemy with the most health.

Also, I thought about building an if function to move the soldiers back if their health to the centre if their health was below 1/4 of their max health but it didn’t do anything when I put it at the end of the commandSoldier function. Any ideas?

def findStrongestTarget():
    mostHealth = 0
    bestTarget = None
    enemies = hero.findEnemies()
    enemyIndex = 0
    # Figure out which enemy has the most health, and set bestTarget to be that enemy.
    while len(enemies) > enemyIndex:
        enemy = enemies[enemyIndex]
        if enemy:
            enemyIndex += 1
            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

#3

That is supposed to be else if. You put elif


#4

python ==> elif; java script==> else if
@cp0649 see comments in the end of your while True loop:

    for i in range(len(soldiers)): # you iterate through soldiers, 
                                   # I think you intend to command archers 
        archer = archers[i]
        commandArcher(archer, i, len(soldiers)); # same as above
                                                 # soldiers instead of archers