Help on Mountain Mercenaries

while True:
    # Move to the nearest coin.
    # Use move instead of moveXY so you can command constantly.
    coin = hero.findNearestItem()
    hero.move(coin.pos)
    hero.say("I need coins!")
    
    # If you have funds for a soldier, summon one.
    if hero.gold > hero.costOf("soldier"):
        hero.say("I should summon something here!")
        
    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 True:
            
            
            # Use the 'attack' command to make your soldiers attack.
            #hero.command(soldier, "attack", enemy)
            hero.command(soldier, "attack", enemy)

I don’t really understand what to put at the end

Here put a for loop that loops over the soldiers array using soldier.

Why don’t you summon anything here?

Andrei

while True:
    # Move to the nearest coin.
    # Use move instead of moveXY so you can command constantly.
    coin = hero.findNearestItem()
    hero.move(coin.pos)
    hero.say("I need coins!")
    
    # If you have funds for a soldier, summon one.
    if hero.gold > hero.costOf("soldier"):
        hero.say("I should summon something here!")
        
    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 True:
            
            
            # Use the 'attack' command to make your soldiers attack.
            #hero.command(soldier, "attack", enemy)
            hero.command(soldier, "attack", enemy)

My hero still doesn’t gather any coins so they can’t summon any soldiers. Am I supposed to use findNearestItem?

You do not need this, do you?
And do not forget to correct those things too.

Andrei

Sorry overlooked that :sweat_smile: New Code:

while True:
    # Move to the nearest coin.
    # Use move instead of moveXY so you can command constantly.
    coin = hero.findNearestItem()
    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 True:
            
            
            # Use the 'attack' command to make your soldiers attack.
            #hero.command(soldier, "attack", enemy)
            hero.command(soldier, "attack", enemy)

And do not forget this.

Andrei


while True:
    # Move to the nearest coin.
    # Use move instead of moveXY so you can command constantly.
    coin = hero.findNearestItem()
    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 len(soldiers) > [soldierIndex]:
            # Use the 'attack' command to make your soldiers attack.
            #hero.command(soldier, "attack", enemy)
            soldier = soldiers[soldierIndex]
            hero.command(soldier, "attack", enemy)
            soldierIndex += 1

It seems that my hero ignores the presence of the ogres, and keep collecting coins. They have summoned the soldiers but don’t tell them to do anything

Delete those.

Here command to attack each soldier’s nearest enemy.

Andrei

What do you mean? Do I add it in after or inside the hero.command?

So make the variable enemy the enemy closest to soldier before you command to attack.

Andrei

Like this?

# Gather coins to summon soldiers and have them attack the enemy.

while True:
    # Move to the nearest coin.
    # Use move instead of moveXY so you can command constantly.
    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 len(soldiers) > [soldierIndex]:
            # Use the 'attack' command to make your soldiers attack.
            #hero.command(soldier, "attack", enemy)
            soldier = soldiers[soldierIndex]
            hero.command(soldier, "attack", enemy)
            soldierIndex += 1

No. But I have to go to bed now. Bye!

Andrei

1 Like

Bye (20 characters)(20 characters)

1 Like

Almost, but not quite.

Your first definition of soldier is not actually being used…you re-define soldier in the while loop, anyway.

Think about how you are defining your while statement…[soldierIndex] is pointing to an array element, not a counter. Without the brackets, it does then become a counter.

3 Likes

It works, but only one soldier attacks

# Gather coins to summon soldiers and have them attack the enemy.

while True:
    # Move to the nearest coin.
    # Use move instead of moveXY so you can command constantly.
    item = hero.findNearestItem()
    hero.moveXY(item.pos.x, item.pos.y)
    # 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 len(soldiers) > soldierIndex:
            # Use the 'attack' command to make your soldiers attack.
            #hero.command(soldier, "attack", enemy)
            hero.command(soldier, "attack", enemy)
            soldierIndex += 1


1 Like

Like the comment says, use move instead of moveXY

Here you define soldier before the while loop, so every time you

you command the same soldier.

1 Like

Thanks! I got it! :sweat_smile:

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