@易卜拉欣102 ,我的代码如下:
def findBestItem(friend, excludeItems):
items = friend.findItems()
bestItem = None
bestItemValue = 0
for item in items:
if item in excludeItems:
# Another peasant is already targeting this item
# The continue statement skips to the beginning of the next iteration of the for-loop
continue
# Find the best item: value divided by distance
distance=friend.distanceTo(item)
rating=item.value/distance
if rating > bestItemValue:
bestItem=item
bestItemValue=rating
# Make sure to return the bestItem!
return bestItem
loop:
peasants = self.findByType("peasant")
claimedItems = []
for peasant in peasants:
nearest = peasant.findNearest(peasant.findEnemies())
# Peasants have gold and costOf() properties, and obey the "buildXY" command.
# If there's an enemy within 10 meters targeting this peasant, build a "decoy" if we can afford one.
# You can get the enemy's target with nearest.target
# Otherwise, use findBestItem to target the best coin to collect
# Use claimedItems.append(item) to add it to the claimedItems array.
if nearest:
distance2=peasant.distanceTo(nearest)
if nearest.target == peasant:
if peasant.gold >= peasant.costOf("decoy"):
self.command(peasant, "buildXY", "decoy",peasant.pos.x-2,peasant.pos.y)
coin=findBestItem(peasant,claimedItems)
if coin:
claimedItems.append(coin)
self.command(peasant, "move", coin.pos)