Munchkin Swarm code problems

Hello I am new to coding and am trying to learn some basic code through CodeCombat and I got stuck on Munchkin Swarm, I cant quite figure out how to get past it

enemy = self.findNearestEnemy()
loop:
    if self.isReady("cleave"):
        self.cleave(enemy)
    else:
        self.attack("Chest")

Here is my code, if anyone could point out any problems with it or if I forgot something that would be fantastic.

u prob forgot cleave maybe.

no cleave is in there, and also I forgot to mention but my character goes out and cleaves a munchkin then comes back beats on the chest for a few seconds then goes back to the dead body and cleaves again, and right after that they swarm and I cant cleave. So yeah that might be a big problem.

instead of else try
if enemy:

Looks like you’re telling it to cleave whenever it can regardless if there’s a good target.

Since you put “enemy =…” outside the loop, it never gets a new target.

jwrobbs is correct, you need to be looking for new enemies every time through the loop, not just once at the beginning. In your code enemy always refers to the first enemy you see regardless of whether he is alive or not.

1 Like

how am i supposed to win if I dont have enough armor and now i have too less gems to buy more? even when i cleave, they kill me to fast. I have less health so i can never win it.

How much health do you have? We designed this one so you wouldn’t have to buy extra armor, so let me know how much health and what gear you have, because I might need to tune it a bit more.

try doing something like this:

loop:
       enemy = self.findNearestEnemy()
       if enemy:
              if self.isReady("cleave"):
                     self.cleave(enemy)
              else:
                     self.attack(enemy)
       else:
              self.attack("Chest")

that didnt change much it just made it so you just have more targets

I have 193 HP and my character just goes to cleave the nothingness after first cleave

1 Like

So what code do you have now?

1 Like
loop:
    enemy = self.findNearestEnemy()
    if self.isReady("cleave"):
        self.cleave(enemy)
    else: self.attack("Chest")
1 Like

Check the distance to the enemy to make sure it’s less than 10, not if cleave is ready.

2 Likes

how do I do the less than 10 code because whenever I try to it gives me an error like “parenthesis dont match” or something

1 Like

Play the previous level–you should have had to do the distance check in there. You can use similar code.

You can also hover over the distanceTo in the spell palette to get a sort of example.

If you’re getting a parenthesis-mismatch error, you probably need to look at all your parentheses carefully to make sure they line up.

2 Likes
loop:
    enemy = self.findNearestEnemy()
    if enemy:
        if self.isReady("cleave"):
            self.cleave(enemy)
        else:
            self.attack(enemy)
    else:
        self.attack("Chest")

This is what I did for this level, I think I passed it.

1 Like

I can’t use that self.isReady ,self.findNearestEnemy()

1 Like

What equipment do you have on? those come with different glasses and wristbands. You should have earned them as you progressed to the level. Did they become unequipped?

1 Like

loop {
this.attack(“Chest”);
var a1=this.findNearestEnemy();

if (this.isReady(“cleave”)&&this.distanceTo(a1)<8) {
this.cleave(a1);

}
}

1 Like