``````# Navigate the mountain maze with a peasant pal.
# Advanced while-loop usage is required!

def moveBothTo(point):
while hero.distanceTo(point) > 1:
hero.move(point)
hero.command(peasant, "move", point)

peasant = hero.findNearest(hero.findFriends())
while True:
# Command your friend to build a decoy towards x + 1:
hero.command(peasant, "buildXY", "decoy", peasant.pos.x + 2, peasant.pos.y)
nextPoint = {"x": hero.pos.x, "y": hero.pos.y + 28};
moveBothTo(nextPoint)
# Create a new x/y dict +28 units away in the x dir:
nextPoint = {"x": hero.pos.x + 28, "y": hero.pos.y}
# Find the nearest enemy:
enemy = hero.findNearestEnemy()
# While there is an enemy:
while enemy:
# While the enemy's health is > 0:
while enemy.health > 0:
if enemy.type != "yeti":
# Attack the enemy:
hero.bash(enemy)
else:
hero.attack(enemy)
# Update the variable to the next nearest enemy:
enemy = hero.findNearestEnemy()
moveBothTo(nextPoint)

``````

I then run out of time, and it says that my code is really slow or an infinite loop.

Welcome back @BlackNinja!
Can you send me the link to this level?
Lydia

1 Like

Hmm, this is odd. Even if you complete the code exactly as it says it doesnâ€™t work. Even the solution on the level editor doesnâ€™t workâ€¦ @Chaboi_3000 any idea why this is happening.
@BlackNinja in the meantime you might have to come up with a more creative solution if the one they set out isnâ€™t working. The current problem with your one is it has â€śwhile enemy.health > 0:â€ť but inside you have â€śif enemy.type != â€śyetiâ€ť:â€ť. What if the enemy is a yeti? Your while loop wonâ€™t end. You could use break (exits out of one while or for loop, so youâ€™d have to do it twice to get out of both.
I canâ€™t remember what I normally do in this level, but at the moment I just chase the brawler.
Danny

1 Like

I finally solved it by switching all the 'whileâ€™s to if statements. Then, at the end I did moveXY to the treasure at the top of the level.

3 Likes

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.