Help on Grim Determination

I need help on the level Grim Determination. For some reason, my peasants are going for the same coin, sometimes hesitating mid-way there, and turning around, not even getting the original coin. I tried


coin.pos.x,coin.pos.y

but it didn’t work so I did coin.pos. Why is this not working?

Here is my 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):
    enemy = hero.findNearest(hero.findEnemies())
    # Heal the paladin with the lowest health using lowestHealthPaladin()
    lowestFriend = lowestHealthPaladin()
    if lowestFriend and paladin.canCast("heal"):
        hero.command(paladin, "cast", "heal", lowestFriend)
    # 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 enemy:
        hero.command(paladin, "attack", enemy)
    if paladin.health < 200:
        hero.command(paladin, "shield")

def commandPeasant(friend):
    coins = hero.findItems()
    for coin in coins:
        if coin and coin.value > 1:
            hero.command(friend, "move", coin.pos)

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

def commandFriends():
    # Command your friends.
    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()
    # Summon griffin riders!
    if hero.gold >= hero.costOf("griffin-rider"):
        hero.summon("griffin-rider")

you are commanding each peasant to get the same coin in the same order.

try to do

def commandPeasant(friend,index):
    coins = hero.findItems()
    coin = coins[index]
    hero.command(friend, "move", coin.pos)

and

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

Now it says that index is not defined.

did you add index as a parameter??

it should be

def commandPeasant(friend,index):
    coins = hero.findItems()
    coin = coins[index]
    hero.command(friend, "move", coin.pos)

not

def commandPeasant(friend):
    coins = hero.findItems()
    coin = coins[index]
    hero.command(friend, "move", coin.pos)

if you did it right, can you please post a screenshot
just click prntscrn button on the keyboard when you have the error and then click ctrl+v in the reply in here

yes, I did
:confused::confused::confused::confused::confused::confused::confused:

so, can you send a screenshot please,
the instructions are in my previous reply

or copy the code and paste it here

Yeah, here it is. I have a mac

01 PM

that is one of the parets you told me to correct

parts, sorry
:confused::confused::confused:

here is the next part

49 PM

It also says peasantIndex is not defined.

actually, just index. this is where it is saying it is an error

24 PM

it looks like you didn’t copy the code exactly, which produced some errors, can you please copy the code that i replied with and paste it in your code and remove the old parts.

def commandPeasant(friend,index):
    coins = hero.findItems()
    coin = coins[index]
    hero.command(friend, "move", coin.pos)


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

those are the parts that needs to be changed

@kyay10 i don’t understand what the index thing is for. Can’t you just tell the peasants to move to friend.findNearestItem()?
like instead of commandPeasant(friend,peasantIndex), just use commandPeasant(friend)
EX code:

def commandPeasant(friend):
    coin = friend.findNearestItem()
    hero.command(friend, "move", coin.pos)

yeah, but that may make them move to the same coin