# Help in munchkin swarm!

**URL:** <https://discourse.codecombat.com/t/help-in-munchkin-swarm/2882>\
**Category:** Uncategorized\
**Created:** [February 26, 2015, 8:30pm UTC](https://discourse.codecombat.com/t/help-in-munchkin-swarm/2882 "2015-02-26T20:30:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![DustNite](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dustnite/32/2104_2.png) [@DustNite](https://discourse.codecombat.com/u/DustNite)\
**Post date:** [February 26, 2015, 8:30pm UTC](https://discourse.codecombat.com/t/help-in-munchkin-swarm/2882/1 "2015-02-26T20:30:45Z")

</div>

I am having trouble with my code, it is only attacking the enemy not the chest. My code is so far (with the problem):

```python
loop {
    enemy = this.findNearestEnemy();
    var distance = this.distanceTo("Chest");
     if (distance < 10) {
          if ( this.isReady("cleave")) {
        this.cleave(enemy);
    } else {
        this.attack(enemy);
    }
    if (enemy) {
        this.attack("Chest");
   
    }  
    }
    
    // Check the distance to the nearest enemy.
    // If it comes closer than 10 meters, cleave it!
    // Else, attack the "Chest" by name.
    
    
} 

```

---

<div class="post-metadata">

**Author:** ![Vlevo](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/vlevo/32/2074_2.png) [@Vlevo](https://discourse.codecombat.com/u/Vlevo)\
**Post date:** [February 26, 2015, 8:34pm UTC](https://discourse.codecombat.com/t/help-in-munchkin-swarm/2882/2 "2015-02-26T20:34:34Z")

</div>

you check distance to chest then attack enemy,  
you check if enemy exists then attack chest

switch those around

if enemy attack enemy  
if distance attack chest

edit:  
I just read the base comments, you should be checking distance to enemy not to chest, so:

if distanceTo enemy attack enemy  
ELSE attack chest

---

<div class="post-metadata">

**Author:** ![DustNite](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dustnite/32/2104_2.png) [@DustNite](https://discourse.codecombat.com/u/DustNite)\
**Post date:** [February 27, 2015, 12:21am UTC](https://discourse.codecombat.com/t/help-in-munchkin-swarm/2882/3 "2015-02-27T00:21:50Z")

</div>

i do not get 😕

**Merged Doublepost**

i do not get it at all

---

<div class="post-metadata">

**Author:** ![Vlevo](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/vlevo/32/2074_2.png) [@Vlevo](https://discourse.codecombat.com/u/Vlevo)\
**Post date:** [February 27, 2015, 6:14am UTC](https://discourse.codecombat.com/t/help-in-munchkin-swarm/2882/5 "2015-02-27T06:14:25Z")

</div>

```python
loop {
    enemy = this.findNearestEnemy();
    var distance = this.distanceTo("Chest"); // That is not enemy
    if (distance < 10) {
        if ( this.isReady("cleave")) {
            this.cleave(enemy);
        } else {
            this.attack(enemy);
        }
        if (enemy) { // This is not chest
            this.attack("Chest");
        }
    }
    
    // Check the distance to the nearest enemy. // as per this comment
    // If it comes closer than 10 meters, cleave it!
    // Else, attack the "Chest" by name. 
}

```

Ok, I’m with JFBM ugly indent makes ugly code (heck, I don’t like the so called one-true-brace) (I love python indenting, what is in is in and nothing hanging where it shouldn’t be)

Wow, now that I fixed the indent I see that the if enemy – atack chest is inside the if chest \< 10 away.
