Help, Library Tactician! [PYTHON]

Hello, I have been working on this level for a while now but can’t seem to keep all of the soldiers alive, the same one always dies, does anyone have any suggestions? :frowning_face:

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)
    self.command(soldier, "defend", defendPos);
pass

def findStrongestTarget():
    mostHealth = 0
    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
    pass



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

archerTarget = None

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

Adding this line to the “commandSoldier()” should work:

def commandSoldier(soldier, soldierIndex, numSoldiers):
    angle = Math.PI * 2 * soldierIndex / numSoldiers
    defendPos = {"x": 41, "y": 40}
    if soldier.health > soldier.maxHealth * 0.3:
        defendPos.x += 10 * Math.cos(angle)
        defendPos.y += 10 * Math.sin(angle)
    self.command(soldier, "defend", defendPos);

Please do not revive dead topics unless you have a similar issue. @CranKeD_Wired hasn’t been on the forum for almost a year, so there is a pretty good chance that he will not even see your question.

Andrei

1 Like

hello @AnSeDra is there a mistake with 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)
    self.command(soldier, "defend", defendPos);
pass

def findStrongestTarget():
    mostHealth = 0
    enemies = hero.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
    pass



def commandSoldier(soldier, soldierIndex, numSoldiers):
    angle = Math.PI * 2 * soldierIndex / numSoldiers
    defendPos = {"x": 41, "y": 40}
    if soldier.health > soldier.maxHealth * 0.3:
        defendPos.x += 10 * Math.cos(angle)
        defendPos.y += 10 * Math.sin(angle)
    self.command(soldier, "defend", defendPos)

archerTarget = None

while True:
    if hero.gold > hero.costOf("soldier"):
        hero.summon("soldier")
    if not archerTarget or archerTarget.health <= 0:
        archerTarget = findStrongestTarget()
        friends = self.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)

and why do you always put andrei in the end of your sentence

Hello @wazi,

@AnSeDra is currently suspended so, he/she will not see your message. AnSeDra puts Andrei at the end of his sentence because (I’m assuming) that is his/her name.
Lydia

Why do you need this twice?

You do not need pass at the end of every def

I personally think hero is better.

I also think you do not need to go this much trouble. You can use a simple for loop.

This should be

def commandArcher(archer):

You don’t need to summon more soldiers

You need to delete an indent.

You can just say:

for i in range(len(soldiers)):
        soldier = soldiers[i]
        commandSoldier(soldier, i, len(soldiers))

This needs to be deleted an indent.
And you can just use:

for archer in archers:

I also recommend sticking with either self or hero as I see that you use both.
Lydia

Thank you very much for all the helps!

1 Like

so you passed the level? congrats! :partying_face:

Congrats on solving the level!
Lydia