[SOLVED]CodeCombat - Yeti Eater

I really need help with this level! I’ve tried looking at other topics people posted, but I can’t seem to figure out the solution! My code:

# Yetis surround us and we need to defeat them.
# Luckily the wizard had time to cast the sleep spell.
# Your hero can devour the yetis' vital powers when they are defeated.
# Defeat them in the order from weakest to the strongest.

# The wizard sorted enemies, but in the order from the strongest to the weakest.
wizard = hero.findNearest(hero.findFriends())
yetis = wizard.findEnemies()

# You need iterate the yetis list in the reverse order with a 'for-loop'.
# The start value should be 'len(yetis) - 1'.
# Iterate while the index greater than -1.
# Use the negative step -1.
len(yetis) - 1
    # Attack each enemy while its health greater than 0.
enemy = hero.findNearestEnemy()
while enemy.health > 0:
    hero.attack(enemy)

Python provides access to the very useful range function, documented here. I suggest looking through a couple of examples there to understand it. All the time range is used for iteration.

This level is perfectly suited for the range function. Your challenge is to figure out the right three arguments to iterate backwards through the indicies of yetis. If you don’t immediately know, that’s fine; there’s tremendous value in experimentation!

You can see the result of the range function with

hero.say(JSON.stringify(range(start, stop, step)))

# (JSON.stringify is usually JS only, but CodeCombat grants you access to it in Python as well)
# (Very handy in debugging)

Ultimately, your solution will look like so:

# For index in range(...)
    # get the yeti at that index
    # attack it while its health is greater than zero

I don’t get it. :frowning: (20 char)

If it helps, here’s my gear:

Screenshot 2021-06-01 9.31.31 AM

There are six yetis, correct? So your yeti index will look like so:
[yetiA, yetiB, yetiC, yetiD, yetiE, yetiF]

To look at them in reverse order: (yetiA, then yetiB, then yetiC…), what indices do you want to loop through?

I don’t get what you mean by:

My new code:

# Yetis surround us and we need to defeat them.
# Luckily the wizard had time to cast the sleep spell.
# Your hero can devour the yetis' vital powers when they are defeated.
# Defeat them in the order from weakest to the strongest.

# The wizard sorted enemies, but in the order from the strongest to the weakest.
wizard = hero.findNearest(hero.findFriends())
yetis = wizard.findEnemies()

# You need iterate the yetis list in the reverse order with a 'for-loop'.
# The start value should be 'len(yetis) - 1'.
# Iterate while the index greater than -1.
# Use the negative step -1.
len(yetis) - 1
[yetiA, yetiB, yetiC, yetiD, yetiE, yetiF]
    # Attack each enemy while its health greater than 0.
while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        while enemy.health > 0:
            hero.attack(enemy)

You can use a for loop.
For yeti in yetis:

Do something

Like this?

for yeti in yetis:
    enemy = hero.findNearestEnemy()
    if enemy:
        while enemy.health > 0:
            hero.attack(enemy)

It still doesn’t work. My code:

# Yetis surround us and we need to defeat them.
# Luckily the wizard had time to cast the sleep spell.
# Your hero can devour the yetis' vital powers when they are defeated.
# Defeat them in the order from weakest to the strongest.

# The wizard sorted enemies, but in the order from the strongest to the weakest.
wizard = hero.findNearest(hero.findFriends())
yetis = wizard.findEnemies()

# You need iterate the yetis list in the reverse order with a 'for-loop'.
# The start value should be 'len(yetis) - 1'.
# Iterate while the index greater than -1.
# Use the negative step -1.
len(yetis) - 1
[yetiA, yetiB, yetiC, yetiD, yetiE, yetiF]
    # Attack each enemy while its health greater than 0.
for yeti in yetis:
    enemy = hero.findNearestEnemy()
    if enemy:
        while enemy.health > 0:
            hero.attack(enemy)

There’s also an error:

Screenshot 2021-06-01 9.44.24 AM

For this part:

[yetiA, yetiB, yetiC, yetiD, yetiE, yetiF]

I’m not sure if I should use quotes or not!

I’m dealing with pseudo-code at the moment, don’t drop it in the editor and expect it to work.

Suppose you were hard-coding this solution. You first want to visit the last element in the array, then the second last, then the third last, etc.

What’s the index of the last element of the array?
What’s the index of the second last element of the array?
etc.

What happens:

Screenshot 2021-06-01 9.47.31 AM

Sorry. I wasn’t replying to @Hydrobolic.

And I still don’t get what you mean, @Hydrobolic.

Do you feel like you understand arrays well? Say, here or here.

For Brittle Morale, I cheated, and just did: hero.say("Gurulax")

But, for Lurkers, I did:



enemyIndex = 0
enemies = hero.findEnemies()

while enemyIndex < 6:
    enemy = enemies[enemyIndex]
    if enemy.type == "shaman":
        pos = enemy.pos
        x = pos.x
        y = pos.y
        hero.moveXY(x, y)
        hero.say ("Wake up!")
        enemyIndex += 1
        while enemy.health > 0:
            hero.attack(enemy)
    else:
        enemyIndex += 1

(I used someone else’s code.)