Has anyone figured out to pick only the Nearest Gold Coins?


I know how to pick the gold coins and how to pick the nearest coins, but was wondering, how could I make an array of gold coins and pick the nearest of these? Is it possible?



  1. Step 1: get an array of coins and learn how to get their properties
    this.say("look! another coin of value "+ item.value+" at location "+item.pos);
  1. Step2: select only the gold coins and put them into another array (gold is coin.value==3)

  2. Step3: find the nearest item in the second array

Or much simpler, replace the this.say in the code above with the standard algorithm for find the best property:

best_id =  null
min_property = Infinity
for( all items in the list)
     //In the for code above, replace the say with this test
     if( property(item) < min_property)
            min_property = property(item)
            best_id =item


Second option:

This makes an array of gold coins:

gold = [coin for coin in self.findItems() if item.value is 3]

It loops through all the items in and if the value property is 3 adds it to a new array called gold.


Why does this not work?

coins = []
for item in hero.findItems():
    if item.value == 3:
coin = hero.findNearest(coins)