New level : Mad Maxer

My code was 4 lines and I won. I just attacked in a loop. All I had was good armor and a good sword!
I will not post my code here though!!!:grin:

Okay, here’s my new bit but the sprite keeps thinking one enemy is the farthest but then switches because it sees another enemy. What to do?

# Kill the enemy that's farthest away first.

loop:
    farthest = None
    maxDistance = 0
    enemyIndex = 0
    enemies = self.findEnemies()

    # Look at all the enemies to figure out which one is farthest away.
    while enemyIndex < len(enemies):
        target = enemies[enemyIndex]
        enemyIndex += 1

        # Is this enemy farther than the farthest we've seen so far?
        distance = self.distanceTo(target)
        if distance > maxDistance:
            maxDistance = distance
            farthest = target

    if farthest:
        # Take out the farthest enemy!
        # Keep attacking the enemy while its health is greater than 0.
         if farthest.health > 0:
            self.attack(farthest)
        else:
            if self.isReady("cleave"):
                self.cleave(enemy)
        pass

Your code is working perfectly. The only problem is, once your hero attacks the farthest enemy, it is no longer the farthest any more. Here is a solution: Change the if enemy.health > 0: into a while-loop. That will tell your hero to attack until the enemy dies.

Oh, gee thanks. Will that make the code problem box pop up say limit is exceeded?

Uh, oh bad news. The code works exceedingly well BUT, the hero only attacks one enemy until it dies and then stops. I even used a flag pick up code to make the hero get close to the enemies but the hero wouldn’t move. Is there any critique to this?
If you want to post the code, please notify me.

You probably to do not have findNearestEnemy in the loop.

This code:

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

will only attack one enemy because you it never searches for a second one.

Another case is when you try apply a filter after a reduce operation:

enemy = self.findNearestEnemy();
if (enemy.type!="sand-yak")
    self.attack(enemy)

this code will fail and not attack anyone if an yak happens to be next to you.

1 Like

The things is you want to attack the farthest.

reset my gems thanks. username is checha1232

1 Like

I could not find any other topics so i posted on this even it is one year later sorry:smile_cat:

1 Like

Or you could create a new topic.

2 Likes