while True:
# Move to the nearest coin.
# Use move instead of moveXY so you can command constantly.
coin = hero.findNearestItem(hero.findItems())
hero.say(“I need coins!”)
hero.move(coin.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:
soldiers = hero.findFriends()
soldierIndex = 0
soldier = soldiers[soldierIndex]
# Loop over all your soldiers and order them to attack.
while soldierIndex < len(soldiers):
hero.command(soldier, “attack”, enemy)
# Use the ‘attack’ command to make your soldiers attack. #hero.command(soldier, “attack”, enemy)
Hi @Jacqueline, please could you post your code with the formatting so I can read it more easily and make sure the indentation (the spaces at the start of each line) is correct.
Instructions here (make sure you copy and paste the code in from the game):
while True:
# Move to the nearest coin.
# Use move instead of moveXY so you can command constantly.
coin = hero.findNearestItem(hero.findItems())
hero.say(“I need coins!”)
hero.move(coin.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:
soldiers = hero.findFriends()
soldierIndex = 0
soldier = soldiers[soldierIndex]
# Loop over all your soldiers and order them to attack.
while soldierIndex < len(soldiers):
hero.command(soldier, “attack”, enemy)
# Use the ‘attack’ command to make your soldiers attack.
#hero.command(soldier, “attack”, enemy)
while True:
# Move to the nearest coin.
# Use move instead of moveXY so you can command constantly.
coin = hero.findNearestItem()
hero.say("I need coins!")
hero.move(coin.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:
soldiers = hero.findFriends()
soldierIndex = 0
soldier = soldiers[soldierIndex]
# Loop over all your soldiers and order them to attack.
while soldierIndex < len(soldiers):
hero.command(soldier, "attack", enemy)
soldierIndex += 1
You don’t really need a while loop you can use a for loop instead. For each soldier in soldiers, command them to attack an enemy, if that enemy exists.
while True:
# Move to the nearest coin.
# Use move instead of moveXY so you can command constantly.
coin = hero.findNearestItem()
hero.say("I need coins!")
hero.move(coin.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:
soldiers = hero.findFriends()
soldierIndex = 0
soldier = soldiers[soldierIndex]
# Loop over all your soldiers and order them to attack.
while soldierIndex < len (soldiers):
hero.command(soldier(soldiers), "attack", enemy)
soldierIndex += 1
My new code is like this but I still don’t get what you are saying…
while True:
# Move to the nearest coin.
# Use move instead of moveXY so you can command constantly.
coin = hero.findNearestItem()
hero.say("I need coins!")
hero.move(coin.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:
soldiers = hero.findFriends()
soldierIndex = 0
soldier = soldiers[soldierIndex]
# Loop over all your soldiers and order them to attack.
for soldierIndex in hero.command:
hero.command(soldier(soldiers), "attack", enemy)
soldierIndex += 1
soldier = soldiers[soldierIndex]
try to put it in the for soldierIndex in hero.command:
soldier = soldiers[soldierIndex]
and the rest of the code that you wrote in it
And put in soldierIndex += 1 in the same place
also advice you to put in
item = hero.findNearestItem()
if (item):
hero.move(item.pos)
Instead of
coin = hero.findNearestItem()
hero.say(“I need coins!”)
hero.move(coin.pos)
I think this can be in a different way, “hero.command” needs to be something already there, and specifically a list, so you already have a list of the soldiers that are available, which is:
Plus for the part below, you can make a new variable, you shouldn’t use an old one because this here is an Index which means it is just a marker or a pointer that constantly increases per the number of items in the list that you are iterating in.
So what you really should remove is those 2 lines:
and this isn’t necessary as the index constantly increases after all the lines are run:
If there is an enemy, than you need to command each soldier to attack an enemy. You don’t need soldierIndex and you don’t need to define soldier. Just do this:
for soldier in soldiers:
if soldier:
#command a soldier to attack an enemy
while True:
# Move to the nearest coin.
# Use move instead of moveXY so you can command constantly.
coin = hero.findNearestItem()
hero.move(coin.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:
soldiers = hero.findFriends()
soldierIndex = 0
soldier = soldiers[soldierIndex]
# Loop over all your soldiers and order them to attack.
while soldierIndex < len(soldiers):
soldier = soldier[soldierIndex]
hero.command(soldiers, "attack", enemy)
soldierIndex += 1