# \[SOLVED\] Mad Maxer Help please!

**URL:** <https://discourse.codecombat.com/t/solved-mad-maxer-help-please/24527>\
**Category:** Level Help\
**Created:** [September 29, 2020, 8:29pm UTC](https://discourse.codecombat.com/t/solved-mad-maxer-help-please/24527 "2020-09-29T20:29:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wincon](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wincon/32/15097_2.png) [@Wincon](https://discourse.codecombat.com/u/Wincon)\
**Post date:** [September 29, 2020, 8:29pm UTC](https://discourse.codecombat.com/t/solved-mad-maxer-help-please/24527/1 "2020-09-29T20:29:48Z")

</div>

So this is my code for Mad Maxer,

```python
# 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

```

---

<div class="post-metadata">

**Author:** ![Grzmot](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/grzmot/32/13514_2.png) [@Grzmot](https://discourse.codecombat.com/u/Grzmot)\
**Post date:** [September 29, 2020, 9:25pm UTC](https://discourse.codecombat.com/t/solved-mad-maxer-help-please/24527/2 "2020-09-29T21:25:47Z")

</div>

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

> [@Wincon](#):
>
> ```python
> 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
> 
> ```

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

```python
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:

> [@Wincon](#):
>
> ```python
> enemy = hero.findNearestEnemy()
> if enemy:
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![Wincon](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wincon/32/15097_2.png) [@Wincon](https://discourse.codecombat.com/u/Wincon)\
**Post date:** [September 29, 2020, 9:42pm UTC](https://discourse.codecombat.com/t/solved-mad-maxer-help-please/24527/3 "2020-09-29T21:42:58Z")

</div>

this is my code now it does not work.

```python
# 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

```

---

<div class="post-metadata">

**Author:** ![Lydia\_Song](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/lydia_song/32/31461_2.png) [@Lydia\_Song](https://discourse.codecombat.com/u/Lydia_Song)\
**Post date:** [September 29, 2020, 10:17pm UTC](https://discourse.codecombat.com/t/solved-mad-maxer-help-please/24527/4 "2020-09-29T22:17:15Z")

</div>

Can you send me the link for this level?  
Lydia

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [September 29, 2020, 10:40pm UTC](https://discourse.codecombat.com/t/solved-mad-maxer-help-please/24527/5 "2020-09-29T22:40:45Z")

</div>

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’?

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [September 29, 2020, 10:43pm UTC](https://discourse.codecombat.com/t/solved-mad-maxer-help-please/24527/6 "2020-09-29T22:43:51Z")

</div>

@Lydia_Song: [https://codecombat.com/play/level/mad-maxer](https://codecombat.com/play/level/mad-maxer)

---

<div class="post-metadata">

**Author:** ![Lydia\_Song](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/lydia_song/32/31461_2.png) [@Lydia\_Song](https://discourse.codecombat.com/u/Lydia_Song)\
**Post date:** [September 29, 2020, 11:14pm UTC](https://discourse.codecombat.com/t/solved-mad-maxer-help-please/24527/7 "2020-09-29T23:14:40Z")

</div>

> [@Wincon](#):
>
> ```python
> 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
> 
> ```

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

> [@Wincon](#):
>
> ```python
> if farthest and farthest.health > 0:
> enemy = hero.findNearestEnemy()
> if enemy:
> hero.attack(enemy)
> pass
> 
> ```

and add this:

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

```

Lydia

---

<div class="post-metadata">

**Author:** ![Wincon](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/wincon/32/15097_2.png) [@Wincon](https://discourse.codecombat.com/u/Wincon)\
**Post date:** [September 29, 2020, 11:38pm UTC](https://discourse.codecombat.com/t/solved-mad-maxer-help-please/24527/8 "2020-09-29T23:38:38Z")

</div>

thank you for your help, It now works!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/a/a9445509d1a758c41b5ddcddc8c8e20a5dedb67f.png) [@system](https://discourse.codecombat.com/u/system)\
**Post date:** [September 30, 2020, 11:38am UTC](https://discourse.codecombat.com/t/solved-mad-maxer-help-please/24527/9 "2020-09-30T11:38:54Z")

</div>

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