Level: Leave it to Cleaver

here is my code it is in javascript

mod edit do not post final solutions

Never mind, I got it to work.

def cleaveWhenClose(target):
if hero.distanceTo(target) < 5:
#cleave command
else:
#attack command
while True:
target = hero.findNearestEnemy()
if target:
if hero.distanceTo(target) < 5:
if hero.isReady (“cleave”):
#cleave command
else:
#attack command

This isn’t working. I tried it and the munchkins beat me. The character didn’t respond, it only stood there.

// 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);
}
}

Hi @YT_DropTheBeat, welcome to the CodeCombat Discourse! :tada:
Please could you format your code so I can see the indents and help you with your code, here’s a topic which explains how to format your code:

@Hexamix_Legend, please could you also format your code.
Thanks
Danny

Hi! Sorry for the late response. I actually managed to finish the level, but thanks for the offer!

1 Like

When I get to the end of the level the time doesn’t run out and I don’t die, it just says that I didn’t defeat the ogres.

Hello and welcome to codecombat discourse! :partying_face: This is a cozy forum where you can share ideas, share fan art, get assistance for code, etc! Before you proceed, we hope that you review this topic, which shows all essentials of this board! Thanks!

Can you please send us your code formated as it is described below so we will be able to help you solve the level and show us the equipment that you are using?

Andrei

Welcome, welcome to the forum! It is a lovely place where you can share lots of things, bugs, or request help on levels! :partying_face: :partying_face:
Can you post your code based on Andrei’s responce?
Lydia

1 Like

Btw if you guys need the code to beat this here

def cleaveWhenClose(target):
if hero.distanceTo(target) < 5:
pass
ready = hero.isReady(“cleave”)
hero.cleave(enemy)
else:
hero.attack(enemy)

while True:
enemy = hero.findNearestEnemy()
if enemy:
cleaveWhenClose(enemy)

ok so when i tried the code i put in it started to get worse and worse

Please delete your code if it runs and works. We try to encourage people to write their own code in this forum. If you give someone a code, they won’t learn at all.
Lydia

So do you need help or no

Thank you It took a long time to do this level.

hello, and please don’t revive “dead topics”, unless if it is relavent.

Could someone delete that correct code like @Chaboi_3000

Welcome to the forum! :partying_face:
Please review this topic before posting :slight_smile:

Also, please do not enter a reply after this topic hasn’t been used for 3+ months, unless you’re asking for help on Leave it to the Cleaver. It bumps the topic to the very top of the discourse front page unnecessarily. Sorry if we sound harsh, we just want to make sure everybody has the best possible experience on the CoCo Discourse. :frowning:

Thanks! :smile:
Rachel

1 Like

I know the code is correct its just wont kill it.
im in forest