Village Guard Level Unsolvable


#1

I can’t seem to complete the level ‘Village guard’. I can’t get my warrioir to move to the right entrance intime, although at first i was able to do that, but then it owuldn’t go back to the left entrance intime to get the last ogre. it is supper, super, so very very veyr very freaking frustrating. I would reeeeeaaaallllyyyy like some help before i bite through my hand in frustration or something… PS I’m using python and the script I wrote is something like this:
loop:
self.moveXY(60, 31)

  rightEnemy = self.findNearestEnemy()
  if rightEnemy:
      self.attack(rightEnemy)
      self.attack(rightEnemy)   

,
Basically after the left side is defended my hero just stands there until the first right ogre comes and then the hero can’t get to the right in time to save the villager.


#2

Please, format your code correctly. There is a button for that : </> . If you don’t know how to use it, please read FAQ.

```
enemy = self.findNearestEnemy()
if enemy:
    self.attack(enemy)
```

Trying to decipher what you’re trying to say and working on it. Is this you whole code up there ? Because there are several obvious reason why it’s not working.


#3

the whole code is something like

loop:
    self.moveXY(35, 34)
    leftEnemy = self.findNearestEnemy()
    if leftEnemy:
        self.attack(leftEnemy)
        self.attack(leftEnemy)
    # Now move to the right entrance.
    # Use "if" to attack if there is an enemy.
  loop:
      self.moveXY(60, 31)
      rightEnemy = self.findNearestEnemy()
      if rightEnemy:
          self.attack(rightEnemy)
          self.attack(rightEnemy)

#4

but what happens is that it just stays at (35,34) until the ogre shows up at about (60,31) and then it can’t get to the villager in time.


#5

You have two nested loops, which is why it doesn’t work as you’d like to work. The first part of the code, checks if there is an enemy on the left, which is fine. After that, you redo a loop in the loop : while on the left, check if there is someone on the right. Make it simpler with only one loop ! If you can’t find the solution, i’ll come back in 30min to help more.

On a general rule, having two nested loops is -usually- a not so good idea.


#6

thx. that worked. can’t believe the whole time i was doign the wrong hting! facepalm


#10

loop:
self.moveXY(35, 34)
leftEnemy = self.findNearestEnemy()
if leftEnemy:
self.attack(leftEnemy)
self.attack(leftEnemy)
# Now move to the right entrance.
# Use “if” to attack if there is an enemy.
loop:
self.moveXY(60, 31)
rightEnemy = self.findNearestEnemy()
if rightEnemy:
self.attack(rightEnemy)
self.attack(rightEnemy)
self.moveXY(35, 34)
ye = self.findNearestEnemy()
if ye:
self.attack(ye)
self.attack(ye)
self.moveXY(60, 31)


#11

Okay, so there’s a piece of code. Why is it there?


#12
# This function attacks the nearest enemy.
def findAndAttackEnemy():
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.attack(enemy)

def findAndCleaveEnemy():
    if hero.isReady("cleave"):
        findAndCleaveEnemy

while True:
    def findAndAttackEnemy():
        hero.moveXY(35, 34)
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.attack(enemy)
    
    def findAndCleaveEnemy():
        if hero.isReady("cleave"):
            hero.cleave(enemy)
    
hero.moveXY(60, 31)
findAndCleaveEnemy()
findAndAttackEnemy()

thats my code for village warder but I can’t find any reason why I cannot beat it. My hero only attacks one and doesn’t cleave. I do not know what run time error I have.