Hunting Party Help!

This error is really weird since I gave this code to my friends and it worked fine.
The Code:

# Command your troops to move east and attack any ogres they see.
# Use for-loops and findFriends.
# You can use findNearestEnemy() on your soldiers to get their nearest enemy instead of yours.
self.moveXY(84, 48)
self.moveXY(61, 28)
self.moveXY(58, 67)
loop:
    enemy = self.findNearest(self.findEnemies())
    if enemy:
        self.attack(enemy)

The outcome is Ran Out of Time.

Your code does not command your soldiers, only your hero.
It can win the level if you have a hero with really powerful weapons.
If no powerful hero, it will fail because the hero will take too long to kill everyone all by himself.

Go back to earlier levels and see how you command your troops

If you still need help, post your new code and we’ll help you fix it.

Actually All my soilders go and attack.I will command.

self.moveXY(84, 48)
self.moveXY(61, 28)
self.moveXY(58, 67)
loop:
    enemy = self.findNearest(self.findEnemies())
    if enemy:
        self.attack(enemy)
    if friend:
        self.command(soilder, "attack")

does not work

And how you get the value of soldier?
You can get all your friends with:

soldiers=self.findFriends()

but then how you iterate (go over each solider in soldiers)?

2 Likes

Where do I put solider in soilders???

Commanding the soldiers does not take game it. Everything else your hero does takes game time, so command your soldiers first:

loop:
    #command soldiers
    #command hero

you already have the command hero part in the loop

If you are not sure how to command each soldier in soldiers go back to earlier levels for example, or see
the discussion on Mountain Mercenaries for how to give orders to each soldier:

I will be working on it.:relieved:

It still does not work!!!

# Command your troops to move east and attack any ogres they see.
# Use for-loops and findFriends.
# You can use findNearestEnemy() on your soldiers to get their nearest enemy instead of yours.
self.moveXY(84, 48)
self.moveXY(61, 28)
self.moveXY(58, 67)
loop:
    enemy = self.findNearest(self.findEnemies())
    friend = self.findFriends()
    if enemy:
        self.attack(enemy)
    for soilder in soilders:
        self.command(soilder, "attack", enemy)

“Still does not work” does not give any information. Usually we do not have time or possibility to run your code. Even if we can run it we do not know what you actually intended to do.

So

1) If there is an error, tells us the error and mark the line in your code.
If it runs but do not do what you wanted, tell us
2) What are you seeing
3) what are you expecting to see

I’ll teach you to debug the code. Teach a man to finish, …

Get a white piece of paper and start with the first line.
The paper will mark everything that the code interpreter know

First line 1) loop – KNOWN: nothing, code interpreter does not know a thing

2) *enemy =self.findNearest(self.findEnemies())*

KNOWN: enemy - hero’s nearest enemy, possibly empty

Remember the possibly empty notation. Always assume that the values that you are getting are junk or latter in life, malicious inputs intended to break down your site or application :slightly_smiling:

3) friend = self.findFriends()

KNOWN:
enemy -hero’s nearest, possibly empty
friend - list of soldiers, possibly empty

4) (1 out of 2) if enemy:

During each if there a two paths to take: case when the condition is true and the case when the condition is false.
Each extra if adds another path. If there are 3 ifs one inside the other, the whole block has 4 different paths you need to consider

If you are taking a path, then you add the condition=true to the list of things the code interpreter knows at that time

5) self.attack(enemy)

This is inside the if so it means that enemy is not empty
if (value) is the standard way of checking if a value is not empty.
KNOWNS: nemy, hero’s nearest, *NOT empty
friends: list of soldiers, possibly empty

self.attack needs a not empty enemy value so it is ok :slightly_smiling:

  1. (2 out of 2) There is no else branch for the if so we do nothing. When going over an “if” always count how many branches you need to go over and ask yourself: it is ok to ignore the missing branches?
    In this case the branch is no enemy, and it may be ok for the hero to do nothing if there are no enemies around

Once you finish the if, you drop the knowledge from the if condition so for now on enemy can be empty again.

6)  for soilder in soilders:

This will go over each soilder in the soilders list. It will be ok if the list is empty
KNOWS: enemy, heroes nearest, possibly empty (we are now outside the if so we have to drop the condition=true assumption.
friends - list of soliders, possibly empty

for soilder in soilders needs a soilders list-type value. The interpreter does not know it at the moment so BAM, your code fails.

7)  self.command(soilder, "attack", enemy)

Once you fix the code and reach here,
KNOW: enemy, hero’s nearest, possibly empty
soilder - an item from the list soilders, not empty
friends - soldiers list, possibly empty

Soilder is know not empty because you got it from for soilder in beforehand.
if you will get the soliders list each loop using findFriends, then it will not contain empty value.
But on a more general case you could place an if(soilder) to make sure soilder is not empty

Self.command needs:
soilder - not empty
enemy - not empty
If you do not have any of this, then you’ll get an error again

==================================================================

Finally, the level asks your soldiers to attack their nearest enemy not yours. Their nearest enemy for a soldier soldy can be got with:

soldy_nearest =soldy.findNearestEnemy()

This requires soldy to be known, not empty.
The result soldy_nearest will be known but possibly empty

1 Like

i just lured all the ogres into the archers and did not attack
i recommend speed ring and softened leather boots when you are doing this and decent gear:slightly_smiling: