[SOLVED] Need help with grim determination

summonTypes = ['griffin-rider']


def summonTroops():
    type = summonTypes[len(hero.built) % len(summonTypes)]
    if hero.gold > hero.costOf(type):
        hero.summon(type)



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):
    if (paladin.canCast("heal")):
        target = lowestHealthPaladin()
        if target:
            hero.command(paladin, "cast", "heal", target)
    elif (paladin.health < 200):
        hero.command(paladin, "shield")
    else:
        target = paladin.findNearestEnemy()
        if (target):
            hero.command(paladin, "attack", target)


def commandPeasant(peasant):
    item = peasant.findNearestItem()
    if item:
        hero.command(peasant, 'move', item.pos)


def commandGriffin(griffin):
    target = hero.findNearest(hero.findByType('warlock'))
    if not target:
        target = griffin.findNearestEnemy()
    if (target):
        hero.command(griffin, "attack", target)


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


while True:
    commandFriends()
    summonTroops()

when i run it, it says success, but when i submit it, my guy reynaldo gets one shotted by an arrow.

Bruh.

I would keep submitting it until you get a successful seed (it took me one try).
I’m still not sure whether it was friendly fire though…

1 Like

If you use Chrome, try to open this level in Mozilla or any other browser. And vice versa. It often helps get another seed quickly.

thank you, i will try

hmmm…lemme try that

i just resubmitted and it worked! Thank you guys!

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!
    paladin = hero.findNearest(hero.findByType("paladin"))
    weakFriend = lowestHealthPaladin()
    enemy = paladin.findNearestEnemy()
    if weakFriend:
        if paladin and paladin.canCast("heal", weakFriend):
            hero.command(paladin, "cast", "heal", weakFriend)
    elif enemy:
        hero.command(paladin, "attack", enemy)
    elif paladin and paladin.health < 200:
        hero.command(paladin, "shield", paladin)
    pass
def commandPeasant(peasant):
    peasant = hero.findNearest(hero.findByType("peasant"))
    coin = peasant.findNearestItem()
    pos = coin.pos
    if peasant and coin:
        hero.command(peasant, "move", pos)
def commandGriffin(griffin):
    griffin = hero.findNearest(hero.findByType("griffin-rider"))
    enemy = griffin.findNearestEnemy()
    bestEnemy = hero.findNearest(hero.findByType("warlock"))
    if griffin and bestEnemy:
        hero.command(griffin, "attack", bestEnemy)
    elif griffin and enemy:
        hero.command(griffin, "attack", enemy)
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()
    if hero.gold > hero.costOf("griffin-rider"):
        hero.summon("griffin-rider")
        # Summon griffin riders!

Here is my code… I can’t see anything wrong with it yet I cannot get it to work. Any suggestions?? :thinking:

What’s above:

and below:

? Please be sure to post all of your code.

However, in each of your command"unit" functions, you are re-defining unit. You are already passing the unit via the call of the function, for example “commandPeasant(friend)”, in the commandFriends function.

Try commenting/deleting these three lines out and see how it runs:

# commandPaladin
    paladin = hero.findNearest(hero.findByType("paladin"))
# commandPeasant
    peasant = hero.findNearest(hero.findByType("peasant"))
# commandGriffin
    griffin = hero.findNearest(hero.findByType("griffin-rider"))

The

hero.summon("griffin-rider")

is the end of my code

If that is the end of your code you’ll have to write some more as dedreus said.
Another problem is the fact that in each command function for each troop your redefining the variable your using to command e.g.

griffin = hero.findNearest(hero.findByType("griffin-rider"))

This means you’ll only ever command the nearest griffin. Why not use the variable that you got from your commandFriends function?
Danny

Thanks, I tried commenting out where I redefined my friends, but it still doesn’t work. My paladins refuse to attack after the first few enemies and get pushed into the mines.
What kind of code do I write at the end? The hero can’t move so he can’t help physically.

I found out that I can edit and I put the top bit on.

Look at this section:

The elif statement means: else & if. So that means that it will only attack the enemy (elif enemy:) when if weakFriend returns true, then, the elif enemy and the elif paladin and paladin.health < 200: will never run.
Danny
P.S. use if, or, on the first line:
if weakFriend and paladin.canCast(“heal”, weakFriend)

I tried that and lasted so much longer – thankyou! I guess now it is time for multiple submitting.

yay
thankyou so much!!!
it worked

Nice, I’m glad :grin:
Danny