Help! Grim Determination

I’m stuck on grim determination with no obvious problem in my code - I just can’t seem to make the paladins survive and to protect Reynaldo! My paladins aren’t able to hold off the attackers. Should I change their code, the griffins’, the peasants, or what?
Thank you!

# 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!
    if paladin.canCast("heal"):
        lowestFriend = lowestHealthPaladin()
        if lowestFriend:
            hero.command(paladin, "cast", "heal", lowestFriend)
    enemies = hero.findEnemies()
    for enemy in enemies:
        if enemy:
            hero.command(paladin, "attack", enemy)
        else:
            hero.command(paladin, "shield")
    pass

def commandPeasant(peasant):
    peasantry = hero.findByType("peasant")
    peasant1 = peasantry[0]
    peasant2 = peasantry[1]
    items = hero.findItems()
    item1 = peasant1.findNearest(items)
    item2 = peasant2.findNearest(items)
    hero.command(peasant1, "move", {'x':item1.pos.x, 'y':item1.pos.y})
    hero.command(peasant2, "move", {'x':item2.pos.x, 'y':item2.pos.y})

def commandGriffin(griffin):
    enemies = hero.findEnemies()
    friends = hero.findFriends()
    enemis = hero.findNearestEnemy()
    for enemy in enemies:
        hero.command(griffin, "attack", enemy)
    if not enemis:
        for friend in friends:
            if friend.type == "paladin":
                hero.command(griffin, "move", {'x':friend.pos.x, 'y':friend.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)

def summonGriffins():
    if hero.gold > hero.costOf("griffin-rider"):
        hero.summon("griffin-rider")

def onSpawn():
    while True:
        items = hero.findItems()
        peasant = pet.findNearestByType("peasant")
        for item in items:
            pet.moveXY(item.pos.x, item.pos.y)
            pet.moveXY(peasant.pos.x, peasant.pos.y)

pet.on("spawn", onSpawn)

while True:
    commandFriends()
    summonGriffins()
    # Summon griffin riders!
    

Here try to find out paladin’s nearest enemy and if it exists and its type is not “warlock”, then command to attack the enemy, else command to shield.

Here try to find griffin’s nearest enemy and if it exists and its type is not “warlock”, then command to attack.

Andrei

Thanks, that helped, but my troops still can’t win. Is there anything else that I could do?

# 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!
    if paladin.canCast("heal"):
        lowestFriend = lowestHealthPaladin()
        if lowestFriend:
            hero.command(paladin, "cast", "heal", lowestFriend)
    enemies = hero.findEnemies()
    enemy = paladin.findNearest(enemies)
    if enemy and enemy.type != "warlock":
        hero.command(paladin, "attack", enemy)
    else:
        hero.command(paladin, "shield")
    pass

def commandPeasant(peasant):
    peasantry = hero.findByType("peasant")
    peasant1 = peasantry[0]
    peasant2 = peasantry[1]
    items = hero.findItems()
    item1 = peasant1.findNearest(items)
    item2 = peasant2.findNearest(items)
    hero.command(peasant1, "move", {'x':item1.pos.x, 'y':item1.pos.y})
    hero.command(peasant2, "move", {'x':item2.pos.x, 'y':item2.pos.y})

def commandGriffin(griffin):
    enemies = hero.findEnemies()
    enemy = griffin.findNearest(enemies)
    if enemy and enemy.type != "warlock":
        hero.command(griffin, "attack", enemy)
    else:
        hero.command(griffin, "move", {'x':65, 'y':40})

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)

def summonGriffins():
    if hero.gold > hero.costOf("griffin-rider"):
        hero.summon("griffin-rider")

def onSpawn():
    while True:
        items = hero.findItems()
        peasant = pet.findNearestByType("peasant")
        for item in items:
            pet.moveXY(item.pos.x, item.pos.y)
            pet.moveXY(peasant.pos.x, peasant.pos.y)

pet.on("spawn", onSpawn)

while True:
    commandFriends()
    summonGriffins()
    # Summon griffin riders!

What should I do @AnSeDra?

I can offer two suggestions. First: In your commandPaladin function, the order in which you present your commands can make a big difference. In your version, your have two if statements stacked, with the final one being an if/else statement. Instead, try using a single if/elif/else statement. This will allow for one and only one action to be performed, rather than two. The basic structure would look something like this:

if pally can cast, then
    heal lowest
otherwise if pally health is less than 200, then
    shield
otherwise
    attack

Second: In your commandPeasant function, you are kind of redefining your peasant. Why? You are already passing ‘peasant’ to the function, so you already have your object. Also, you are attempting to control both peasants, designating different items. There is no need for this. Since you are passing ‘peasant’ to the function, only that specific object will be addressed. The next time the function is called, it will be a new object, thereby the commands will apply only to it.

3 Likes

Thank you, that helped a lot! Also, do you have any suggestions for how to get the bonus objectives?

Nevermind, when I submitted it I got lucky and no paladins died.

1 Like