Mega-munchkin Knows No Limits

Ivy the archer never runs away from the mean mega-munchkin. I thought the variable distance would return false after said munchkin is too close. Can anyone help me on where my logic fails here?

// That's a big'un! With some clever thinking, Ivy should be able to take care of this situation single-handedly.
loop {
    // Find the archer.
    var friend = this.findNearest(this.findFriends());
    var enemy = this.findNearest(this.findEnemies());
    
    // Tell the archer to attack the enemy as long as not too close to hit
    if(friend && enemy) {
        var distance = friend.distanceTo(enemy);
        while(distance > 5){
            this.command(friend, "attack", enemy);
        }
        // When too close run to safeZone
        var safeZone = {"x": 19, "y": 14};
        this.command(friend, "move", safeZone);
        }
    }
}

Hi @PixelAA

I tried your code (in my preferred python syntax) and tried to see using self.say(distance) at various places of the code to see where code is getting stuck. I found the problem zone but not the reason why it is a problem. Got to wait for someone more experienced explaining this problem.

Also, I figured the mega munchkin (should be called munchking :stuck_out_tongue:) has an attack range of ~6. So, should change distance limit to ~10. By tweaking while loops to if/elif, the level is passing. My code:

loop:
    x = x + 1
    enemy = self.findNearest(self.findEnemies())
    friend = self.findNearest(self.findFriends())
    if friend and enemy:
        distance = friend.distanceTo(enemy)
        if distance > 10:
            self.command(friend, "attack", enemy)
        elif distance <= 10:
            self.command(friend, "move", {"x": friend.pos.x - 10, "y":  friend.pos.y - 10})
1 Like

Interesting, when I changed from a while to an if else the logic works.

Can someone explain the reason?

Having my archer move to a specific distant (safeZone) location didn’t work out too well, she stopped attacking when the Munchking only had 12 hp left because she was cornered with the MunchKing too close. Your adjustment of the friend.pos got her through to the end.

Thanks!

1 Like

distance isn’t updated when you use the while loop.

You could add distance = friend.distanceTo(enemy) inside the while loop, but It’s probably easier to use the if / else syntax.

2 Likes

Thanks trotod! I guess the loop around all the code tricked me into believing that the scope for the while was the same as for an if/else. I’ll experiment more in the future with where I place my variables.