# While ogres were sleeping. Code doesn't work

**URL:** <https://discourse.codecombat.com/t/while-ogres-were-sleeping-code-doesnt-work/14858>\
**Category:** Level Help\
**Created:** [May 30, 2018, 11:10am UTC](https://discourse.codecombat.com/t/while-ogres-were-sleeping-code-doesnt-work/14858 "2018-05-30T11:10:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![chichi\_King](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chichi_king/32/8247_2.png) [@chichi\_King](https://discourse.codecombat.com/u/chichi_King)\
**Post date:** [May 30, 2018, 11:10am UTC](https://discourse.codecombat.com/t/while-ogres-were-sleeping-code-doesnt-work/14858/1 "2018-05-30T11:10:08Z")

</div>

```python
var points = [{x: 21, y: 8}, {x: 33, y: 8}, {x: 45, y: 8},
    {x: 57, y: 8}, {x: 68, y: 8}, {x: 68, y: 18},
    {x: 68, y: 28}, {x: 68, y: 38}, {x: 68, y: 48},
    {x: 68, y: 58}, {x: 56, y: 58}, {x: 44, y: 58}, 
    {x: 32, y: 58}, {x: 20, y: 58}, {x: 10, y: 60}];

var pointIndex = 0;

while (pointIndex < points.length) {
    var point = points[pointIndex];
    hero.moveXY(point.x, point.y);
    var enemy = hero.findNearestEnemy();
    var coin = hero.findNearestItem();
    // Attack only if the enemy.team is "ogres"
    // AND the enemy's health is less than 10
    if(enemy.team == "ogres" && enemy.health < 10) {
        hero.attack(enemy);
    }
    // Collect a coin if coin.value is less than 5
    // AND its distance is less than 7
    if(coin.value < 5 && coin.distance < 7) {
        hero.moveXY(coin.pos.x, coin.pos.y);
    }
    // Attack only if the enemy.health is less than 10
    // AND the enemy's type is "skeleton".
    if(enemy.health < 10 && enemy.type == "skeleton") {
        hero.attack(enemy);
    }
    pointIndex++;
}

```

Hero always pick up the farest coin so everybody can hear him.

 ![11](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/1/11333bde0a746c86253a2468cabe15fb16eaebab.jpg)

---

<div class="post-metadata">

**Author:** ![xython](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xython/32/7269_2.png) [@xython](https://discourse.codecombat.com/u/xython)\
**Post date:** [May 31, 2018, 9:28pm UTC](https://discourse.codecombat.com/t/while-ogres-were-sleeping-code-doesnt-work/14858/2 "2018-05-31T21:28:07Z")

</div>

There is an interesting error in this code ( + 2 logic errors ). chichi\_King is using coin.distance function that is not currently described anywhere.

```python
      var coin = hero.findNearestItem();
      print('coin.distance: ' + coin.distance);

```

gives me this output in the console:

```python
|Athena's Woodhouse| coin.distance: function(thang, fromEdges) {
    if (fromEdges == null) {
      fromEdges = false;
    }
    if (!(thang && (thang.isVector || thang.isThang || (!_.isNaN(thang.x + thang.y))))) {
      console.log('distance from', this.id, 'to', thang != null ? thang.id : void 0, 'did not work: isVector', thang != null ? thang.isVector : void 0, 'isThang', thang != null ? thang.isThang : void 0, 'x', thang != null ? thang.x : void 0, 'y', thang != null ? thang.y : void 0, 'keys', _.keys(thang));
      throw new ArgumentError("Find the distance to a target unit.", "distance", "target", "object", thang);
    }
    return Math.sqrt(this.distanceSquared(thang, fromEdges));
  }

```

I can’t understand how to use the second argument “fromEdges” . and when to use this function instead of distanceTo(object)?

---

<div class="post-metadata">

**Author:** ![chichi\_King](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chichi_king/32/8247_2.png) [@chichi\_King](https://discourse.codecombat.com/u/chichi_King)\
**Post date:** [June 1, 2018, 7:23am UTC](https://discourse.codecombat.com/t/while-ogres-were-sleeping-code-doesnt-work/14858/3 "2018-06-01T07:23:16Z")

</div>

Thank you for that! :)))  
I’ve used `hero.distanseTo(coin);` instead so it works

---

<div class="post-metadata">

**Author:** ![xython](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/xython/32/7269_2.png) [@xython](https://discourse.codecombat.com/u/xython)\
**Post date:** [June 1, 2018, 9:48am UTC](https://discourse.codecombat.com/t/while-ogres-were-sleeping-code-doesnt-work/14858/4 "2018-06-01T09:48:59Z")

</div>

Have you replaced the logic

```python
if(condition1) action1;
if(condition2) action2;
if(condition3) action3;

```

This can work with a particular seed, but it’s not the right one.  
/PS - off topic  
. Have you used the console to create a session for “Bash ‘em all”?. I’ve maybe a solution, but a little effort is needed from you /

---

<div class="post-metadata">

**Author:** ![chichi\_King](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chichi_king/32/8247_2.png) [@chichi\_King](https://discourse.codecombat.com/u/chichi_King)\
**Post date:** [June 1, 2018, 11:29am UTC](https://discourse.codecombat.com/t/while-ogres-were-sleeping-code-doesnt-work/14858/5 "2018-06-01T11:29:30Z")

</div>

Actually I have not. I didn’t get it.  
I do not know how to use console in that case. I only know how to open it and search for mistakes.  
Yes i’m asking because I’m just stuck with it. And sometimes HINTS is not enough.

---

<div class="post-metadata">

**Author:** ![Zong\_Wu](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/zong_wu/32/10857_2.png) [@Zong\_Wu](https://discourse.codecombat.com/u/Zong_Wu)\
**Post date:** [October 16, 2019, 2:42am UTC](https://discourse.codecombat.com/t/while-ogres-were-sleeping-code-doesnt-work/14858/6 "2019-10-16T02:42:45Z")

</div>

I fixed it you need to add something like change enemy.team to enemy.type and telling your hero back to the track  
here is my code, please take a look:

```python
# 敌人在睡觉。这是破坏活动的最好时机！
points = [{"x": 21, "y": 8}, {"x": 33, "y": 8},
    {"x": 45, "y": 8}, {"x": 57, "y": 8}, {"x": 68, "y": 8},
    {"x": 68, "y": 18}, {"x": 68, "y": 28}, 
    {"x": 68, "y": 38}, {"x": 68, "y": 48},
    {"x": 68, "y": 58}, {"x": 56, "y": 58},
    {"x": 44, "y": 58}, {"x": 32, "y": 58}, 
    {"x": 20, "y": 58}, {"x": 10, "y": 60}]

pointIndex = 0;

while pointIndex < len(points):
    point = points[pointIndex];
    hero.moveXY(point["x"], point["y"])
    enemy = hero.findNearestEnemy()
    coin = hero.findNearestItem()
    # 攻击仅当enemy.team是"ogres"
    # 且敌人的生命值小于10
    if enemy.type == "munchkin" and enemy.health < 10:
        hero.attack(enemy)
        hero.moveXY(point["x"], point["y"])
    # 收集硬币仅当coin.value小于5
    # 且其距离小于7
    if coin.value < 5 and hero.distanceTo(coin) < 7:
        hero.moveXY(coin.pos.x, coin.pos.y)
        hero.moveXY(point["x"], point["y"])
    # 攻击仅当enemy.health小于10
    # 且敌人的类型是"skeleton"。
    if enemy.type == "skeleton" and enemy.health < 10 and hero.distanceTo(enemy) < 10:
        hero.attack(enemy)
        hero.moveXY(point["x"], point["y"])
    pointIndex += 1

```

---

<div class="post-metadata">

**Author:** ![Munza](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/munza/32/11316_2.png) [@Munza](https://discourse.codecombat.com/u/Munza)\
**Post date:** [June 13, 2020, 8:06pm UTC](https://discourse.codecombat.com/t/while-ogres-were-sleeping-code-doesnt-work/14858/7 "2020-06-13T20:06:46Z")

</div>

> [@Zong\_Wu](#):
>
> `and hero.distanceTo(enemy) < 10`

I think that this is the important piece of information. It stops the hero taking a shortcut to a skeleton and waking the ogres.

---

<div class="post-metadata">

**Author:** ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)\
**Post date:** [June 14, 2020, 4:47am UTC](https://discourse.codecombat.com/t/while-ogres-were-sleeping-code-doesnt-work/14858/8 "2020-06-14T04:47:27Z")

</div>

Hi @Munza and welcome to the forum! 🥳

Please do not revive dead topics unless you need help on the same level, ok? But it is ok to try and help people, but in some newer topics, do you understand?
