Cant pass level sarven-shepherd: my hero wont attack


#1

It would appear that my hero wouldnt fight against being bullied, since he just gets beaten from the enemys and doesnt attack. Heres my code at the moment:

loop:
    enemies = self.findEnemies()
    enemyIndex = 0

    # Wrap this logic in a while loop to attack all enemies.
    # Find the array's length with: len(enemies)
while enemyIndex < len(enemies):
    enemy = enemies[enemyIndex]
    # "!=" means "not equal to."
    if enemy.type != "sand-yak":
        # While the enemy's health is greater than 0, attack it!
       while enemy.health > 0:
           self.attack(enemy)
           enemyIndex += 1

# Between waves, move back to the center.
self.moveXY(40, 30)

#2

Your while loop is outside of the main loop, so it is never reached (hint: indent the whole while block).

Also, you only increment enemyIndex when the enemy is not a sand-yak, so you’d get an infinite loop upon reaching the first sand-yak (hint: deindent enemyIndex += 1 twice).

self.moveXY should also be inside the loop.


#3
> loop:
>     enemies = self.findEnemies()
>     enemyIndex = 0
>     while enemyIndex < len(enemies):
>         enemy = enemies[enemyIndex]
>         if enemy.type != "sand-yak":
>             while enemy.health > 0:
>                 self.attack(enemy)
>                 enemyIndex += 1
>         pass
>     self.moveXY(40, 33)

My hero wont attack :confused: can someone tell me what`s wrong ?


#4

Move the enemyIndex +=1 out of the if

    enemyIndex=0
    while enemyIndex < len(enemies):
        enemy=enemies[enemyIndex]
        #do something with the enemy
            # conditional blocks are indented more than parent blocks
        enemyIndex+=1
    #end while

Observe that enemyIndex+=1 is at the same level as enemy=enemies[enemyIndex]

Also you do not need the pass. It means do nothing and it is only used if the code syntax requires a instruction to be present:

For example:

if (enemy):
     # attack enemy here
     pass

This is a teaching code. The if requires at least one instruction. But you do not have an instruction yet because the student must write it. So the teacher puts a pass until the student writes it


#5

Can you help me figure out what is wrong with my code? It keeps coming back saying that I have an infinite loop, or the code is really slow. Thanks.

while(true) {
    var enemies = hero.findEnemies();
    var enemyIndex = 0;

    while (enemyIndex < enemies.length) {
        var enemy = enemies[enemyIndex];
        if (enemy.type != "sand-yak") {
            while (enemy.health > 0) {
                hero.attack(enemy);
            }
            enemyIndex += 1;
        }
    }

    // Between waves, move back to the center.
    hero.moveXY(40, 32);
}

#6

Never mind, found out problem.

while (hero.now() < 60) {
    var enemies = hero.findEnemies();
    var enemyIndex = 0;
    while (enemyIndex < enemies.length) {
        var enemy = enemies[enemyIndex];
        var target = hero.findNearest(hero.findEnemies());
        if (target) {
            if (target.type != "sand-yak") {
                while (target.health > 0) {
                hero.attack(target);
                }
                enemyIndex += 1;
            }
            else {
                break;
            }
        }
    }
    hero.moveXY(40, 32);
}

#7

Actually, the problem with your first code is that it only increments the enemyIndex when the enemy is not a yak, so when it finds a yak the loop would get stuck checking the same enemy (yak) and never go forward. You need to always increment the enemyIndex so the loop can skip the yaks:

    var enemies = hero.findEnemies();
    var enemyIndex = 0;

    while (enemyIndex < enemies.length) {
        var enemy = enemies[enemyIndex];
        if (enemy.type != "sand-yak") {
            // ...
        }
        enemyIndex += 1; // Increment enemyIndex outside of the `if` statement
    }

But yeah, I see you have found a workaround to the problem and it works, so it’s fine I guess. Keep in mind the “correct” logic though, as you most likely will need it in the future.


#8

should my while loop be inside of the main loop r should it be outside of the loop?


#9

It should be inside of the main loop.


#10

but where is the center? :confused:


#11

The center? What do you mean by the center?


#12

never mind! Solved it already! :grinning: