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

[SOLVED] Library tactician help[python]

when i run my code it freezes up and nothing happens

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 = 0
    bestTarget = findStrongestTarget()
    enemies = self.findEnemies()
    enemyIndex = 0
for enemy in enemies:
    if enemy.health > mostHealth:
        bestTarget = enemy
        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

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)

Hi,
The reason you have an infinite loop is this line:

Why are you calling the function inside the function? This is called recursion, and you will do it in the glacier, but at the moment all it’s doing is causing an infinite loop.
Imagine how the code would run:

Then it will just go: 1, 2, 3, --1, 2, 3, --1, 2, 3, --1 etc…
That’s your infinite loop. Instead you can define it as None, as you did in earlier array levels.
Your code still doesn’t work, but you haven’t had an opportunity to test it, so I won’t say anything else.
Danny

Okay I will try that

THX @Deadpool198 it worked