while(true) {
var yak = hero.findNearestEnemy();
if (yak) {
if (yak.pos.y > hero.pos.y) {
hero.buildXY("fence", yak.pos.x , yak.pos.y - 10);
} else {
hero.buildXY("fence", yak.pos.x, yak.pos.y + 10);
} // if yak is not at top it's at bottom, then close if (yak) logic with another }
} else { // no yak - go to the target
hero.moveXY(hero.pos.x + 10, hero.pos.y // you missed );
}
}
// it can be formatted this way:
while(true) {
var yak = hero.findYak();
if (yak) {
if (yak_is_top)
hero.buildBottom(); // if you have only one action after the condition
else
hero.buildTop(); // you can miss {}
}
else
hero.moveToTarget(); // but this is not the right way to type js
}
Hello and welcome to codecombat discourse! This is a cozy forum where you can share ideas, share fan art, get assistance for code, etc! Before you proceed, we hope that you review this topic, which shows all essentials of this board! Thanks!
You want to check if there is a yak. If there is a yak, then check where its position is. If it’s y is greater than your hero’s y, then build a fence 10m below the yak. If the yak’s y is less than your hero’s y position, then build a fence 10m above the yak. Else, there is no yak, move right 10m.
I know python so you would need to format it like this in python:
while True:
yak = hero.findNearestEnemy()
if yak:
if yak.pos.y > hero.pos.y:
#Build a fence below
if yak.pos.y < hero.pos.y:
#Build a fence above
else: # There is no yak
#Move right
Not 100% but I think JS would be:
while (true) {
var yak = hero.findNearestEnemy()
if (yak) {
if (yak.pos.y > hero.pos.y) {
//Build fence below
}
if (yak.pos.y < hero.pos.y) {
//Build fence above
}
}
else {
//Move Right
}
}
Just replace the comments with build and move statements.