[SOLVED] Village Champion issues

I’m not sure what I’m doing wrong. I don’t understand where the error is at. I thought that I defined a function cleaveOrAttack there at the beginning of my code. Perhaps I don’t understand the terminology? Please help

# Incoming munchkins! Defend the town!

# Define your own function to fight the enemy!
cleaveOrAttack = (enemy) =>
    enemy = @findNearestEnemy()
    if enemy
      if @isReady 'cleave'
        @cleave enemy
      else
        @attack enemy
        
# Move between patrol points and call the function.
while true
  @moveXY 35, 34
    # Use cleaveOrAttack function you defined above.
  cleaveOrAttack()

  @moveXY 47, 27
    # Call the function again.
  cleaveOrAttack()
 
  @moveXY 60, 31
    # Call the function again.
  cleaveOrAttack()
    

I figured it out. I was passing info into the function that I wrote (enemy). After removing enemy out of it, it works and passes.

Incoming munchkins! Defend the town!

Define your own function to fight the enemy!

def cleaveOrAttack():
# In the function, find an enemy, then cleave or attack it.
target = hero.findNearestEnemy()
if enemy:
if hero.isReady(“cleave”):
hero.cleave(target)
else:
hero.attack(enemy)

Move between patrol points and call the function.

while True:
hero.moveXY(35, 34)
# Use cleaveOrAttack function you defined above.
target = hero.findNearestEnemy()
if target:
hero.cleaveOrAttack(target)
hero.moveXY(47, 27)
# Call the function again.
target= hero.findNearestEnemy()
if target:
hero.cleaveOrAttack(target)
hero.moveXY(60, 31)
# Call the function again.
target= hero.findNearestEnemy()
if target:
hero.cleaveOrAttack(enemy)

That’s the code I have used and its not working, Ive probably missed something really basic or Ive just done it completely wrong…any advice would be appreciated.

Please learn to post your code properly. It just requires a small amount of effort.

To post your code from the game, use the </> button or it won’t format properly. When you click the </> button, the following will appear:

Please paste ALL of your code inside the triple back tick marks.

``` <— Triple back tick marks.

Paste ALL of your code in here.

``` <— Triple back tick marks.

There are many people here willing and able to help. If you use the </> button correctly, then ALL of your code should look like this:

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.attack(enemy)
    else:
        hero.say("My code is formatted properly")

If ALL of your code isn’t formatted like the code above, then you’re doing it wrong and we can’t see the structure of the code to troubleshoot whether or not that is the issue. Use the </> button and help us help you. This works when sending a private message as well.

Thank you.

    # In the function, find an enemy, then cleave or attack it.
    target = hero.findNearestEnemy()
    if enemy:
        if hero.isReady("cleave"):
            hero.cleave(target)
    else:
        hero.attack(enemy)

# Move between patrol points and call the function.
while True:
    hero.moveXY(35, 34)
    # Use cleaveOrAttack function you defined above.
    target = hero.findNearestEnemy()
    if target:
        hero.cleaveOrAttack(target)
    hero.moveXY(47, 27)
    # Call the function again.
    target= hero.findNearestEnemy()
    if target:
        hero.cleaveOrAttack(target)
    hero.moveXY(60, 31)
    # Call the function again.
    target= hero.findNearestEnemy()
    if target:
        hero.cleaveOrAttack(enemy) 

type or paste code here

I hope thats right now :slight_smile:

Much better.

Half your code uses the variable “target” and the other half uses an undefined variable “enemy”. Pick one and stick with it.

Also, when you call your function, you don’t have to put hero. in front of it.

You already have an if enemy conditional in the function, so you don’t have to use one in your while True loop. Just move to the xy coordinates and call the function.

1 Like

Ill try that however it says in the guide that Cleave changes enemy to target, is that correct???so which is the correct to use i mean will it make a difference …and that is why I have done half that way where cleave is used I have used Target…Thank you.

No I think my code is a complete mess…somewhere along the lines I have overthought it and confused myself…Ive tried constantly over the last few days with a headache to fix it I may just scrap it all and start from fresh. What I do find misleading in game I could be wrong as Ive been struggling to think straight over the last few days but an arrow sends you to village champion first which i struggled with for hours and when I looked in advice it mentions doing Village Warder first. Probably my brain it confused me anyhow skipping to a more complicated part without understand the previous if that makes sense.

# Incoming munchkins! Defend the town!

# Define your own function to fight the enemy!

def cleaveOrAttack():
    # In the function, find an enemy, then cleave or attack it.
    enemy = hero.findNearestEnemy()
    if enemy:
        if hero.isReady("cleave"):
            hero.cleave(enemy)
    else:
        if enemy:
            hero.attack(enemy)
    pass
# Move between patrol points and call the function.
while True:
    hero.moveXY(35, 34)
    # Use cleaveOrAttack function you defined above.
    enemy= hero.findNearestEnemy()
    if enemy:
            cleaveOrAttack(enemy)
            
    hero.moveXY(47, 27)
    # Call the function again.
    enemy = hero.findNearestEnemy()
    if enemy:
            cleaveOrAttack(enemy) 

    hero.moveXY(60, 31)
    # Call the function again.
    if enemy:
        cleaveOrAttack(enemy)
type or paste code here

I put this and its still doesn’t work.
back to scratch I guess then.

In your function, the else statement doesn’t need an if enemy conditional statement after it. Read my comments in your code below.

def cleaveOrAttack():
    # In the function, find an enemy, then cleave or attack it.
    enemy = hero.findNearestEnemy()
    if enemy:
        if hero.isReady("cleave"):
            hero.cleave(enemy)
    else:  # this conditional statement should line up with the if hero.isReady("cleave")
        if enemy: # eliminate this line
            hero.attack(enemy)
    pass

Think of it like this,

if enemy:
    if hero is ready:
        do this
    else: # if hero is NOT ready
        do that

In your while True loop, just moveXY and call the function. You don’t need to redefine the enemy variable or check for an enemy again. This is all taken care of in your function.

hero.moveXY(x, y)
cleaveOrAttack()
Ok so tried this as you mention. However I still get an Error msg saying "There is no enemy. Use Enemy = hero.findNearestEnemy()" which was why I started redefining the enemy variable int he first place.It does look like I was overcomplicating things but even simplified I get the error msg.

def cleaveOrAttack():
    # In the function, find an enemy, then cleave or attack it.
    enemy = hero.findNearestEnemy()
    if enemy:
        if hero.isReady("cleave"):
            hero.cleave(enemy)
    else:
        hero.attack(enemy)
    pass
# Move between patrol points and call the function.
while True:
    hero.moveXY(35, 34)
    # Use cleaveOrAttack function you defined above.
    cleaveOrAttack(enemy)
            
    hero.moveXY(47, 27)
    # Call the function again.
    cleaveOrAttack(enemy) 
            
    hero.moveXY(60,31)
    # Call the function again.
    cleaveOrAttack(enemy) 

type or paste code here

My sincerest apologies if I am being very stupid here…it looks like this should be really simple as you have shown but its not working…Ive got a nasty headache and 2 year old constantly screaming for attention …try my best a the moment…the closest I got was Keeping the redefined variables in but even then 1 peasant keeps dying so I must be doing something wrong.

The structure of your function is still incorrect. Review the above corrections. Also, there should not be arguments in the parenthesis when you call your function. You should call the function just like it appears in the definition.

[quote=“MunkeyShynes, post:12, topic:13952”]
Ok so going back to your comments and checking I’ve done them.

1)Half your code uses the variable “target” and the other half uses an undefined variable “enemy”. Pick one and stick with it. Done!

  1. “Also, when you call your function, you don’t have to put hero”. in front of it.Done removed!

3)“You already have an if enemy conditional in the function, so you don’t have to use one in your while True loop. Just move to the xy coordinates and call the function”. Ok removed!

  1. “In your function the else statement doesn’t need an if enemy conditional statement after it”. Removed Done.!

5)“Read my comments in your code below.” Done!

6)“In your while true loop just move XY.” (I don’t think I have the coordinates wrong do I?)

  1. “And call the function. You don’t need to redefine the enemy variable or check for an enemy again.” This is all taken care of in your function. Done I believe!

[quote=“MunkeyShynes, post:15, topic:13952”]
Also, there should not be arguments in the parenthesis when you call your function. You should call the function just like it appears in the definition.
[/quote] Done too I think

so this is what I have now and I get this error message`def cleaveOrAttack():
# In the function, find an enemy, then cleave or attack it.
def cleaveOr Attack():
enemy = hero.findNearestEnemy()
if enemy:
if hero.isReady(“cleave”):
hero.cleave(enemy)
else:
hero.attack(enemy)

Move between patrol points and call the function.

while True:
hero.moveXY(37, 34)
# Use cleaveOrAttack function you defined above.
cleaveOrAttack()

hero.moveXY(47, 30)
# Call the function again.
cleaveOrAttack() 
        
hero.moveXY(58,31)
# Call the function again.
cleaveOrAttack()`

Error Message highlights hero.attack(enemy) in red and Reads: Fix Your Code. Line 12: attack’s argument target should have type object, but got null. Target is null. Is there always a Target to attack? (Use if?)

My apologies for my stupidy and thank you for your patience.

The problem is in your function but I can’t see the structure because you didn’t post your code correctly.

Also, it looks like there is a white space in the name of the function that wasn’t there before. There shouldn’t be any spaces in the name of the function.

My aplogies I posted it incorrectly, here it is in the correct format. This is the latest with attempts to debug utilising the say command.
the first two hero.say correctly show the defined enemy variable, however after the else statement, enemy is null. Can you explain why the enemy is being set to null after the else statement.

def cleaveOrAttack():
    # In the function, find an enemy, then cleave or attack it.
    enemy = hero.findNearestEnemy()
    if enemy:
        hero.say(enemy)
        if hero.isReady("cleave"):
            hero.cleave(enemy)
            hero.say(enemy)
    else:
        hero.say(enemy)
        hero.attack(enemy)

# Move between patrol points and call the function.
while True:
    hero.moveXY(35, 34)
    # Use cleaveOrAttack function you defined above.
    cleaveOrAttack()
            
    hero.moveXY(47, 27)
    # Call the function again.
    cleaveOrAttack() 
            
    hero.moveXY(60,31)
    # Call the function again.
    cleaveOrAttack()

    hero.moveXY(61,30)
    # Call the function again.
    cleaveOrAttack()

Look closely at the structure of your function. The entire problem is there. As I pointed out before (see above example), the else conditional is not aligned properly. That’s why it’s failing.