Library Tactician Dec '15/ Apr '21

@Wiilli I don’t get where to put the bestTarget = findStrongestTarget()

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

def findStrongestTarget():
    mostHealth = 15
    bestTarget = findStrongestTarget
    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:
        self.command(archer, "attack", archerTarget)
    elif nearest:
        self.command(archer, "attack", nearest)
        
archerTarget = None

loop:
    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)

I did all the indents but I still don’t know what’s wrong.

It just has an error that says:

Maximum call stack size exceeded

@Neel_Sharma You need to surround your code with ```, see the FAQ for details.

Maximum call stack side exceeded means that you have an infinite recursion.
Once of your functions calls itself forever without an exit.

I see the following code:

def findStrongestTarget():
mostHealth = 15
bestTarget = findStrongestTarget

It should be

bestTarget= None

Use 3 backquotes ``` (top-left in yout keyboard) on a line by themselves to comment your code

your_Code

So I would use this.commandSoldiersoldier, i, soldiers.length); instead of this.commandArcher(soldier, i, soldiers.length);?

yes, in the 1st for loop, where you are supposed to command your soldiers.

There seems to be a ( missing in the code.

I added the (, and now I passed! Bows Thanks for all the help!

can you show me your code? I’m stunk in this for days, thank you !

Thank you really needed that!

Sorry it took so long for me to answer, I haven’t played in a long time…

I am not allowed to show you the code, but I can help you with yours.

I have figured out a way to defeat this level. You change the defend.Pos.x and y to +=7.5, not 10.
Hard level, though

:grinning:

1 Like

PLZ Help i cant get past this 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);

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 i in range(len(enemies)):
enemy = enemies[1]
if enemy.health > mostHealth:
mostTarget = enemy.health
bestTarget = enemy

    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 = hero.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])

here is the error

Jeff, please format all of your code, not just sections of it.

I tried stil dont work

hmm…when you click </> in the menu above the this chat window, you see this, right?:

image

and replacing ‘type or paste code here’ with your entire code is not working?

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 i in range(len(enemies)):
enemy = enemies[1]
if enemy.health > mostHealth:
mostTarget = enemy.health
bestTarget = enemy

    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 = hero.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])