Grim determination - weird codes

hi

I’ve done the codes and succeeded the level already after the execution, but when I “submit” it, my codes failed somehow, and when I run the codes again, its’ not working anymore, so weird.

# 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):
    # 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!
    weakestPaladin = lowestHealthPaladin()        
    enemy = paladin.findNearestEnemy()
    if enemy:
        distance = paladin.distanceTo(enemy)
        if distance < 20 and enemy.health > 0:
            hero.command(paladin, "attack", enemy)
        
    if paladin.health < paladin.maxHealth / 3:
        hero.command(paladin, "shield")
        
    if weakestPaladin:
        if paladin.canCast('heal'):
            hero.command(paladin, "cast", 'heal', weakestPaladin)
        pass


def commandGriffin(griffin):
    enemy = griffin.findNearestEnemy()
    if enemy:
        hero.command(griffin, "attack", enemy)



def commandPeasant(peasant):
    if hero.canCast("haste", peasant):
        hero.cast("haste", peasant)
    if hero.isReady("reset-cooldown"):
        hero.resetCooldown("haste")
        
    coin = peasant.findNearestItem()
    if coin:
        hero.command(peasant, "move", coin.pos)
    




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!
    if hero.gold > hero.costOf("griffin-rider"):
        hero.summon('griffin-rider')

when I ran the codes the second time, all the peasants, paladins, griffin riders stopped moving after around 15 seconds

Hmm, this code seems to be alright. But obviously something’s going wrong… Please could you post a screenshot of your equipment and the level screen.
Thanks
Danny

I’d suggest:

  • commandPaladin
    1. You currently have a stack of several if statements. I would recommend nesting them, using the if/elif/else method
    2. I’d also recommend heal first, shield second, attack last (not sure this matters, but I think healing first is key, as it help keep the paladin in the fight longer)
1 Like

yeah, sure


Well, on your screen you seem to be winning… Try what @dedreous said, maybe that will make it work every time, not just sometimes.
Danny

1 Like