Level: Leave it to Cleaver


#1

Hi, don’t know how to do Leave it to Cleaver Backwoods Forest level. Plz help.

PS, how to display code in this form? I can’t seem to do it with copying and pasting, espcially it does not work with indents.


#2

sorry here is my code just read the FAQ XD


# This shows how to define a function called cleaveWhenClose
# The function defines a parameter called target
def cleaveWhenClose(target):
    if hero.distanceTo(target) < 5:
        # Put your attack code here
        hero.attack(target)
        # If cleave is ready, then cleave target
        if hero.isReady("cleave"):
            hero.cleave(target)
        # else, just attack target!
        else: 
            hero.attack(target)

# This code is not part of the function.
while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        # Note that inside cleaveWhenClose, we refer to the enemy as target.
        cleaveWhenClose(enemy)

#3

in your def() statement, did you define what target was?
e.g:

target = hero.findNearestEnemy() 

Was that ALL of the code you posted? Could you post ALL of the code?


#4

Yes that was all my code


#5

What does target equal?


#6

isn’t target the enemy?


#7

It looks like you’re attacking before cleaving which is changing the timing of your attacks.


#8

Anyone wanna explain why this:

// This shows how to define a function called cleaveWhenClose
// The function defines a parameter called target
function cleaveWhenClose(target) {
    if(hero.distanceTo(target) < 5) {
        // Put your attack code here
        // If cleave is ready, then cleave target
        if (hero.isReady) {
            hero.cleave(enemy);
        }
        // else, just attack target!
        else {
            hero.attack(enemy);
        }
    }
}

// This code is not part of the function.
while(true) {
    var enemy = hero.findNearestEnemy();
    if(enemy) {
        // Note that inside cleaveWhenClose, we refer to the enemy as target.
        cleaveWhenClose(enemy);
    }
}

Doesn’t work?


#9

Please format your code using the </> button when posting.


#10

Your problem can be solved with the missing code cookies after def cleaveWhenClose(target). You also specified three attacks. One is unnecessary; the other two are proper.

If you can solve the riddle, Mr. Riddler would be happy to hop along your adventures!


#11

def cleaveWhenClose(target):
if hero.distanceTo(target) < 5:
pass
# Put your attack code here
# If cleave is ready, then cleave target
hero.cleave(enemy)
# else, just attack target!
else:
hero.attack(enemy)

This code is not part of the function.

while True:
enemy = hero.findNearestEnemy()
if enemy:
# Note that inside cleaveWhenClose, we refer to the enemy as target.
cleaveWhenClose(enemy)
?


#12

This shows how to define a function called cleaveWhenClose

The function defines a parameter called target

def cleaveWhenClose(target):
if hero.distanceTo(target) < 5:
pass
# Put your attack code here
# If cleave is ready, then cleave target
hero.cleave(enemy)
# else, just attack target!
else:
hero.attack(enemy)

This code is not part of the function.

while True:
enemy = hero.findNearestEnemy()
if enemy:
# Note that inside cleaveWhenClose, we refer to the enemy as target.
cleaveWhenClose(enemy)

This my code for leave it to the cleaver and it doesn’t work. The last two ogres aren’t getting attack. My family has tried it my brother and dad they both failed


#13

it’s very difficult to see the problem because your code isn’t properly formatted for this forum, so I can’t see the structure. The proper use of the triple backticks when posting code can be found here:

One glaringly obvious problem is that your function starts out referring to target in the arguments but then you switch and start referring to enemy in the arguments. Consistency is important. Enemy is not defined in the function, just the code. The code can’t read definitions in the function and the function can’t read definitions in the code.


#14

I don’t understand what you said and I learned by looking at my code when the last two ogres come out it tries to cleave them and it doesn’t use the else to attack instead. Please send more help. Sorry for all the trouble


#15

Until you learn to properly post your code here - from the game - NOT by copying and pasting it from this board, help is going to be limited because we can’t see the structure of your code. Did you even look at the link I posted explaining how to do that? You must use the </> button. Please read and follow these directions when posting code. There are many people here who are willing and able to help, but you must apply a little effort to enable them to do so.


#16

I finished the level sorry for getting back late


#17

ive been stuck on this level for weeks and nothing has come so i need help


#18

You’ve come to the right place for help, but unless you post your code we can’t see what’s wrong.


#19

change hero.isReady to hero.isReady(“cleave”)