Mayhem of Munchkins help

I’m trying to beat a Mayhem of Munchkins on LUA, but every time I put in loop it says that it’s wrong. Here is my code.

loop
    local enemy1 self:findNearestEnemy
    self:attack(enemy1)
    self:attack(enemy1)
end

Help please!

Have you defined enemy1?

Try to check the existence of the enemy before attacking. When if it is not there, the code will try to attack a non-existent term, thus the wrong statement. Not sure about LUA syntax, in python it will look like this, let me know if you can/cannot work out the logic:

loop:
    enemies = self.findEnemies()
    enemy1 = self.findNearest(enemies)
    if enemy1:
        self.attack(enemy)

Also, no need to attack twice, it is possible to attack till the enemy is alive by adding a while loop checking health of enemy like as follows:

loop:
    enemies = self.findEnemies()
    enemy1 = self.findNearest(enemies)
    if enemy1:
        while enemy.health > 0:
            self.attack(enemy)

Also in future, put up code only as per FAQ instructions (radiant, harmonious formatting). This makes everyreader’s reading easier… need for bigger codes.

Hello, Bem, and welcome. Please read the FAQ prior to you next post, so that you learn how to format your code properly. I’ve done it for you this time, but read it so you can do so yourself in the future.

Here are your problems:
On line 2:

local enemy1 self:findNearestEnemy

You need to assign enemy1 to self:findNearestEnemy(), that is, using an equal sign, like this:

local enemy1 = self:findNearestEnemy()

You also need a pair of parentheses after findNearestEnemy. It is a method, after all.


Lastly, you need to add an if-statement checking if your enemy exists, like this:

loop
    local enemy1 = self:findNearestEnemy()
    if enemy1 then
        -- Do stuff.
    end
end
1 Like