# Dungeon Arena: Coding problems

**URL:** <https://discourse.codecombat.com/t/dungeon-arena-coding-problems/1254>\
**Category:** Uncategorized\
**Created:** [October 31, 2014, 3:12pm UTC](https://discourse.codecombat.com/t/dungeon-arena-coding-problems/1254 "2014-10-31T15:12:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Crackers](https://avatars.discourse-cdn.com/v4/letter/c/22d042/32.png) [@Crackers](https://discourse.codecombat.com/u/Crackers)\
**Post date:** [October 31, 2014, 3:12pm UTC](https://discourse.codecombat.com/t/dungeon-arena-coding-problems/1254/1 "2014-10-31T15:12:28Z")

</div>

I’m not sure if this is a bug, or I just have issues with my code.

On the level tutorial, and the regular Dungeon Arena level, playing as a human, I have some problems:

The first is that the code: self.distance(enemy) allways gives me this error message:  
time 0.1: Find the distance to a target unit.

I can see that the “enemy” object is initalized, and has the type munchkin, it was created with this method:  
enemy = self.getNearest(enemies)

My second problem is with the librarian unit, I can’t cast spells on other units, because this code:  
if self.canCast(“slow”, enemy): self.castSlow(enemy) Gives me this error:  
time 0.1: Target must be a unit

This is my full code:

```python
friends = self.getFriends()
enemies = self.getEnemies()
if enemies.length is 0:
    return # Chill if all enemies are dead.

enemy = self.getNearest(enemies)
friend = self.getNearest(friends)

distanceFromFriend = friend.pos.x - self.pos.x
distanceFromEnemy = enemy.pos.x - self.pos.x

if distanceFromFriend < distanceFromEnemy and friend.type != "base":
    if self.canCast("haste", self.getNearest(friends)): 
        self.castHaste(self.getNearest(friends))
        return    
    elif friend.health < friend.maxHealth:        
        if self.canCast("regen", friend): self.castRegen(friend)
        return
    return
else:
    if self.canCast("slow", enemy): 
        self.castSlow(enemy) 
        return
    else:
        self.attack(enemy)
        return
    return

```

Also I couldn’t use the beneficial spells, my nearest “friend” is my base, is that expected?

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![DollarAkshay](https://avatars.discourse-cdn.com/v4/letter/d/91b2a8/32.png) [@DollarAkshay](https://discourse.codecombat.com/u/DollarAkshay)\
**Post date:** [October 31, 2014, 4:42pm UTC](https://discourse.codecombat.com/t/dungeon-arena-coding-problems/1254/2 "2014-10-31T16:42:40Z")

</div>

1.Distance Function

```
self.distance(enemy)

```

will give you an error because at time 0.1 the variable enemy will be [NULL.So](http://NULL.So) you are saying find distance to `NULL`.So check if the enemy is present and then call the distance function

```
if enemy
    dist = self.distance(enemy)

```

2.Enemy == NULL

```
if self.canCast("slow", enemy): self.castSlow(enemy)

```

this will give you an error because at time 0.1 there is no enemy.the the variable enemy will be NULL.You you are technically saying cast slow on `NULL`. A proper way to do this would be

```
if self.canCast("slow", enemy) && enemy : 
    self.castSlow(enemy)

```

3.If you are standing close to your base you base will be the closest friendly unit

Hope This Helped  
DA

---

<div class="post-metadata">

**Author:** ![Crackers](https://avatars.discourse-cdn.com/v4/letter/c/22d042/32.png) [@Crackers](https://discourse.codecombat.com/u/Crackers)\
**Post date:** [October 31, 2014, 5:50pm UTC](https://discourse.codecombat.com/t/dungeon-arena-coding-problems/1254/3 "2014-10-31T17:50:51Z")

</div>

Thanks a lot!

That works!

I’ll need to learn more, keep getting blocked on the code but for other issues now. XD

Best,  
Jorge

---

<div class="post-metadata">

**Author:** ![jbah8223](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jbah8223/32/1743_2.png) [@jbah8223](https://discourse.codecombat.com/u/jbah8223)\
**Post date:** [December 13, 2014, 8:56pm UTC](https://discourse.codecombat.com/t/dungeon-arena-coding-problems/1254/4 "2014-12-13T20:56:53Z")

</div>

I need help with that but in java
