[Solved] Hunters and Prey (python)

Literally asking the same thing that everyone else is, what part of my code is causing my archers to move with the soldiers?

# Ogres are trying to take out your reindeer!
# Keep your archers back while summoning soldiers to attack.

def pickUpCoin():
    # Collect coins.
    item = hero.findNearestItem()
    hero.move(item.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.
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.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(archer):
    enemy = friend.findNearestEnemy()
    if enemy:
        distance = friend.distanceTo(enemy)
    elif enemy and distance < 25:
        hero.command(friend, "defend", "Reindeer")

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

Thanks!

you don’t need to defend the reindeer. Your troops don’t know what the reindeer are because you haven’t defined it. Tell it to attack because if the distance is too far, it wont attack. Other than that, it should be fine

here i think. your telling all friends.

1 Like

it should be archer i think

and here, you’re only commanding the archer to protect one reindeer, but the goal is all

Also, you might want to nest your elif enemy and distance < 25: into your if enemy: function, and then remove the enemy part of the elif. Oh and make the elif an if

the same thing appears in the given code as

commandSoldier(friend)

and I’ve made it so the command will only function for soldiers, or in the archer case, archers

The problem here is that if there is no enemy and i’m only calculating distance, a null from the code

distance = friend.distanceTo(enemy)

then my code will break.

its because ur not calculating distance before checking it

    if enemy:
        distance = friend.distanceTo(enemy)
    elif enemy and distance < 25:
        hero.command(friend, "defend", "Reindeer")

if theres an enemy it calculates distance first…

also random note my soldiers don’t move…

That if/else statement doesn’t make sense. Only if there is no enemy will it move on to the elif statement, and since there is no enemy it will not run.

yea but you will never get to the elif part because it will always say “Oh there’s an enemy. Lets find the distance! Let’s attack them!” instead of “Oh there’s an enemy. How far is it? Oh no, it’s farther than 25 meters. Let’s stay where we are.”

lmao because of my gear I can just order the archers to stay put and have myself attack everything bc the munchkins only come from the side if the archers cross a certain x value.

# Ogres are trying to take out your reindeer!
# Keep your archers back while summoning soldiers to attack.

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

def summonTroops():
    # Summon soldiers if you have the gold.
    if hero.gold >= hero.costOf("soldier") or hero.costOf("archer"):
        hero.summon("soldier") or hero.summon("archer")
    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():
    # Soldiers should attack enemies.
    for soldier in hero.findFriends():
        enemy = hero.findNearestEnemy()
        if enemy:
            hero.command(soldier, "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():
    for archer in hero.findFriends():
        enemy = hero.findNearestEnemy()
        distance = archer.distanceTo(enemy)
        if enemy and distance < 25:
            hero.command(archer, "attack", enemy)
        else:
            pass
    

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

this is my code… i really need some help. this is the error I get on line 24
Screenshot 2023-04-08 17.11.44