[SOLVED] Mad Maxer Help please!

So this is my code for Mad Maxer,

# Attack the enemy that's farthest away first.

while True:
    farthest = None
    maxDistance = 0
    enemyIndex = 0
    enemies = hero.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 = hero.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.
        enemy = hero.findNearestEnemy()
        if enemy:
            hero.attack(enemy)
        pass

Hi @Wincon, the issue with your code is this:

You need to continue attacking the enemy so your if statement should like this:

if farthest and farthest.health > 0:

Then, you don’t need to find enemy or look to see if there is an enemy. All you need to do is change your if statement and get rid of this:

Then, I think your code will work. Hope this helps!
Grzmot

this is my code now it does not work.

# Attack the enemy that's farthest away first.

while True:
    farthest = None
    maxDistance = 0
    enemyIndex = 0
    enemies = hero.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 = hero.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 and farthest.health > 0:
            enemy = hero.findNearestEnemy()
            if enemy:
                hero.attack(enemy)
        pass

Can you send me the link for this level?
Lydia

In the first section of your code, you went to a lot of trouble to define ‘farthest’ However, at the end, you are re-defining ‘enemy’…what happened to ‘farthest’?

@Lydia_Song: https://codecombat.com/play/level/mad-maxer

1 Like

You want to attack the farthest enemy, so delete this:

and add this:

while farthest.health > 0:
            hero.attack(farthest)

Lydia

1 Like

thank you for your help, It now works!

1 Like

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