Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Help on resource valleys [SOLVED]

# Collect all the coins!

def commandPeasant(peasant, coins):
    # Find the nearest coin to the `peasant` from the `coins` array,
    friends = hero.findFriends()
    friend = hero.findNearest(friends)
    if friend.type=="peasant":
        coin = friend.findNearestItem()
        for friend in friends:
            hero.command(friend, "move", {'x': coin.pos.x, 'y': coin.pos.y})
    pass

friends = hero.findFriends()
peasants = {
    "Aurum": friends[0],
    "Argentum": friends[1],
    "Cuprum": friends[2]
}

while True:
    items = hero.findItems()
    goldCoins = []
    silverCoins = []
    bronzeCoins = []
    for item in items:
        if item.value == 3:
            goldCoins.append(item)
        # Put bronze and silver coins in their approriate array:
        if item.value == 2:
            silverCoins.append(item)
        if item.value == 1:
            bronzeCoins.append(item)
    
    commandPeasant(peasants.Aurum, goldCoins)
    commandPeasant(peasants.Argentum, silverCoins)
    commandPeasant(peasants.Cuprum, bronzeCoins)

Please explain the problem @ducky

In commandPeasant(peasants.Aurum, goldCoins) you are telling {“Aurum”: friends[0]} to collect gold coins. So

def commandPeasant(peasant, coins):  # the peasant here is e.g. a single person - {"Aurum": friends[0]}
    # Find the nearest coin to the `peasant` from the `coins` array,
    friends = hero.findFriends() # why do you need to search again the peasants?
    friend = hero.findNearest(friends) # you already know the 3 collectors
    if friend.type=="peasant":  # and they all are peasants
        coin = friend.findNearestItem() # the peasant or some friend is searching for gold
        for friend in friends:   # do you need to iterate over friends?
            hero.command(friend, "move", {'x': coin.pos.x, 'y': coin.pos.y}) # who do you command, arbitrary  friend or a single peasant?

I am sorry, I do not understand.

for friend in friends:
            hero.command(friend, "move", {'x': coin.pos.x, 'y': coin.pos.y})

The highlight says that I should fix my code. “Try hero.pos.”

I see what you’re trying to do and how you’re trying to do it. I tried it that way and couldn’t get it to work. xython is right, you’ve way over complicated the commandPeasant function. It only needs to define coin and if coin, command peasant to go to the coin’s position. That’s it. Keep it simple.

THX :rice_ball::rice_ball::rice_ball:

All that happens now is that every peasant for the first coin collects it, but only the gold collecting peasant continues.

# Collect all the coins!

def commandPeasant(peasant, coins):
    # Find the nearest coin to the `peasant` from the `coins` array,
    friends = hero.findFriends()
    friend = hero.findNearest(friends)
    coin = friend.findNearestItem()
    if coin:
        hero.command(friend, "move", {'x': coin.pos.x, 'y': coin.pos.y})
    pass

friends = hero.findFriends()
peasants = {
    "Aurum": friends[0],
    "Argentum": friends[1],
    "Cuprum": friends[2]
}

while True:
    items = hero.findItems()
    goldCoins = []
    silverCoins = []
    bronzeCoins = []
    for item in items:
        if item.value == 3:
            goldCoins.append(item)
        # Put bronze and silver coins in their approriate array:
        if item.value == 2:
            silverCoins.append(item)
        if item.value == 1:
            bronzeCoins.append(item)
    
    commandPeasant(peasants.Aurum, goldCoins)
    commandPeasant(peasants.Argentum, silverCoins)
    commandPeasant(peasants.Cuprum, bronzeCoins)

Oh, I get it now. The code only registers the closest friends. Ohhhhhhhhh!!!

I still don’t know what to do except simplify it, which I did. :sweat::sweat:

Get rid of all lines with ‘friends’ and ‘friend’ in your function. You need to command the peasant ( he/she is

    commandPeasant(peasants.Aurum, goldCoins)
    commandPeasant(peasants.Argentum, silverCoins)
    commandPeasant(peasants.Cuprum, bronzeCoins)

from the main loop. Substitute friend with peasant in

coin = friend.findNearestItem()
# edit - you pass to commandPeasant(peasant, coins) coins:  goldCoins,   silverCoins, bronzeCoins
# so your peasant must find nearest coin from coins
hero.command(friend, "move", {'x': coin.pos.x, 'y': coin.pos.y})

in the function. You don’t need to iterate over peasant because this is a single person: Aurum or Argentum or Cuprum.

1 Like

THX :grinning::grinning::grin::grin::rice_ball::rice_ball::rice_ball::rice_ball::rice_ball::rice_ball: