This is my code, it defeats the yeti and collects the coins. And it attacks the skeletons, but for some reason, it doesn’t summon the soldiers.
Somebody please help me!!
def commandSoldiers():
for soldier in hero.findByType("soldier"):
enemy = soldier.findNearestEnemy()
if enemy:
hero.command(soldier, "attack", enemy)
while True:
enemy = hero.findNearestEnemy()
soldiers = hero.findFriends()
soldierIndex = 0
soldier = soldiers[soldierIndex]
item = hero.findNearestItem()
if enemy:
if hero.isReady("throw"):
hero.throw(enemy)
else:
hero.attack(enemy)
elif item:
hero.moveXY(item.pos.x, item.pos.y)
elif hero.gold > hero.costOf("soldier"):
hero.summon("soldier")
for soldier in hero.findFriends():
enemy = soldier.findNearestEnemy()
if enemy:
hero.command(soldier, "attack", enemy)
commandSoldiers()
else:
hero.moveXY(19, 40)
This is my revised code, it does all the stuff that I said in the previous post, plus it summons soldiers now, but the soldiers won’t attack the skeletons. Please help!
def commandSoldiers():
soldiers = hero.findFriends()
if soldiers:
for soldier in soldiers:
if soldier and soldier.type == "soldier":
enemy = soldier.findNearestEnemy()
if enemy:
hero.command(soldier, "attack", enemy)
And about soldiers
elif hero.gold > hero.costOf("soldier"):
hero.summon("soldier")
friends hero.findFriends()
if friends:
for friend in friends:
if friend and friend.type == "soldier":
commandSoldiers()
else:
hero.moveXY(19, 40)
Delete the parts below because you already defined commandSoldiers()
I suggest you do something like this:
def commandSoldiers():
if hero.gold >= hero.costOf("soldier"):
hero.summon("soldier")
for soldier in hero.findByType("soldier"):
enemy = soldier.findNearestEnemy()
if enemy:
hero.command(soldier, "attack", enemy)
Delete this part because you already defined commandSoldiers() and the code below are included inside
Why are you adding this again if you already have it, so you can delete this part.
After this part try calling the commandSoldiers()
(I’m not sure if its going to work because I reset my progress for JavaScript and lost all my equipment so I can’t test the code to see if it works, I’ll try to assist without running the code)
I meant if you can copy and paste the code to the level and it completes, then it is a solution and it should be removed. If not, try to only give pieces of the correct code and only after you tried to explain a copule of times, ok @PeterPalov?