Hi guys. I know I have a problem in my code, but can’t locate it. I don’t know why the “while” loop stops executing, because the condition is always “true” and the if else statement that i need, the –else if (heroY === 24)— is also definitely correct.
I only want to know why the “while” loop stops. Thank you in advance.
Here is my code:
hero.moveXY(18, 40);
var heroY;
heroY = hero.pos.y;
while (true) {
heroY = hero.pos.y;
hero.say("starting while");
var enemy = hero.findNearestEnemy();
var friend = hero.findNearestFriend();
var item = hero.findNearestItem();
if (enemy) {
if (hero.canCast("force-bolt")) {
hero.cast("force-bolt", enemy);
}
} else if (friend) {
if (hero.canCast("heal")) {
hero.cast("heal", friend);
}
} else if (item) {
if (item.type == "potion") {
hero.say("Potion! Yay!");
hero.moveXY(item.pos.x, item.pos.y);
}
} else if (heroY === 40) {
hero.say("moving down");
hero.moveXY(hero.pos.x, hero.pos.y - 16);
} else if (heroY === 24) {
hero.moveXY(hero.pos.x + 16, hero.pos.y + 16);
}
}
I dont know why the while loop stops (It could be an equipment problem) but i do know this.
Your soloution will not work. You will have to check the type (For example)
while (true){
enemy = hero.findNearestEnemy();
if (enemy && enemy.type == "munchkin"){
hero.attack(enemy);
}
}
Here is a pseudo code example
function Check(x, y){
hero.move(x, y);
enemy = hero.findNearestEnemy();
if (enemy && enemy.type == "munchkin"){
hero.attack(enemy);
}
}
You would have to keep on checking all the possible options, then you would call the function and give it some parameters so the hero would move to to parameters and check all of the options
Enderlord, my problem is not the type of something. My problem is that my while loop stops and the program ignores item.pos.y the second time, when I instruct the hero to go to the next (3rd, steel) door.
it is because of the random seeds. Sometimes it isn’t a potion. If there isn’t a potion then all of the conditions in your program would be false, so the program would exit out of the while loop. What happens if it is not a potion? Then you have to give the program an else statement (For example you would move to the next door)
It only gives you random seeds when you hit “Submit” button, which I haven’t done yet. I know that there are random seeds, but during any game, the hero’s y-coordinate is always known. Everything else is out of question.
Actually, I don’t think I learned about "for " loop. This is a level without "for " loops I believe. Is there any way to understand the problem of my program not seeing hero’s y-coordinate?
Xython, thank you I’ll try it out, but I want to underdstand the problem with particularly MY code. I figured out, if I change the x-coordinate, I have no problem. But with changing y-coordinate, I do.
See JavaScript | Math.abs( ) function
The hero will never be at the exact point you are sending him/her so you have to suppose some minimal error in the coordinates. .
Xython, look guys, I know of ways to complete this level using my own knowledge and using the ways from later (more advanced) levels. I don’t care about these ways. I want to understand how code works. If I have a mistake I want to correct it. I clearly have a mistake here with Y-coordinate, that’s it. I only need to understand what’s the problem with this specific Y-coordinate in my code. I thank you all for the replies!
// your code
while (true) {
// your code
} else if (Math.abs(heroY - 40) < 0.1 ) { // LINE CORRECTED
// because it is straight down the original code can remain if you want to ope only 3 doors
// your code
} else if (Math.abs(heroY - 24)< 0.1) { // LINE CORRECTED
// your code
}
This is your code with faulty lines corrected , you can try it.
Another edit
I put console.log('heroY: ’ + heroY); straight after while (true) { to see the real coordinates:
This is not needed, since you’re defining heroY here:
Also the hero.says(), it’s absolutely fine if thinks it helps you understand what’s going on; but I would advise not using them in your final result.
The problem lies where you think it doesn’t, which I think is why you’re having trouble finding it:
Why have you used === here, and == here:
(just saying == is the right way, well the way that works)
I’m not saying that will solve all your problems with this level (if you have any more), but that’s definitely one.
And I think @xython and enderlord were trying to help you actually finish the level with what you’re trying to do, but to me it looks like you’re not done, but you’ve run into an error early. Not sure just wanted to see.
Also can you not class code problems under “adventurer”, I know it’s hard to find a suitable category, but “uncategorized” is what all the senior discourse people say.
Hope this helps,
Deadpool thanks a ton, the thing is I stated I don’t need help with completing the level, but with understanding why my existing code doesn’t work. That’s why I found almost every answer irrelevant.
Now, as to your answer. I see that declaring variables several times is not needed. And I tried using == in my code instead of ===, but it doens’t make a difference. Btw I don’t see how hero.say() could hurt. In short, here is the edited code which doesn’t work:
hero.moveXY(18, 40);
while (true) {
hero.say("starting while");
if (hero.pos.x == 18 && hero.pos.y == 40) {
hero.say("moving down");
hero.moveXY(hero.pos.x, hero.pos.y - 16); //this works
hero.say("At this point I SHOULD have changed my hero.pos.y to 24");
} else if (hero.pos.y == 24) {//here it works only with x
hero.say("moving to the steel door");
} else if (hero.pos.y != 24) {
hero.say("My Y-position hasn't changed!");
}
}
Here even when the hero clearly has pos.y == 24 it still executed the last IF.