# 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
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.
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.
THX