Need advice with Mountain Mercenaries


#1

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

#2

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?


#3

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.


#4

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.


#5

I often find myself doing the same thing.