Level: Wild Horses


#1

Hey,

I love the game CodeCombat and so I am a adventure and diplomat. :slight_smile:

I am new here and my English isn’t the best. :wink:
I have a problem in the Level Wild Horses.
My code (I use JavaScript):

loop {
    // How do you find the nearest friendly unit?
    // horse = ?
    var horse = this.findFriends();
    
    if (horse) {
        var x1 = horse.pos.x - 7;
        var x2 = horse.pos.x + 7;
        var y = horse.pos.y;
        if (x1 >= 1) {
            // Move to the horse's y position but use x1 for the x position.
            this.moveXY(x1, y);
        } else if (x2 <= 79) {
            // Move to the horse's y position but use x2 for the x position.
            this.moveXY(x2, y);
        }
        var distance = this.distanceTo(horse);
        if (distance <= 10) {
            this.say("Whoa");
            // Move to the red x to return the horse to the farm.
            this.moveXY(27, 54);
            // Move back out into the pasture to begin looking for the next horse.
            this.moveXY(43, 18);
        }
    }
}

If I do this, then a mistake will be happen:

I try this:

loop {
    // How do you find the nearest friendly unit?
    // horse = ?
    var horse = this.findFriends();
    
    if (horse) {
        var hpos = horse.pos;
        var x1 = hpos.x - 7;
        var x2 = hpos.x + 7;
        var y = hpos.y;
        ...
        ...

But it happens the same mistake.
Is the mistake my fault or is the mistake a bug?


#2

Try to this.say(horse). What does it say? Can you find out from this info what`s wrong?

Spoiler:
You save a list to horse, but you need a single horse. Try:
var horse = this.findNearest(this.findFriends());

#4

Try this: this.say(horse). Still unsure invite @nick


#5

This post is useful.
http://discourse.codecombat.com/t/level-wild-horses/2991/2?u=munchingbrotatotoday


#6

I had a similiar problem until I used horse.pos.y in the y coords

this.moveXY(x1, y);

becomes

 this.moveXY(x1,horse.pos.y);

I’m using python but I believe it should be the same


#7

I’m not clear yet about when to use this. and when not to. It’s for any single instances, isn’t it?

So why: var horse = this.findNearest(this.findFriends());

But not: this.moveXY(x1, this. horse.pos.y);

Thanks for any clarification.


#8

this refers to your own object. Your object has some properties and functions.

this.findEnemies();            // Will find all enemies that YOU can see.
friendlySoldier.findEnemies(); // Will find all enemies the soldier stored in friendlySoldier can see

The arrays you get from these two calls are not necessarily the same. You may see enemies the soldier can’t , or vice versa. But you can order the soldier to attack a target he cannot see, but which is visible to you.


In your example you store the horse in a general all-purpose variable accidentally named horse. It is not attached to anything, just hanging around in mid-space, waiting to be used.
The second line doesn’t search in mid-space though. It looks in the object this, which doesn’t happen to have a horse saved. Because you explicitly told you want the horse from this and no other horse, you will get undefined as result, which then breaks your call for the property pos.

What you could do is this.horse = this.findNearest(this.findFriends()); Then you would save the horse in this.
But now there is no horse saved in mid-space, so horse.pos (without this) fails. The horse in this.horse is never looked at because you didn’t said so.


#10

Hello. I am using python and my code does not work. Can anyone help?

loop:
horse = self.findNearest(wild horse)
if horse:
x1 = horse.pos.x - 7
x2 = horse.pos.x + 7
if x1 >= 1:
self.moveXY(x1, horse.pos.y)
elif x2 <= 79:
self.moveXY(x2, horse.pos.y)
distance = self.distanceTo(horse)
if distance <= 10:
self.say(“Whoa”)
self.moveXY(27, 54)
self.moveXY(41, 18)
It is indented correctly.


#11

what is wild horse in your program?
Try self.findNearestFriend() instead


#12

Hey I am stuck on this level here is my code:
while(true) {
// How do you find the nearest friendly unit?
// horse = ?
var horse = hero.findFriends();
if (horse) {

    var x1 = horse.pos.x - 7;
    var x2 = horse.pos.x + 7;
    if (x1 >= 1) {
        // Move to the horse's y position but use x1 for the x position.
        hero.moveXY(x1, y);
    } else if (x2 <= 79) {
        // Move to the horse's y position but use x2 for the x position.
        hero.moveXY(x2, y);
    }
    var distance = hero.distanceTo(horse);
    if (distance <= 10) {
        hero.say("Whoa");
        // Move to the red x to return the horse to the farm.
        hero.moveXY(27, 54);
        // Move back out into the pasture to begin looking for the next horse.
        hero.moveXY(41, 29);
    }
}

}
It keeps saying property X is undefined any help?