# Level: Wild Horses

Hey,

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

I am new here and my English isnāt the best.
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?

1 Like

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());
``````
1 Like

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

1 Like

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

1 Like

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.

1 Like

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.

3 Likes

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.

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

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?

Adrian, such a method does not exist.

Greetings zeldaball,
Your horse variable is declared as an array, as there are multiple horses (and friends in this case). How could you choose only ONE of those horses? You will also see the names of all the āfriend(s)ā in this case.

PAX

Hi @pblum12330, welcome to the CodeCombat discourse!
Thank you for posting, but could you not reply to a topic that is more than a year old unless you are looking for help yourself. The reason for this is, most of these users arenāt active any more and so thereās no point in you writing a response.
If you want to check whether someone is active, click on their profile, the click again and you should go to their profile page. Look for the āSeenā tab at the top of the page, if itās longer than 6 months then please donāt reply to them as they wonāt see it.
Thank you
Danny

Apologies,

I did not read all of the rules.

PAX,

I have a second post where I did that as well. O.o

1 Like

Donāt worry itās really alright, I just wanted to make sure you knew the rules from now onwards.
Danny