Grim determination HELP anyone

here is the link of the level
it says “fix youre code ‘try hero.distanceTo()’”
here is my code

    if target.distance < paladin.distance:

my full code

# Your goal is to protect Reynaldo

# Find the paladin with the lowest health.
def lowestHealthPaladin():
    lowestHealth = 99999
    lowestFriend = None
    friends = hero.findFriends()
    for friend in friends:
        if friend.type != "paladin":
            continue
        if friend.health < lowestHealth and friend.health < friend.maxHealth:
            lowestHealth = friend.health
            lowestFriend = friend

    return lowestFriend

def commandPaladin(paladin):
    target = hero.findNearestEnemy()
    # Heal the paladin with the lowest health using lowestHealthPaladin()
    # You can use paladin.canCast("heal") and command(paladin, "cast", "heal", target)
    # Paladins can also shield: command(paladin, "shield")
    # And don't forget, they can attack, too!
    lowestHealthPaladin()
    if paladin.canCast("heal"):
        if target:
            hero.command(paladin, "cast", "heal", target)
    if paladin:
        hero.command(paladin, "shield", target)
        hero.command(paladin, "attack", target)
    pass

def commandGriffin(griffin):
    firend = hero.findFriends()
    if friend.type == "griffin-rider":
        enemy = griffin.findEnemies()
        if enemy:
            hero.command(friend, "attack", enemy)

def commandPeasant(peasant):
    friend = hero.findFriends()
    if friend.type == "peasant":
        coin = hero.findNearestItem()
        if coin:
            hero.command(friend, "move", {'x': coin.pos.x, 'y': coin.pos.y})

def commandFriends():
    # Command your friends.
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == "peasant":
            commandPeasant(friend)
            pass
        elif friend.type == "griffin-rider":
            commandGriffin(friend)
            pass
        elif friend.type == "paladin":
            commandPaladin(friend)

while True:
    commandFriends()
    # Summon griffin riders!
    hero.summon("griffin-rider")

Here check your spelling.

You can delete this.

Why are you using the variabile friend when you need to use the variabile peasant?

Here what happens if you do not have enough gold to summon a griffin rider?

Here don’t you want to find the paladin’s nearest target?

Here don’t you want to put what that function returns into a variable (like lowest)?

Here why do you heal the enemy instead of the lowest paladin?

The shield command doesn’t require any parameters, so you can delete the , target part from here.

Andrei

3 Likes

can i command to build a fence if yes, how?

Only you can build a fence, peasants can not do that.

Andrei

2 Likes

And this structure will never ever work as intended :

    if paladin.canCast("heal"):
        if target:
            hero.command(paladin, "cast", "heal", target)
    if paladin:
        hero.command(paladin, "shield", target)
        hero.command(paladin, "attack", target) 

see other topics about “Grim determination” explaining that problem.

2 Likes