Help on Thunderhooves!

Help! When ever I try my code my play moves right to the first space, but when the yak appears, everything stops. What’s wrong with my code?

// Get to the oasis, // fencing off paths with yaks on them as you go.

loop {
var yak = this.findNearestEnemy();
if (yak) {

    // A yak is above you if its y is greater than your y.
    // If the yak is above you, build a fence 10m below it.

    if(yak.y > this.pos.y) {
        var x = this.pos.x;
        var y = yak.y-10;
        this.buildXY("fence", x, y);
    }

    // If the yak is below you, build a fence 10m above it.

    if(yak.y < this.pos.y) {
        var xA = this.pos.x;
        var yA = yak.y+10;
        this.buildXY("fence", xA, yA);
    }

} else {

    // Move right 10m towards the oasis.

    var xB = this.pos.x+10;
    var yB = this.pos.y;
    this.moveXY(xB, yB);
}
}

}

I dont know java script but for your “y” you have this.pos.y and for the yak’s “y” you have yak.y. It just makes me wonder if the yak.y is a problem, cause your “else” doesnt have a yak in it, and you said its working fine

Hello @icefurybman1 and welcome to the forums! Feel free to reuse existing topics (related to the level) to ask your question next time.


As @ZeroZeal pointed out, yak.y is not defined, what you need is yak.pos.y (which is the yak’s y position)

Furthermore, I believe there is no need to use separate x, xA and xB variables; you could just (re)use x throughout your code.

Cheers

@ant
My code still doesn’t work, and I fixed the yak.pos thing. Now, whenever I build a fence, I build it in a random place. Why? What’s wrong with my code?

// Get to the oasis, // fencing off paths with yaks on them as you go.

loop {

var yak = this.findNearestEnemy();
if(yak) {
var yakY = yak.pos.y;
if (yakY > this.pos.y) {

    // A yak is above you if its y is greater than your y.
    // If the yak is above you, build a fence 10m below it.

    var x = this.pos.x;
    var y = yakY-10;
    this.buildXY("fence", x, y);    
}

    // If the yak is below you, build a fence 10m above it.

if(yakY < this.pos.y) {
    var yA = yakY+10;
    this.buildXY("fence", x, yA);
}

} else {

    // Move right 10m towards the oasis.

    var x2 = this.pos.x+10;
    var y2 = this.pos.y;
    this.moveXY(x2, y2);
}
}

}

Well, in the if(yakY < this.pos.y) block you do not define x . You could simply define it as in the first if block, or you could move it outside the if block(s).

commented code
loop {

var yak = this.findNearestEnemy();
// you could define x here

if(yak) {
var yakY = yak.pos.y;
// or you could define x here, too

if (yakY > this.pos.y) {
    var x = this.pos.x;     // x is defined here - if you defined it above, you don't need this line
    var y = yakY-10;
    this.buildXY("fence", x, y);    
}

if(yakY < this.pos.y) {
    // x is not defined here!
    var yA = yakY+10;
    this.buildXY("fence", x, yA);
}

} else {
    var x2 = this.pos.x+10;
    var y2 = this.pos.y;
    this.moveXY(x2, y2);
}
}
} // one brace too many :-)

And once again: there is no need to use separate x, x2 and y, yA, y2 variables. Just use x and y, they should not conflict.