Enemy nearest changed every round


#1

Hi,

I’m very confused on this level [thunderhooves][1]
I don’t know if it’s normal or a bug.

With this code:

var lastYak = null;
loop {
    var yak = this.findNearestEnemy();
    if (yak && yak != lastYak) {
        if (yak.pos.y > this.pos.y) {
            this.buildXY("fence", this.pos.x, this.pos.y + 1);
        } else {
            this.buildXY("fence", this.pos.x, this.pos.y - 1);
        }
        lastYak = yak;
        this.say(lastYak);
    } else {
        this.moveXY(this.pos.x + 5, 31);
    }
}
```

Why from the second yak changed every round ?


  [1]: https://codecombat.com/play/level/thunderhooves

#2

You mean, why does the second yak come from a different direction each round? Because submissions give a random seed each time. Yaks that come from one direction might come from another.


#3

Not by each refresh but each loop if you prefer.
More simple is to try my code and look what the hero says.

The hero change a yak after each loop.
But in theory it’s always the same yak nearest by the hero.


#4

I think what’s happening is that your hero can see the previous yaks because they are not completely fenced off.

I believe the original instructions were to build the fence 10 meters above or below a yak’s position, not in relation to your hero’s position. (If you want the position to be in relation to your hero’s position, I think it’s by 5 meters.)