[SOLVED] Help on Library Tactician Python

I need help on Library Tactician.Here is my code:

# 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 hero.findEnemies():
        if enemy.health > mostHealth:
            mostHealth = enemy.health
            
    bestTarget = mostHealth
        
    
    # 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.
    archer = hero.findByType("archer")
    for i in range(len(soldiers)):
        soldier = soldiers[i]
        commandSoldier(soldier, i, len(soldiers));
    # use commandArcher() to command your archers
    commandArcher(archer)

Notice any difference between

and

?

bestTarget should be an enemy.

2 Likes

Ok I will try rewriting bestTarget = mostHealth.

2 Likes

Yes, sorry I see it now. @MidnightWolf39 had the problem I think. When you assigned the bestTarget variable:

You made bestTarget be equal to the mostHealth, not the mostHealth enemy. If you want the mostHealth enemy, you need make bestTarget be equal to enemy who has the mostHealth.

3 Likes

On library tactician how do I define nearestTarget

nearest = hero.findNearestEnemy()

Like that.
Lydia

2 Likes

Can you help me complete it because I am having trouble with the goal?

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);

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

def commandArcher(archer):
    nearest = archer.findNearestEnemy()
    if archerTarget:
        hero.command(archer, 'attack', archerTarget)
    elif nearest:
        hero.command(archer, 'attack', nearest)

archerTarget = None

def thor(target):
    if target:
        if hero.canCast("chain-lightning", target):
            hero.cast("chain-lightning", target)


while True:
    badGuy = findStrongestTarget()
    if hero.gold > hero.costOf('soldier'):
        hero.summon('soldier')
    if not archerTarget or archerTarget.health <= 0:
        archerTarget = badGuy
    thanos = badGuy
    thor(thanos)
    friends = hero.findFriends()
    soldiers = hero.findByType('soldier')
    archers = hero.findByType('archers')
    for i, soldier in enumerate(soldiers):
        commandSoldier(soldier, i, len(soldiers));
    for i in range(len(archers)):
        archer = archers[i]
        commandArcher(archer)

Do you have an error?
Lydia

Are you more comfortable with index? Because I don’t think you’ll need an index for this level.

I don’t know much, but isn’t this supposed to be

for i in range(len(soldiers)):

@AnSeDra
@PeterPalov
some help?
Lydia

Now it’s doing this

This is probably still a problem with the for loop. Please post your new code, it’s always helpful to do this after changing your code.
Thanks
Danny

2 Likes

she is not on right now but when there is the chance I will ask her to post her new code

1 Like
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);

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

def commandArcher(archer):
    nearest = archer.findNearestEnemy()
    if archerTarget:
        hero.command(archer, 'attack', archerTarget)
    elif nearest:
        hero.command(archer, 'attack', nearest)

archerTarget = None

def thor(target):
    if target:
        if hero.canCast("chain-lightning", target):
            hero.cast("chain-lightning", target)


while True:
    badGuy = findStrongestTarget()
    if hero.gold > hero.costOf('soldier'):
        hero.summon('soldier')
    if not archerTarget or archerTarget.health <= 0:
        archerTarget = badGuy
    thanos = badGuy
    thor(thanos)
    friends = hero.findFriends()
    soldiers = hero.findByType('soldier')
    archers = hero.findByType('archers')
    for i, soldier in range(len(soldiers)):
        commandSoldier(soldier, i, len(soldiers));
    for i in range(len(archers)):
        archer = archers[i]
        commandArcher(archer, i, len(archer));

This should be indented backwards 4 spaces.

Where are you defining archerTarget? You need to define it inside the while loop.

You’re mixing the while loops together. You need to change it all the way to the second one.
Danny

I fixed it but it keeps showing this

Here is my new code

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.commandableMethods
    hero.commandableTypes
    hero.command(soldier, "defend", defendPos)

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

def commandArcher(archer):
    nearest = archer.findNearestEnemy()
    if archerTarget:
        hero.command(archer, 'attack', archerTarget)
    elif nearest:
        hero.command(archer, 'attack', nearest)

archerTarget = None

def thor(target):
    if target:
        if hero.canCast("chain-lightning", target):
            hero.cast("chain-lightning", target)


while True:
    badGuy = findStrongestTarget()
    if hero.gold > hero.costOf('soldier'):
        hero.summon('soldier')
    if not archerTarget or archerTarget.health <= 0:
        archerTarget = badGuy
    thanos = badGuy
    thor(thanos)
    friends = hero.findFriends()
    soldiers = hero.findByType('soldier')
    archers = hero.findByType('archers')
    for i, soldier in range(len(soldiers)):
        commandSoldier(soldier, i, len(soldiers));
    for i, range in range(len(archers)):
        archer = archers[i]
        commandArcher(archer, i, len(archer));

Never mind I beat it

3 Likes

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.