Level mountain mercenaries

Soldiers is a list. A list is a more advanced type of variable which contains many individual values
For example soldiers will contain: soldiers[0], soldiers[1], soldiers[2], …
The number used to retrieve the individual values is called index.
In soldiers[2] we search for the value at the index 2

Additionally, a list will contain a special value soldiers.length which tell you how long is the list
Since the indexes start with 0 a list of length N will have elements from 0 to N-1
Remember the N-1 or you’ll get into errors latter

To loop over the list use:

soldierIndex=0
while (soldierIndex <soldier.length -1):
     // do something with soldiers[soldierIndex]
    soldierIndex++

If do not care about the index and you just to go over every value in the list, there is a simpler way:

for soldier in soldiers:
    // do something with solider

Thanks. I beat it rather quickly after I sent my most recent post. I just got the armor that gives you 1000 extra health and I beat it rather quickly once I figured out how to do it. Thanks :slightly_smiling:

I am doing this level and I incremented soldierIndex. But I build the soldiers and get coins but the soldiers don’t attack.

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

loop {
    var coin = hero.findNearest(hero.findItems());
    // Move to the nearest coin.
    // Use move instead of moveXY so you can command constantly.
    hero.move(coin.pos);
    
    // If you have funds for a soldier, summon one.
    if (hero.gold > hero.costOf("soldier")) {
        hero.summon("soldier");
    }
    
    var enemy = hero.findNearest(hero.findEnemies());
    if (enemy) {
        while (soldier < soldier) {
        // Loop over all your soldiers and order them to attack.
        var soldiers = hero.findFriends();
        var soldierIndex = 0;
        var soldier = soldiers[soldierIndex];
        // Use the 'attack' command to make your soldiers attack.
        hero.command(soldier, "attack", enemy);
        soldierIndex++;
        }
    }
}


While soldier < soldier will always return false.

You’re on the right track, but the while loop needs to compare something else.

Try comparing the soldierIndex to the total amount of soldiers you have (note that the total amount of soldiers changes constantly, but is a property of the list you already have called soldiers).

i hope this will help you figure out the solution.

You can also refer to a post by Adrian a few posts above this who explains the loops

Personally I rarely use while loops.

If I care about the index I use things like:

for i in range(len(soldiers)):

If I don’t I use

for soldier in soldiers

Works very well for me

Hello. Here is my code. I can’t even reach the end of the level because of some sort of lags. Hero, soldiers and enemies moves very slow. My quad core processor 3,2 GHz works on 100%, and then page crush. What I’m doing wrong?
I also have 8GB RAM, Nvidia 250 GTS, google chrome browser, and a very very good stable internet connection 30/30 Mb/sec.
I tryed it on FireFox. Still lag alot. This time it reaches to the end but I’m lose. Help please.

while True:
    while hero.now() < 20:
        items = hero.findItems()
        coin = hero.findNearest(items)
        if items:
            ItemIndex = 0
            while ItemIndex < len (items):
                one = hero.distanceTo(items [ItemIndex]) / items [ItemIndex].value
                two = hero.distanceTo(coin) / coin.value
                if one < two:
                    coin = items[ItemIndex]
                ItemIndex += 1               
        hero.move(coin.pos)
        while hero.gold > hero.costOf("soldier"):
            hero.summon("soldier")
    
        soldiers = hero.findFriends()
        soldierIndex = 0
        enemies = hero.findEnemies()
        enemy = hero.findNearest(enemies)
        if enemies and soldiers:
            while soldierIndex < len (soldiers):        
                soldier = soldiers[soldierIndex]
                hero.command(soldier, "attack", enemy)
                soldierIndex += 1

Find “mistake” in the code by myself: It works only first 20 sec :slight_smile: But still lag a lot.

After 3 houers of trying, at last I got error message: “Line 15: TypeErrore: Statement execution limit reached” what does it means?

My experience is Hard to Believe.
I did everything all right and my troops were overwhelmed.
I tried every combat technique and it won’t work.
Then I had the revelation to remove those “say” lines.
And it speeds up collecting coin 10 times faster!

It’s easy to believe. say is an action and it takes some time.

I mean, I did expect that statement to take some time, just like any other statement, but it is taking way more time than it should; feels almost like a bug.

Several actions take time: the moveXY action will “freeze” your code until the hero finishes moving to the given position, the attack action will freeze your code until the hero manages to deliver the attack. Likewise, the say action will freeze your code while the hero is saying something, which usually takes one second to conclude.

Hello. I am getting a "cannot read property ‘x’ of undefined. Heres my code. Can i know why?

while True:
    item = hero.findItems()
    if item:
        newX = item.pos.x
        newY = hero.pos.y
        hero.moveXY(newX, newY)
    if hero.gold > hero.costOf("soldier"):
        hero.summon("soldier")
    enemy = hero.findNearest(hero.findEnemies())
    if enemy:
        soldiers = hero.findFriends()
        soldierIndex = 0
        soldier = soldiers[soldierIndex]
        hero.command(soldier, "attack", enemy)
        soldierIndex += 1

Take a closer look at this line:

item = hero.findItems()

The findItems method returns an array of items, but it seems you want a single item. In that case, you probably want:

item = hero.findNearest(hero.findItems())
1 Like

thank you.
I guess I missed that.

My code almost works.
When my last soldier dies, I freeze up. Bottom statements of code

if enemy:
        soldiers = hero.findNearest(hero.findFriends())
        soldierIndex = 0
        soldier = soldiers[soldierIndex]
        self.command(soldiers, "attack", enemy)
        hero.move(item.pos)
        soldierIndex += 1
    elif soldiers == None:
        enemies = hero.findNearest(hero.findEnemies())
        hero.attack(enemies)

@JCRadical That would be correct.

If you are not checking if there are any soldiers and then accesings the soldier array of index 0 it will throw an error.

Add an if statement to check if an array of soldiers is returned from hero.findFriends()

Also you are mixing soldier code with soldiers code. What you want to do is to iterate through all of your soldiers and tell them to attack the enemy.

I figured it out. Thanks tho

this is my code

while True:
coin = hero.findNearestItem()
# Move to the nearest coin.
# Use move instead of moveXY so you can command constantly.
if coin:
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!")
    hero.summon("soldier")
enemy = hero.findNearest(hero.findEnemies())
if enemy:
    # Loop over all your soldiers and order them to attack.
    while True:
        soldiers = hero.findFriends()
        soldierIndex = 0
        soldier = soldiers[soldierIndex]
    
    # Use the 'attack' command to make your soldiers attack.
    #hero.command(soldier, "attack", enemy)
    hero.command(soldier, "attack", target)

can you tell me what im doing wrong

A few things:

  1. The hero needs to pause to speak, so you aren’t collecting coins fast enough by using hero.say().
  2. You have a while-true loop in a while-true loop. Review Desert levels that use this subject so you can break out of a loop or use a while-conditional.
  3. Be sure to format your code by surrounding it with triple backticks ( ` ) at the top and bottom of your code so we can see any indentation errors.

I think the problem is that you used item.pos.x instead of hero.pos.x on line 4.