Need advice with Mountain Mercenaries

Hi, I’m relatively new to coding its been about a week and the websites been great. I’m having abit of trouble with Mountain Mercenaries though. As far as I know the code works the character moves through the coins to the next summons soldiers and the soldiers attack but they get overwhelmed very easily. If possible can you advise me on this?
Heres my code: (the coin while loop is indented it just doesnt show properly on the block quote)

while True:
# Move to the nearest coin.
# Use move instead of moveXY so you can command constantly.
if hero.gold < hero.costOf(“soldier”):
while hero.gold < hero.costOf(“soldier”):
items = hero.findItems()
nearest = hero.findNearest(items)
if nearest:
hero.distanceTo(nearest)
hero.move(nearest.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
    if soldiers:
        while len(soldiers) > soldierIndex:
            soldier = soldiers[soldierIndex]
            hero.command(soldier, "attack", enemy)
            soldierIndex += 1

What will help you the most on this mission is how quick you collect coins and how fast you build soldiers.

berhaps maybe it is a good time to use the jump ability to collect coins that are far?

you’ve really over complicated the first part of your code (the part that didn’t format properly here in Discourse). This is really slowing down the process of collecting coins and subsequently, summoning soldiers. All you need do is define coin and if coin, move to coin.pos. Keep it simple. The more complex your code is, the more room there is for errors.

Also, the if conditional, “if soldiers:” is superfluous and unnecessary.

The ring of speed would help a lot here too if you don’t have it yet.

Ahhh I see alright thanks for the help let me try it out quick. I do have a bad habit of over thinking things sadly lol. Yeah I purchased the ring of speed for this stage, but still had the issue.

Update: thanks @munkey it works perfect now. I really shouldn’t overcomplicate things lol.

1 Like

I often find myself doing the same thing.

1 Like
# 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.
    items = hero.findItems()
    coinIndex = 0
    while coinIndex < len(items):
        coin = items[coinIndex]
        hero.move(coin.pos)
        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
            while soldierIndex < len(soldiers):
                soldier = soldiers[soldierIndex]
                hero.command(soldier, "attack", enemy)
                soldierIndex += 1

Need some help with this. Whoever can help me, please help me.

This is primarily a structure issue. All of your code after the while coinIndex conditional is inside this while loop. You need to tab back the if conditional statements so that they are just inside the while True loop. Also, you don’t need to iterate through the array of coins. Simply define it, look for nearest, then use the move method to pick it up.

Additionally, I would remove the hero.say statement. It takes one complete second to execute the hero.say instruction. It accomplishes nothing other than slow you down and you need as many soldiers as possible to defeat all the ogres.