[SOLVED] Mountain Mercenaries(python)

after a while of collecting coins, my hero just stops. then it goes into an infinity loop. please help. :slightly_frowning_face:

while True:
    coin = hero.findNearestItem()
    if coin:
        hero.move(coin.pos)
    
    # If you have funds for a soldier, summon one.
    if hero.gold > hero.costOf("soldier"):
        hero.summon("soldier")
        
    enemy = hero.findNearest(hero.findEnemies())
    if enemy:
        soldiers = hero.findFriends()
        soldierIndex = 0
        soldier = soldiers[soldierIndex]
        # Loop over all your soldiers and order them to attack.
        while soldier:
            
            # Use the 'attack' command to make your soldiers attack.
            #hero.command(soldier, "attack", enemy)
            hero.command(soldier, "attack", enemy)

change the while soldier into while soldierIndex < len(soldiers):

place this after the while loop
soldier = soldiers[soldierIndex]
and put this after the command
soldier = soldiers[soldierIndex]
then it should work
Hope this helps

It didn’t really help.

# Gather coins to summon soldiers and have them attack the enemy.
while True:
    coin = hero.findNearestItem()
    if coin:
        hero.move(coin.pos)
    
    # If you have funds for a soldier, summon one.
    if hero.gold > hero.costOf("soldier"):
        hero.summon("soldier")
        
    enemy = hero.findNearest(hero.findEnemies())
    if enemy:
        soldiers = hero.findFriends()
        soldierIndex = 0
        soldier = soldiers[soldierIndex]
        # Loop over all your soldiers and order them to attack.
        while soldierIndex < len(soldiers):
            
            # Use the 'attack' command to make your soldiers attack.
            #hero.command(soldier, "attack", enemy)
            hero.command(soldier, "attack", enemy)
            soldier = soldiers[soldierIndex]
        soldier = soldiers[soldierIndex]

this must be on top of the command
soldier = soldiers[soldierIndex]
the other one delete it
and add soldierIndex += 1
behind the command also in the while loop

basically like this
soldier = soldiers[soldierIndex]
hero.command(soldier, “attack”, enemy)
soldierIndex += 1

got it. I have now passed annother level with your help. thanks! :smile:

1 Like

no problem if you need help with other levels just call me by my name if i’m online i’ll try to help you

1 Like

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