[SOLVED] Stuck on Mountain Mercenaries


#1

Can anyone help me with my code, it doesn’t work.

while True:
    # Move to the nearest coin.
    # Use move instead of moveXY so you can command constantly.
    hero.say("I need coins!")
    if hero.gold < hero.costOf("soldier"):
        while hero.gold < hero.costOf("soldier"):
            items = hero.findItems()
            nearest = hero.findNearest(items)
            if nearest:
                hero.say(hero.distanceTo(nearest))
                hero.move(nearest.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:
        # Loop over all your soldiers and order them to attack.
        soldiers = hero.findFriends()
        soldierIndex = 0
        if soldiers:
            hero.say("soldiers")
            while len(soldiers) > soldierIndex:
                soldier = soldiers[soldierIndex]
                if soldier:
                    hero.say("found one")
                    hero.command(soldier, "attack", enemy)
                soldierIndex += 1


#2

what do you mean “doesn’t work”? What is happening?


#3

my hero moves but the soldiers keep dying and the ogres kill me


#4

Maybe stop using say it wastes time.


#5

I did what you said but i still die


#6

here is my new code

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



#7

Just delete this, it’s irrevelevant.
You’ve already got this:


#8

Passed it thx :slight_smile: :wink: :stuck_out_tongue: :rofl: