Mountain Mercenaries (Stuck)


#1

I am stuck on Mountain Mercenaries.There are just too many enemy units for my soldiers to handle, and soldiers aren’t attacking until they get attacked. How do I fix my code?

while True:
    # Move to the nearest coin.
    # Use move instead of moveXY so you can command constantly.
    coins = self.findItems()
    coinIndex = 0
    coin = coins[coinIndex]
    if coin:
        self.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:
        # Loop over all your soldiers and order them to attack.
        
        soldiers = hero.findFriends()
        soldierIndex = 0
        soldier = soldiers[soldierIndex]
        
        # Use the 'attack' command to make your soldiers attack.
        while soldierIndex < len(soldiers):
            hero.command(soldier, "attack", enemy)
            soldierIndex += 1

#2

Its because your only ordering one soldier to attack which is the first one in the array.

while soldierIndex < len(soldiers):
            hero.command(soldier, "attack", enemy) # Does not change the soldiers Index
            soldierIndex += 1

Try

if enemy:
        # Loop over all your soldiers and order them to attack.
        
        soldiers = hero.findFriends()
        soldierIndex = 0
                                                           #Moved Line
        
        # Use the 'attack' command to make your soldiers attack.
        while soldierIndex < len(soldiers):
            soldier = soldiers[soldierIndex]              # To here
            hero.command(soldier, "attack", enemy)
            soldierIndex += 1