Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Munchkin Swarm code not working

Here is my code in Javascript

while(true) {

    var nearestEnemy = hero.findNearestEnemy();
    var distance = hero.distanceTo(nearestEnemy);
    var Enemy = hero.findNearestEnemy();
    if(distance < 10) {
        var Enemy = hero.findNearestEnemy();
    hero.cleave(Enemy);
    }
    else {
        hero.attack("Chest");
    }

     
}

Edit: My character right now for this level.
Capture

Hello, Welcome to the forum. Please explain what problems you are having in more detail.

While there are a few things that can clean up, your code does run and complete the level when I run it even with the basic armor. So I’ll need more details on what you are seeing.

Below are some cleanup suggestions for your code.

Since you already have the var nearestEnemy = hero.findNearestEnemy(); you don’t need to have the other two lines that find the same value var Enemy = hero.findNearestEnemy();. Also, since your distance is based on the nearestEnemy value, you don’t want to grab another value (Enemy) just before you attack. The ‘if’ check ensures the conditions meet the criteria to perform the next task. By grabbing another value after the check, the ‘if’ check becomes pointless.

you will have to get some armor to last long enough to break the chest open but other than the extra code brooksy was talking about this should get you through

@brooksy125 I tried what you said but this happened.

while(true) {

    var nearestEnemy = hero.findNearestEnemy();
    var distance = hero.distanceTo(nearestEnemy);
    if(distance < 10) {
    hero.cleave(Enemy);
    }
    else {
        hero.attack("Chest");
    }

     
}

@mondays I also tried what you said but it really didn’t help. Thanks for trying…

Capture3

Take a closer look at who you are cleaving. You don’t have an Enemy variable to attack, but you do have a nearestEnemy.

What you are supposed to do is find the nearest enemy because there is no specific enemy to cleave. I think I just need a better weapon.

This is the only weapon that cleaves, the weapon isn’t the problem. Currently your code commands your hero to cleave an enemy that you have not identified.

while(true) {
    var nearestEnemy = hero.findNearestEnemy(); // variable that finds nearestEnemy
    var distance = hero.distanceTo(nearestEnemy);  // variable to get distance to nearestEnemy
    if(distance < 10) {  //checked distance to nearestEnemy
    hero.cleave(Enemy); //but attacks Enemy which doesn't exist.  use the variable that is defined
    }
    else {
        hero.attack("Chest");
    }
}

@TopGaming11 are you still experiencing this issue?