Munchkin Swarm code problems


#1

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.


#2

u prob forgot cleave maybe.


#3

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.


#4

instead of else try
if enemy:


#5

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.


#6

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.


#7

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.


#8

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.


#9

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")

#10

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


#11

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


#12

So what code do you have now?


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

#14

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


#15

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


#16

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.


#17
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.


#19

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


#20

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?


#21

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

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

}
}