Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

[SOLVED] Hunters and Prey please help

def pickUpCoin():
hero.toggleFlowers(False)
while True:
coin = hero.findNearestItem()
hero.move(coin.pos)
pass

def summonTroops():
if hero.costOf(“soldier”):
hero.summon(“soldier”)
pass

def commandSoldier(soldier):
friends = hero.findFriends()
for friend in friends:
enemy = friend.findNearestEnemy()
if enemy :
hero.command(friend, “attack”, enemy)
pass

def commandArchers():
friends = hero.findFriends()
for friend in friends:
enemy = friend.findNearestEnemy()
if enemy and friend.distanceTo(enemy) < 25:
hero.command(friend, “attack”, enemy)
else:
hero.command(friend, “move”, {“x”:friend.pos.x, “y”:friend.pos.y})

while True:
pickUpCoin()
summonTroops()
friends = hero.findFriends()
for friend in friends:
if friend.type == “soldier”:
commandSoldier(friend)
elif friend.type == “archer”:
commandArchers()
pass

my archers just attack the nearest enemy they find (the one on the other side of the map)

Hi @ActualllOLLIEpop and welcome to the forum! :partying_face:

Could you format your code as it is explaned here: https://discourse.codecombat.com/t/essentials-how-to-post-format-your-code-correctly/15521/2

So we can help you solve the level?

1 Like
def pickUpCoin():
    hero.toggleFlowers(False)
    while True:
        coin = hero.findNearestItem()
        hero.move(coin.pos)
    pass

def summonTroops():
    if hero.costOf("soldier"):
        hero.summon("soldier")
    pass

def commandSoldier(soldier):
    friends = hero.findFriends()
    for friend in friends:
        enemy = friend.findNearestEnemy()
        if enemy :
            hero.command(friend, "attack", enemy)
    pass

def commandArchers():
    friends = hero.findFriends()
    for friend in friends:
        enemy = friend.findNearestEnemy()
        if enemy and friend.distanceTo(enemy) < 25:
            hero.command(friend, "attack", enemy)
        else:
            hero.command(friend, "move", {"x":friend.pos.x, "y":friend.pos.y})

while True:
    pickUpCoin()
    summonTroops()
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == "soldier":
            commandSoldier(friend)
        elif friend.type == "archer":
            commandArchers()
            pass

Please, remove this loop

1 Like

def pickUpCoin():
hero.toggleFlowers(False)
while True:
coin = hero.findNearestItem()
hero.move(coin.pos)
pass

def summonTroops():
if hero.costOf(“soldier”):
hero.summon(“soldier”)
pass

def commandSoldier(soldier):
friends = hero.findFriends()
for friend in friends:
enemy = friend.findNearestEnemy()
if enemy :
hero.command(friend, “attack”, enemy)
pass

def commandArchers():
friends = hero.findFriends()
for friend in friends:
enemy = friend.findNearestEnemy()
if enemy and friend.distanceTo(enemy) < 25:
hero.command(friend, “attack”, enemy)
else:
hero.command(friend, “move”, {“x”:friend.pos.x, “y”:friend.pos.y})

sorry, ill, do it again with the formation : )

Remove this loop

Here put

commandArchers(friend)

Here put

commandArchers(friend)

Delete this loop

Instead of soldier put friend

Also, remove this loop

1 Like

def pickUpCoin():
hero.toggleFlowers(False)
while True:
coin = hero.findNearestItem()
hero.move(coin.pos)
pass

def summonTroops():
if hero.costOf(“soldier”):
hero.summon(“soldier”)
pass

def commandSoldier(soldier):
friends = hero.findFriends()
for friend in friends:
enemy = friend.findNearestEnemy()
if enemy :
hero.command(friend, “attack”, enemy)
pass

def commandArchers():
friends = hero.findFriends()
for friend in friends:
enemy = friend.findNearestEnemy()
if enemy and friend.distanceTo(enemy) < 25:
hero.command(friend, “attack”, enemy)
else:
hero.command(friend, “move”, {“x”:friend.pos.x, “y”:friend.pos.y})Preformatted text

for some reason it does’t want to format

Can you correct the things I told you that are wrong?

1 Like

So have you finished the level?

1 Like

ill try what you said and run it

it still doesn’t work. I’ll send you the new code.

def pickUpCoin():
    hero.toggleFlowers(False)
    while True:
        coin = hero.findNearestItem()
        hero.move(coin.pos)
    pass

def summonTroops():
    if hero.costOf("soldier"):
        hero.summon("soldier")
    pass

def commandSoldier(friend):
    friends = hero.findFriends()
    enemy = friend.findNearestEnemy()
    if enemy :
        hero.command(friend, "attack", enemy)
    pass

def commandArchers(friend):
    friends = hero.findFriends()
    enemy = friend.findNearestEnemy()
    if enemy and friend.distanceTo(enemy) < 25:
        hero.command(friend, "attack", enemy)
    else:
        hero.command(friend, "move", {"x":friend.pos.x, "y":frien.pos.y})

while True:
    pickUpCoin()
    summonTroops()
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == "soldier":
            commandSoldier(friend)
        elif friend.type == "archer":
            commandArchers()
            pass

Remove this loop. This is what is wrong

1 Like

Here put

commandArchers(friend)
1 Like

So now does it work?

1 Like

won’t removing the while loop make it run only once?

Yes, remove it. Please! Now is it alright?

1 Like