(Python): Hunter's Prey

I do not know what to do so far. This is what my code looks like:

# 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"):
        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 commandSoldiers():
    for soldier in hero.findFriends():
        enemy = soldier.findNearestEnemy()
        if enemy:
            hero.command(soldier, "attack", enemy)

# 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.

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
            commandSoldiers(friend)
        elif friend.type == "archer":
            # Be sure to command your archers.
            
            pass

Hi phantomofthedark2,

Firstly don’t bother with Archipelago-Gold’s suggestion. Your statement says “If hero.gold is greater than or equal to the cost of the soldier then”, and that should be fine.

You need to understand a new concept in functions in this level. The code when you started will have had this in it.

def commandSoldiers(soldier):

If you imagine that a function is a machine. Up until now, the machine has been using materials that it already has to process and produce an output. Now however we’re putting an input into the machine, which is ‘soldier’.

So you need to remove the for loop in your commandSoldiers(soldier) function (you’ll need to sort the indentation, but keep the rest of the code the same). Wherever the code comes across the word ‘soldier’ it will use the input.

If you look at when you call the code (in the while True loop) you then put a specific friend into the function, so the ‘machine’ knows what to operate the function on.

You need to write the same code for the commandArcher(whateverArguementYouPutInHere) function, and to then call the function in the while True loop.

Have a go at that, and post your code again if you need more help.

Jenny

7 Likes

I do not understand what you are saying. Can you simplify it? I am only 9.

Break it down. It isn’t too complicated.

How(20000000000000000000000000000)

Hmmm, sorry I’ve only just seen that you’ve asked for more explanation.

So your function with an argument might be something like:

def heroAttack(greenAliens):
   if greenAliens:
        hero.attack(greenAliens)

Now, there aren’t greenAliens in Coco (not that I’ve found yet). But that’s OK, as when we call the function we put:

enemy = hero.findNearestEnemy()
heroAttack(enemy)

The function will then replace all the greenAliens with enemy, so the code will get the hero to check if there’s an enemy, and attack the enemy if there is one.

Does that help? Can you now write some code which starts:

def commandSoldier(greenAliens):

So from what you said i understand that i have to change this:

def commandSoldiers():
    for soldier in hero.findFriends():
        enemy = soldier.findNearestEnemy()
        if enemy:
            hero.command(soldier, "attack", enemy)

To This:

def heroAttack(greenAliens)

so for here specify and define the enemy.

But don’t delete the function :smirk:

What do i put IN the function?

keep the stuff in the function but something like this

def soldierAttack(type of enemy here)

Inside the loop

soldierAttack(type of enemy here)

What do i put i n the the function of def heroAttack(greenAliens)

Put this bit inside the function.

        enemy = soldier.findNearestEnemy()
        if enemy:
            hero.command(soldier, "attack", enemy)

Only you need to change ‘soldier’ to ‘greenAliens’ (or change ‘greenAliens’ into ‘soldier’. It doesn’t matter which way round, as long as the input into the machine (function) is the same as the thing that it’s operating on).

1 Like

Im getting very confused?!

Wow its so cool that you are coding at 9! I started much later than you. Keep it up :+1:

1 Like

Thanks!(200000000000)

I started when I just turned 9 now I am 12.

Cool. I started when i was 8 and three quarters. I turned nine18 days ago.

1 Like

Wow cool so three years. I started at 10 and I’m 15 now

I started coding when I was 7 and a half and I still code to this day