Level: Lowly Kithmen

For some reason when I collect the first gem and go up and move towards the second, the ogre comes to attack me and I put the this.findNearestEnemy(); this.attack(enemy); this.attack(enemy); and instead of attacking the second ogre which is the nearest enemy it travels back to the first ogre which is dead and gives me the line “I cannot attack that which is already dead.” then he walks downwards and kills himself. I don’t know if this is actually a bug or if my code is incorrect, yet I am in a beginners computer programming class and the teacher says that he thinks it is a bug. I’d be very grateful for a response on how to fix this if there is a solution. Thank you.

Try assigning this.findNearestEnemy() to the enemy variable when you can see the second enemy:

enemy = this.findNearestEnemy();  // This updates the value stored in the `enemy` variable.

If you just call this.findNearestEnemy(); without doing anything with the return value, it won’t have any effect.

This is a really hard part, so we are in the middle of making a few more levels to introduce this more clearly!

Okay thank you nick I got it now. Yea it didn’t exactly tell me that I was suppose to create another variable at first but it did today.

I’m having an issue where I am not picking up the gems. Yet I stand right on them.
All I can think of is something about me not using a second variable might be causing this, but I don’t know why.
Here is my code
enemy = self.findNearestEnemy()
self.attack(enemy)
self.attack(enemy)
enemy = self.findNearestEnemy()
self.attack(enemy)
self.attack(enemy)
self.moveDown()
self.moveRight()
self.moveRight()

I was having the same issue as yourself and all I had to do was close the level and open it again.
My code (which finishes the level) is exactly the same as yours except I have given the enemies different names.

I tried out your code and it does finish the level so it must be a problem with your browser. To fix it I suggest just opening and closing your web browser or clearing the cache.

Sorry about that one, guys–it’s this bug. Still have no idea what’s causing it, but will be hoping to drill down and get it soon!

Let me know if you have any extra info about how to reproduce it more consistently.

This is what my code looks like. After he kills the first ogre, he just ignores the other.

var enemy1 = this.findNearestEnemy();
var enemy2 = this.findNearestEnemy();

this.attack(enemy1);
this.attack(enemy1);
this.attack(enemy2);
this.attack(enemy2);

Ah, I see the problem. You need to look for enemy2 after you kill enemy1, because otherwise you just have two variables references to the closest munchkin called enemy1 and enemy2 and no variables referencing the second munchkin (who was further away when you defined both variables at the beginning of the level).

I’ve tried several things. I tried adding more moves in between so that when it searches, the enemy will now be the closest enemy. Still no luck. I even put it on a loop in case he might catch it.

I think what is bothering me most is that I am trying to find the level or tutorial tip that actually explains how to get out of this problem, and nothing is helping. When you suggest how to resolve this problem, are you suggesting it because you know how to already in your own mind? Or are you suggesting it as in there was a level, or tutorial somewhere that I need to go back and reference to learn how to remedy this?

If its the latter, can you please point me to the level?

I don’t know what your code looks like now, so I’ll have to talk about the code you have from two days ago.

var enemy1 = this.findNearestEnemy();
var enemy2 = this.findNearestEnemy();

These happen at essentially the same time . . . If you pause the playback and roll it back to the beginning and ask yourself, “Which is the closest” and then ask again . . . did anything change? no.

You need to kill enemy1 then set enemy2

var enemy1 = this.findNearestEnemy();
// attack & kill enemy1
var enemy2 = this.findNearestEnemy();
// attack & kill enemy2
// do what ever else needs to be doneto finish the level.

I’m sorry if I am not getting it. The code looks exactly the same. When I am told to kill the first and then find the second, I don’t remember any portion of that being in any of the other lessons.

There is something and I understand what I need to do, but I don’t see where I can learn how to do this.

How is it I am supposed to search for the second? What lesson was that taught in? Our code looks the same, what am I missing?

var enemy1 = this.findNearestEnemy();
var enemy2 = this.findNearestEnemy();

this.attack(enemy1);
this.attack(enemy1);
this.attack(enemy2);
this.attack(enemy2);

How are these the same?

var enemy1 = this.findNearestEnemy();
this.attack(enemy1);
this.attack(enemy1);

var enemy2 = this.findNearestEnemy();
this.attack(enemy2);
this.attack(enemy2);

“Computers are dumb . . . they only know what you tell them.”

They both say find NEAREST enemy. In yours there is no reason for it to pick different enemies: “enemy2 = findNearestEnemy()” doesn’t say: “findSecondNearestEnemy” (there is, by the way, no such a function, someday you could write one if you want.) :smile:
(In mine it picks different enemies 'cause the first guy be DEAD!)

What if we called the variables “Bob” and “Robert”. Bob is a nickname for Robert so, can you see them being the same guy, but they also don’t have to be the same?? enemy1 and enemy2 are just names somebody picked to make it so the programmer (you) can keep track of what is going on. They only mean what you tell them to mean. They don’t change until you tell them to mean something else. For example, during the fight the second guy (the one who was farther away) might get closer to you than the first guy (who was closer), but enemy1 still means the same guy it was before… If you were to run “findNearestEnemy()” at that point it would now tell you the other guy is nearer. If you wanted you could use one name “enemy” (the program doesn’t care) and in later levels you will. (You will learn ways to make your code shorter and more generic.)

Speaking of generic, in the earlier levels you attacked the ogres by their name (tom, sally, harry, whatever) and you “Hard coded” the names into your program . . . what if somebody changed the level such that there were differently named ogres (the night shift) guarding the exit? OH!NO! :frowning: , but this new program doesn’t care about the names. it just happily attacks the closest guy, then looks to see who else “wants some of the hurt you are dishing out.” Lowly Kithmen is (I believe) the second level where you “don’t know the names of your enemies” (so much for kicking butt and taking names…) I believe the first was “Master of Names”. You are already on your way to making your programs run no matter what gets thrown at them.

So if you want you can go back to “Master of names” and see how that one worked. (I think that is one where the second or third or ?? enemy is hiding down the hallway.) Some levels are almost the same as the previous level. In the “first” you are given a lot of the code, in the “second” you have to pull it out of your memory and/or make little changes to it.

1 Like

Thank you for the response. It has helped me better understand what is possibly going on.

Right now what I am struggling with is understanding why my new updated code is not working along with trying to take the advice that you and Nick provided. I really want to first understand how they designers intended for me to beat this level and this is where I am missing the point.

My current code that continues to not work:

var enemy1 = this.findNearestEnemy();
this.attack(enemy1);
this.attack(enemy1);

var enemy2 = this.findNearestEnemy();
this.attack(enemy2);
this.attack(enemy2);

I’m not trying to be difficult, I am just having a difficult time understand what the correct way to do it, when I feel it should behave or I missed something in the past.

This level says for me to create a second variable and attack it. I have done so. Not to mention, I am not even sure if the variable makes a difference on where in the stack it resides. Previously, I was under the assumption that the variables being next to each other on top was not a problem, since it was just a variable.

Now I think I have to move it to a different part of the stack since the variables are being assigned strangely. What level/tutorial is that included in? The mystery for me is I want to learn, I really do. And in my confusion I have to ask these questions to try to get some clarity. Although everyone else understands it, what I need to to understand what I am doing wrong.

So as of now, with my updated code the way it is. My character kills the first mob, runs away and doesn’t do anything else. In other runs I even tried moving him around several times before looking for the second mob. The results are the same.

Any suggestions?

I resolved the problem. The steps were nothing that I haven’t tried before, but this combo worked. Previously I attempted to give my character some space and time to use the second variable that resulted in the same problem, but this combo worked. I think the tips for this level is lacking the understanding that the user needs to provide more time or space so the second variable will recognize the second mob, even if they are actually closer.

var enemy1 = this.findNearestEnemy();
this.attack(enemy1);
this.attack(enemy1);
this.moveDown();
this.moveRight();

var enemy2 = this.findNearestEnemy();
this.attack(enemy2);
this.attack(enemy2);
this.moveRight();

That’s really weird; I just tried again the intended solution (what you had in the previous post), and it still works for me. Is anyone else able to reproduce the problem with that code?

Same level, JavaScript fork. I have the Fine Wooden Glasses equipped rather than the level rewarded Crude Glasses.

Using the example given in the Fine Wooden Glasses tooltip, I produce the solution:

loop {
var enemy = this.findNearestEnemy();
if (enemy) {
this.attack(enemy);
    } else {
var item = this.findNearestItem();
if (item) {
    var pos = item.pos;
    var x = pos.x;
    var y = pos.y;
    this.moveXY(x, y);   
    }
}
}

This solution gets a returned error message of “this.moveXY(x, y); is not a function”. I’ve had instructors look over what I wrote, and it is correct, it should work, but instead I have to use the following solution to finish the level:

loop {
var enemy = this.findNearestEnemy();
if (enemy) {
this.attack(enemy);
    } else {
    this.moveDown();
    this.moveRight(2);    
    }
}

This script works for me PYTHON
enemy1 = hero.findNearestEnemy()

hero.throw(enemy1)
hero.attack(enemy1)
hero.moveRight()
hero.moveDown()

hero.moveRight(3)