Help on Grim Determination


#1

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

#2

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")

#3

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


#4

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)

#5

Now it says that index is not defined.


#6

did you add index as a parameter??


#7

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


#8

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


#9

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


#10

or copy the code and paste it here


#11

Yeah, here it is. I have a mac

01 PM


#12

that is one of the parets you told me to correct


#13

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


#14

here is the next part

49 PM


#15

It also says peasantIndex is not defined.


#16

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

24 PM


#17

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.


#18
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


#19

@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)

#20

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