Move the ELSE entirely out of the if-block (but still inside the loop!). The only moment this code will cause a movement if an enemy is present and not above you.
But your task is to move forward if there is no enemy, that is
if (enemy) {
// check if enemy is above or below
}
else{
// move
}
loop {
enemy = this.findNearestEnemy();
if (enemy) {
// A yak is above you if it’s y is greater than your y.
// If the yak is above you, build a fence 10m below it.
if(enemy.pos.y < this.pos.y){
var y = enemy.pos.y + 10;
var x = enemy.pos.x;
this.buildXY("fence", x, y);
}
if(enemy.pos.y > this.pos.y){
y = enemy.pos.y - 10;
x = enemy.pos.x;
this.buildXY("fence", x, y);
}
else {
var xx = this.pos.x + 10;
var yy = this.pos.y;
this.moveXY(xx, yy);
}
}
# Get to the oasis,
# fencing off paths with yaks on them as you go.
loop:
yak = self.findNearestEnemy()
if yak:
# If the yak is above you, build a fence 10m below it.
# 如果yak在我上面,在yak下面10米建立fence
if yak.pos.y > self.pos.y:
self.buildXY("fence",yak.pos.x , yak.pos.y -10)
# If the yak is below you, build a fence 10m above it.
# 如果yak在我下面,在yak上面10米建立fence
if yak.pos.y < self.pos.y:
self.buildXY("fence", yak.pos.x, yak.pos.y + 10)
pass
else:
# Move right 10m towards the oasis.
pass
x = self.pos.x +10
y = self.pos.y
self.moveXY(x, y)
Does it work? Please test your code before you post it. If it still does not behave as you like, you can ask about a specific problem (like “Why does my hero not move, move command is here”).
Earlier I made a mistake posting my code here is my code my hero will not move
// Get to the oasis,
// fencing off paths with yaks on them as you go.
loop {
enemy = this.findNearestEnemy();
if (enemy) {
// A yak is above you if it's y is greater than your y.
// If the yak is above you, build a fence 10m below it.
if(enemy.pos.y < this.pos.y){
var y = enemy.pos.y + 10;
var x = enemy.pos.x;
this.buildXY("fence", x, y);
}
if(enemy.pos.y > this.pos.y){
y = enemy.pos.y - 10;
x = enemy.pos.x;
this.buildXY("fence", x, y);
}
else {
var xx = this.pos.x + 10;
var yy = this.pos.y;
this.moveXY(xx, yy);
}
}
}
(Underscores for readability)
This is the third time you post your code. It is the same, it stays the same. I told you a possibility you could try. Please try it.
// Get to the oasis,
// fencing off paths with yaks on them as you go.
loop {
enemy = this.findNearestEnemy();
if (enemy) {
// A yak is above you if it's y is greater than your y.
// If the yak is above you, build a fence 10m below it.
if(enemy.pos.y < this.pos.y){
var y = enemy.pos.y + 10;
var x = enemy.pos.x;
this.buildXY("fence", x, y);
}
if(enemy.pos.y > this.pos.y){
y = enemy.pos.y - 10;
x = enemy.pos.x;
this.buildXY("fence", x, y);
}
}
else {
var xx = this.pos.x + 10;
var yy = this.pos.y;
this.moveXY(xx, yy);
}
}
Fixed your code, essentially. You may have to do a touch of changing of spacing.
Basically,
else {
var xx = this.pos.x + 10;
var yy = this.pos.y;
this.moveXY(xx, yy);
}
Needs to be INSIDE your MAIN “loop”, and OUTSIDE any of the IF loops. You have a main IF loop of “If Enemy” and 2 sub “if” loops of “If enemy.pos > my pos” or “if enemy pos < my pos”. You want to make sure that your walking “ELSE” loop (the one I quoted just before this paragraph) is not inside the IF Enemy loop. I’ve fixed that in the first code. Again, you may need to tweak the brackets slightly. But overall, the code that I first posted (an update of your code) has a high likelihood of working.
TL;DR - The first paste should be your fixed code The first pasted code is yours, just modified.
# Get to the oasis,
# fencing off paths with yaks on them as you go.
loop:
yak = self.findNearestEnemy()
if yak:
# If the yak is above you, build a fence 10m below it.
# 如果yak在我上面,在yak下面10米建立fence
if yak.pos.y > self.pos.y:
self.buildXY("fence",yak.pos.x , yak.pos.y -10)
# If the yak is below you, build a fence 10m above it.
# 如果yak在我下面,在yak上面10米建立fence
if yak.pos.y < self.pos.y:
self.buildXY("fence", yak.pos.x, yak.pos.y + 10)
pass
else:
# Move right 10m towards the oasis.
pass
x = self.pos.x +10
y = self.pos.y
self.moveXY(x, y)
I agree that this level has to be changed. I spend hours changing the code over and over until I got fed up with it and just copy and pasted Rytriks code. The code needs better comments otherwise beginners wont be able to code it correctly. At least write in the comments not to use distanceto. The comments:
// If the yak is above you, build a fence 10m below it.
// If the yak is below you, build a fence 10m above it.
and the yaks running at you suggest that you need the right timing to build and thus need distanceto.
But distanceto makes this level impossible.
I just realized that the comment suggesting to compare your y-position to the yak’s y-position was missing from all languages except JavaScript, so I went and added that into the other languages’ sample code as well. Thanks for the heads up @mystd!
I am new to coding, but haven’t had any trouble until this level. I tried this code, but it doesn’t work because it only defines the distance between self and yak, not whether it is up or down (-y or +y). So when a yak is < 20m away, it automatically builds y - 10, whether it is above or below. I think this is why myself, and other beginners are having such trouble.
I’m using this code, but getting told that I have an undefined function on line 11 (“this.buildXY(“fence”, xAbove, yAbove);”). I’m not seeing it.
// 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 it's y is greater than your y.
// If the yak is above you, build a fence 10m below it.
if (yak.pos.y > this.pos.y) {
var xAbove = yak.pos.x;
var yAbove = yak.pos.y + 10;
this.buildXY("fence", xAbove, yAbove);
} else if (yak.pos.y < this.pos.y) {
var xBelow = yak.pos.x;
var yBelow = yak.pos.y - 10;
this.buildXY("fence", xBelow, yBelow);
// If the yak is below you, build a fence 10m above it.
} } else {
// Move right 10m towards the oasis.
var newX = this.pos.x + 10;
var newY = this.pos.y;
this.moveXY(newX, newY);
}
}
Hey Jacob, looks like you have the Edge of Darkness equipped instead of the Crude Builder’s Hammer, so that’s why you don’t have the buildXY method. It should be giving you a much better error message, though! We’ll work on that. Otherwise, it looks like your code will be good to go.
Ah! Thanks so much! Gotta say, I’m relieved to find out that the problem was with my understanding of the game, not the language. Phew.
-Jacob
Merged Multipost
Sorry, Nick. I’m right back. Now my little swordsman can’t get to the
first spot to build a fence before he’s run down by a yak. How do you
fix a problem like that?
Merged Multipost
Nevermind, Nick. I adjusted the distance above or below my guy to build the fence, and that made it work.
loop {
var yak = this.findNearestEnemy();
if (yak) { // if there is a yak
if(yak.pos.x < this.pos.x){ // above us
this.buildXY("fence",yak.pos.x, yak.pos.y+10); // build a fence 10m below the yak
}else{ // or if it is not above us (which must be below us)
this.buildXY("fence",yak.pos.x, yak.pos.y-10); //build a fence 10 above the yak
}
} else {
this.moveXY(10+this.pos.x, this.pos.y); // no yak? just walk to the right
}
}
what happens is:
my hero walks 10m to the right
a yak comes from above
my hero builds a fence right at the end of the path, the yak stops at the fence
my hero walks 10m to the right
my hero walks 10m to the right
a yak comes from below
my wired hero builds a fence just at the very bottom of the path, right at about y=8
and than try to walk to the right from there
whats wrong with my code?
you are comparing “x” when you should be comparing “y”…
So you are saying “if yak behind me build above yak” else “build below yak” which is exactly what it does . . . build a fence below both yaks…
(you have the “below” code where the “above” code should be – according to the comments, this doesn’t effect the running of the code . . . just the reading of it.)