Help on Hunters and Prey

# 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 > 20:
        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 = friend.findNearestEnemy()
    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():
    enemy = friend.findNearestEnemy()
    if enemy:
        if hero.distanceTo < 25:
            hero.command(friend, "attack", enemy)

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()
            pass

One of the issues that this level demonstrates is that once you command your archers and soldiers to attack, they wind up running willy-nilly all over the map, leaving your reindeer unprotected from a flank attack. Try keeping your archers where they are by adding an else statement to your commandArcher function so that when they’re not attacking, they return to their original X coordinate position.

def commandArcher():
    enemy = friend.findNearestEnemy()
    archerPos = {"x": 23, "y": archer.pos.y}
    if enemy:
        if hero.distanceTo < 25:
            hero.command(friend, "attack", enemy)
        else:
            hero.command(friend, "move", archerPos)

With these two extra and simple lines of code, the archers will stay in a defensive position to protect the reindeer.

When I did this level I also wrote similar code for the soldiers’ function so they would stay closer the archers in a better defensive position and make the munchkins come to them to be killed, but it passes without it. It’s only necessary if you suffer from OCD like I do. :crazy_face:

2 Likes

Thanks a lot. I solved it already :smile:

yay good job !!!