While ogres were sleeping. Code doesn't work

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.

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

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

gives me this output in the console:

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

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

Have you replaced the logic

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 /

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.

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:

# 敌人在睡觉。这是破坏活动的最好时机!
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


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

1 Like

Hi @Munza and welcome to the forum! :partying_face:

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?