Hunters and prey [PYTHON]

Help please

# Keep your archers back while summoning soldiers to attack.

def pickUpCoin():
    # Collect coins.
    coin = hero.findNearestItem()
    if coin:
        hero.move(coin.pos)
    pass

def summonTroops():
    # Summon soldiers if you have the gold.
    if hero.gold > hero.costOf("soldier"):
        hero.summon("soldier")
        
    pass
    
# This function has an argument named soldier.
# Arguments are like variables.
# The value of an argument is determined when the function is called.
def commandSoldier(soldier):
    # Soldiers should attack enemies.
    friend = hero.findFriends()
    if friend and friend.type == "soldier":
        enemy = friend.findNearestEnemy()
        if enemy:
            hero.command(friend, "attack", enemy)
    pass

# Write a commandArcher function to tell your archers what to do!
# It should take one argument that will represent the archer passed to the function when it's called.
# Archers should only attack enemies who are closer than 25 meters, otherwise, stay still.
def commandArcher():
    friend = hero.findFriends()
    if friend and friend.type == "archer":
        enemy = friend.findNearestEnemy()
        archer.pos = (23, archer.pos.y)
        while True:
            hero.command(friend, "move", archer.pos)
        if enemy and friend.distanceTo(enemy) <= 25:
            hero.command(friend, "move", archer.pos)
            hero.command(friend, "attack", enemy) and hero.command(friend, "move", archer.pos)
            hero.command(friend, "move", archer.pos)
        else:
            hero.command(friend, "move", archer.pos)

while True:
    pickUpCoin()
    summonTroops()
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == "soldier":
            # This friend will be assigned to the variable soldier in commandSoldier
            commandSoldier(friend)
        elif friend.type == "archer":
            # Be sure to command your archers.
            commandArcher(friend)
            pass

lemme summon @JustALuke

no need to ping me, if I’m on the discourse I will see a new topic :smiley:
also next time could you post the link to the level; would help getting to it a lot (so I don’t need to type in the level link manually)

i caught you typing while i was eating

1 Like

uh my fish is mad im gonna feed him

also question are you fully done with codecombat

Instead of running hero.findFriends() inside of commandSoldier() and commandArcher() (which finds an array of units by the way, not just one object).
You just have to use the archer and soldier that is passed to your function, and you do need to add archer to the arguments in your function definition for commandArcher like it was done with commandSoldier
Also, you cannot use a While True loop in commandArcher or your hero will just sit there and command them but not collect anything
There is some redundant code in commandArcher but it won’t hurt anything

yes, with the campaign (other than a few new levels they added after my sub expired)
I might take part in future arenas if I have the time though

wait can you show me what that looks like

my world map?

no the code that you. said

It’s against the rules (and I agree)
image

I can help you further if you’re confused on something though

not solution i mean what the code fix you told me looks like

well for the first part
instead of def commandArcher(): you need to add archer in between the parantheses
delete archer.pos = (23, archer.pos.y) (you cannot set archer.pos)
and change all instances of friend to archer inside commandArcher
same thing with commandSoldier but with soldier ofc

uh my fish is mad wait right here

1 Like

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.