Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

[SOLVED] Library Tactitian - Python

I am having trouble defeating Library Tactitian. My problem is one person dies, so I end up losing. Please help! Here’s my 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.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 i, soldier in enumerate(soldiers):
            commandSoldier(soldier, i, len(soldiers));
    for i in range(len(archers)):
        archer = archers[i]
        commandArcher(archer)

Please help me!

In your commandSoldier function, try adding a test to check on soldier.health; if it is above a certain value, then defendPos; if less, then defend Hushbaum instead…Hushbaum will eventually heal them and they’ll return to the circle.

This lasted for a while, but then a person died.

def commandSoldier(soldier, soldierIndex, numSoldiers):
    angle = Math.PI * 2 * soldierIndex/numSoldiers
    defendPos = {"x": 41, "y": 40}
    DefendPos = {"x": 42, "y": 42}
    defendPos.x += 10 * Math.cos(angle)
    defendPos.y += 10 * Math.sin(angle)
    if soldier.health > 50:
        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 i, soldier in enumerate(soldiers):
            commandSoldier(soldier, i, len(soldiers));
    for i in range(len(archers)):
        archer = archers[i]
        commandArcher(archer)

I tried using this to give me extra support, but it didn’t work:

while True:
    hero.moveXY(41, 40)
    hero.build("arrow-tower", 41, 40)

It wouldn’t let me build, even though I have and equipped the Stone Builder’s Hammer.

heh…good idea on the tower tho :wink:

Try increasing the health you are testing for. There are a few other issues, btw…I typically cover one at a time, so as not to confuse. However, if you’d prefer, I can list out what I see, all at once.

I tried increasing the health that I am testing for to 100, but it was the same result.

interesting…I just tried your code, with health test (I used > 60 and the alternate pos of 42, 40) and passed. The other issues I thought I saw are apparently not issues after all.

It worked. Thank you for helping me pass this level!

1 Like

Please pick a response and select the Solved checkbox…this will mark this topic so that others know they can move on.