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
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++;
}
}
}
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
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
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!
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.
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)