[SOLVED] Grim Determination Help (python)

# 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!
    while True:
        enemy = hero.findNearestEnemy()
        lowest = lowestHealthPaladin()
        if paladin.canCast("heal"):
            hero.command(paladin, "cast", "heal", lowest)
            if enemy:
                hero.attack(enemy)
    pass

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

def commandPeasant(peasant):
    while True:
        item = hero.findNearestItem()
        if item and item.type == "coin":
            hero.command(peasant, "move", item.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")

The peasants don’t collect gold, the griffin riders don’t spawn, and the paladins do nothing. Help! The link is https://codecombat.com/play/level/grim-determination. Thanks!

At a quick glance, I notice that you have a While True: loop in the Command functions. Once you get to that, you don’t have a way to get out and will keep trying to command the Paladin to heal or the griffin to attack and nothing else. You either want to remove the While True loop in the functions (best method), or create a way to break out of it.

Hi CryptiCrystal,

  • Take out the ‘while True’ in the function definitions.

  • In the def commandFriends() you still have a # in front of the commandPeasant, so it will be ignored.

  • In the lowestHealthPaladin function you have two if statements. Make one of these an elif.

  • In the commandPaladin function make the 2 ifs into an if and an elif, and have them at the same level.

Try correcting those, repost your code if it still doesn’t work!

Jenny

ok! I will try that (20 chars)

Like, would I have to make while loops that aren’t while true loops, because I did fix it, but now it says my code never finished. Here it is

# 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
        elif 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!
    while paladin.health < paladin.maxhealth:
        enemy = hero.findNearestEnemy()
        lowest = lowestHealthPaladin()
        if paladin.canCast("heal") and lowest:
            hero.command(paladin, "cast", "heal", lowest)
        elif enemy:
            hero.attack(enemy)
    pass

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

def commandPeasant(peasant):
    while peasant.health > 0:
        item = peasant.findNearestItem()
        if item and item.type == "coin":
            hero.command(peasant, "move", item.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")

Oh wait, I’m not commanding the paladins to attack, I’m commanding myself. Whoops!

You don’t want to include while loops at all in the functions. The functions are designed to provide a command to the friend then go back to the main loop, until they are given a different command. You don’t need to continually command them within the function. The While True loop in the main line of code will ensure you keep looping through all your friends and command them as directed. So remove all the While loops in the functions, shift your indentation and see where your code gets you then. It should at least start commanding them, but you may need to tweak the commands to complete the level.

2 Likes

So that because I’m running the commands in a while loop, the commands don’t need a loop?

Correct, the functions with the commands for each group don’t need a loop.

The commandFriends() function isolates one friend at a time, then grabs the command for that friend from the specific function for the friend type. Once that friend knows what to do, it will continue to do that command until it is commanded differently when you go through the commandFriends() for loop again. A friend can only follow one command at at time and will do that until the command is changed.

Cool! I fixed the code and the indentation errors after removing the while loops, and I finished the level! Thanks so much. :grinning:

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.