# Help, mad maxer\[SOLVED\]

**URL:** <https://discourse.codecombat.com/t/help-mad-maxer-solved/29101>\
**Category:** Level Help\
**Created:** [May 27, 2021, 1:40pm UTC](https://discourse.codecombat.com/t/help-mad-maxer-solved/29101 "2021-05-27T13:40:16Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gerald\_Jaydn\_Fernand](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/gerald_jaydn_fernand/32/22752_2.png) [@Gerald\_Jaydn\_Fernand](https://discourse.codecombat.com/u/Gerald_Jaydn_Fernand)\
**Post date:** [May 27, 2021, 1:40pm UTC](https://discourse.codecombat.com/t/help-mad-maxer-solved/29101/1 "2021-05-27T13:40:16Z")

</div>

```python
`https://codecombat.com/play/level/mad-maxer?
Can someone help me? Thanks!
![image|690x318](upload://2TZbIj3iEAFcU9F1iiGIHmQEdX8.jpeg)
# Attack the enemy that's farthest away first.

while True:
    farthest = None
    maxDistance = 0
    enemyIndex = 0
    enemy = hero.findNearestEnemy()
    # Look at all the enemies to figure out which one is farthest away
    while enemyIndex < len(enemy):
        target = enemy[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.
        while enemy.health > 0:
            hero.attack(enemy)`

```

---

<div class="post-metadata">

**Author:** ![Anonym](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/anonym/32/47546_2.png) [@Anonym](https://discourse.codecombat.com/u/Anonym)\
**Post date:** [May 27, 2021, 1:49pm UTC](https://discourse.codecombat.com/t/help-mad-maxer-solved/29101/2 "2021-05-27T13:49:49Z")

</div>

Hello and welcome to codecombat discourse! This is a cozy forum where you can share ideas, share fan art, get assistance for code, etc! Before you proceed, we hope that you review [this topic](https://discourse.codecombat.com/t/welcome-to-codecombat-discourse-forums/15601), which shows all essentials of this board! Thanks!

Hello, @Gerald\_Jaydn\_Fernand! Please read the stuff above, and format your code properly.

---

<div class="post-metadata">

**Author:** ![Eric\_Tang](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/eric_tang/32/15310_2.png) [@Eric\_Tang](https://discourse.codecombat.com/u/Eric_Tang)\
**Post date:** [May 27, 2021, 1:52pm UTC](https://discourse.codecombat.com/t/help-mad-maxer-solved/29101/3 "2021-05-27T13:52:05Z")

</div>

Huh. @Anonym Why is your @ not @ing him?

---

<div class="post-metadata">

**Author:** ![Anonym](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/anonym/32/47546_2.png) [@Anonym](https://discourse.codecombat.com/u/Anonym)\
**Post date:** [May 27, 2021, 1:52pm UTC](https://discourse.codecombat.com/t/help-mad-maxer-solved/29101/4 "2021-05-27T13:52:43Z")

</div>

idk, it didn’t work occasionally

---

<div class="post-metadata">

**Author:** ![Gerald\_Jaydn\_Fernand](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/gerald_jaydn_fernand/32/22752_2.png) [@Gerald\_Jaydn\_Fernand](https://discourse.codecombat.com/u/Gerald_Jaydn_Fernand)\
**Post date:** [May 27, 2021, 1:56pm UTC](https://discourse.codecombat.com/t/help-mad-maxer-solved/29101/5 "2021-05-27T13:56:48Z")

</div>

did I do something wrong?

---

<div class="post-metadata">

**Author:** ![Anonym](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/anonym/32/47546_2.png) [@Anonym](https://discourse.codecombat.com/u/Anonym)\
**Post date:** [May 27, 2021, 1:57pm UTC](https://discourse.codecombat.com/t/help-mad-maxer-solved/29101/6 "2021-05-27T13:57:43Z")

</div>

Please read [this topic](https://discourse.codecombat.com/t/welcome-to-codecombat-discourse-forums/15601) to format the code correctly.

---

<div class="post-metadata">

**Author:** ![Gerald\_Jaydn\_Fernand](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/gerald_jaydn_fernand/32/22752_2.png) [@Gerald\_Jaydn\_Fernand](https://discourse.codecombat.com/u/Gerald_Jaydn_Fernand)\
**Post date:** [May 27, 2021, 2:02pm UTC](https://discourse.codecombat.com/t/help-mad-maxer-solved/29101/7 "2021-05-27T14:02:05Z")

</div>

I did it so now what am I supposed to do. 🙂

---

<div class="post-metadata">

**Author:** ![Anonym](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/anonym/32/47546_2.png) [@Anonym](https://discourse.codecombat.com/u/Anonym)\
**Post date:** [May 27, 2021, 2:03pm UTC](https://discourse.codecombat.com/t/help-mad-maxer-solved/29101/8 "2021-05-27T14:03:22Z")

</div>

I am very sorry, but I can’t reply now(((((  
@Eric_Tang ?

---

<div class="post-metadata">

**Author:** ![Eric\_Tang](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/eric_tang/32/15310_2.png) [@Eric\_Tang](https://discourse.codecombat.com/u/Eric_Tang)\
**Post date:** [May 27, 2021, 2:04pm UTC](https://discourse.codecombat.com/t/help-mad-maxer-solved/29101/9 "2021-05-27T14:04:08Z")

</div>

I have school right now.

---

<div class="post-metadata">

**Author:** ![abc](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@abc](https://discourse.codecombat.com/u/abc)\
**Post date:** [May 27, 2021, 2:07pm UTC](https://discourse.codecombat.com/t/help-mad-maxer-solved/29101/10 "2021-05-27T14:07:01Z")

</div>

> [@Gerald\_Jaydn\_Fernand](#):
>
> ```python
> distance = hero.distanceTo(target)
> if distance > maxDistance:
> maxDistance = distance
> farthest = target
> 
> ```

It’s because this part is not inside of the while loop checking if enemyIndex is less than the length of enemies. Indent it one time.

> [@Gerald\_Jaydn\_Fernand](#):
>
> ```python
> while enemy.health > 0:
> hero.attack(enemy)`
> 
> ```

Here you want to check if farthest health is greater than zero. If it is, then attack farthest.

---

<div class="post-metadata">

**Author:** ![Gerald\_Jaydn\_Fernand](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/gerald_jaydn_fernand/32/22752_2.png) [@Gerald\_Jaydn\_Fernand](https://discourse.codecombat.com/u/Gerald_Jaydn_Fernand)\
**Post date:** [May 27, 2021, 2:10pm UTC](https://discourse.codecombat.com/t/help-mad-maxer-solved/29101/11 "2021-05-27T14:10:18Z")

</div>

I do not understand…

---

<div class="post-metadata">

**Author:** ![abc](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@abc](https://discourse.codecombat.com/u/abc)\
**Post date:** [May 27, 2021, 2:13pm UTC](https://discourse.codecombat.com/t/help-mad-maxer-solved/29101/12 "2021-05-27T14:13:21Z")

</div>

For the second part I mentioned, if you are attacking enemy, then you will be attacking the nearest enemy. You need to attack the farthest enemy not the nearest enemy. So you need attack farthest while farthest health is greater than zero. But if you attack farthest and do indent the first part I mentioned, farthest won’t update, so you won’t attack farthest. So all you need to do is restart your code, and then you need to add this in the if farthest loop:

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

```

---

<div class="post-metadata">

**Author:** ![Gerald\_Jaydn\_Fernand](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/gerald_jaydn_fernand/32/22752_2.png) [@Gerald\_Jaydn\_Fernand](https://discourse.codecombat.com/u/Gerald_Jaydn_Fernand)\
**Post date:** [May 27, 2021, 2:17pm UTC](https://discourse.codecombat.com/t/help-mad-maxer-solved/29101/13 "2021-05-27T14:17:08Z")

</div>

Thank you so much! you are awesome!

---

<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:** [May 28, 2021, 2:17am UTC](https://discourse.codecombat.com/t/help-mad-maxer-solved/29101/14 "2021-05-28T02:17:59Z")

</div>

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