Help I am stuck in Mountain Mercenaries (Python)

my code is

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

please help

Hi @Jacqueline, please could you post your code with the formatting so I can read it more easily and make sure the indentation (the spaces at the start of each line) is correct.
Instructions here (make sure you copy and paste the code in from the game):

Danny

1 Like

put your code in </> like this:

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

So, it is easier to read

Ok thank you for telling me

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

I still can’t solve it

1 Like

maybe use item = hero.findNearestItem()
if (item):
hero.move(item.pos)
This makes you move to move towards the coins

And maybe try to put soldier = soldiers[soldierIndex] into the while soldier index loop
IDK if it works but you can try it out :slight_smile:

You don’t really need a while loop you can use a for loop instead. For each soldier in soldiers, command them to attack an enemy, if that enemy exists.

What do you mean by for loop?

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

My new code is like this but I still don’t get what you are saying…

For loop example:

for *variable name* in *list name*:
    [do something]

Ok thank you! let me try

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

This is my New code

soldier = soldiers[soldierIndex]
try to put it in the for soldierIndex in hero.command:
soldier = soldiers[soldierIndex]
and the rest of the code that you wrote in it :slight_smile:
And put in soldierIndex += 1 in the same place

also advice you to put in
item = hero.findNearestItem()
if (item):
hero.move(item.pos)
Instead of
coin = hero.findNearestItem()
hero.say(“I need coins!”)
hero.move(coin.pos)

I think this can be in a different way, “hero.command” needs to be something already there, and specifically a list, so you already have a list of the soldiers that are available, which is:

Plus for the part below, you can make a new variable, you shouldn’t use an old one because this here is an Index which means it is just a marker or a pointer that constantly increases per the number of items in the list that you are iterating in.

So what you really should remove is those 2 lines:

and this isn’t necessary as the index constantly increases after all the lines are run:

try just doing
for soldier in hero.findFriends:
Do something
If you put attack in the for loop, all soldiers will attack your nearest enemy.

If there is an enemy, than you need to command each soldier to attack an enemy. You don’t need soldierIndex and you don’t need to define soldier. Just do this:

for soldier in soldiers:
    if soldier:
        #command a soldier to attack an enemy

You also do not need the hero.say() statements.

Hi @Jacqueline. All you need to change is:

  1. Remove the hero.say("I need coins!") line
  2. Check if there is a coin
  3. Remove the hero.say("I should summon something here!") line
  4. Remove the for-loop and replace it with a while-loop (that’s why the variable soldierIndex is there) and loop through your soldiers…
  5. … and use this syntax to command your soldiers:
element = list[elementIndex]
hero.command(element, "do stuff", target)
  1. And increment soldierIndex inside the while-loop

Hope this helps.

1 Like
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 soldierIndex < len(soldiers):
            soldier = soldier[soldierIndex]
            hero.command(soldiers, "attack", enemy)
            
    soldierIndex += 1

my new code but still incomplete…