Unable to figure out diamond dozen

def findMostHealth(enemies):
    target = None
    targetHealth = 0
    enemyIndex = 0
    while enemyIndex < len(enemies):
        enemy = enemies[enemyIndex]
        if enemy.health > targetHealth:
            target = enemy
            targetHealth = enemy.health
        enemyIndex += 1
    return target

def valueOverDistance(item):
    return valueOverDistance(item) > hero.distanceTo(item)

# Return the item with the highest valueOverDistance(item)
def findBestItem(items):
    bestItem = None
    bestValue = 0
    itemsIndex = 0
    
    # Loop over the items array.
    # Find the item with the highest valueOverDistance()
    while itemsIndex < len(items):
        item = items[itemsIndex]
        if item.value/hero.distanceTo(item) < bestValue:
            bestItem = item
            bestValue = valueOverDistance(item)
        itemsIndex += 1
    return bestItem

while True:
    enemies = hero.findEnemies()
    enemy = findMostHealth(enemies)
    if enemy and enemy.health > 15:
        while enemy.health > 0:
            hero.attack(enemy)
    else:
        coins = hero.findItems()
        coin = None
        coin = findBestItem(coins)
        if coin:
            hero.move(coin.pos.x, coin.pox.y)

I’m on the same level. You can join this topic if you want.

1 Like

Hi elemersglue,

While it’s nice of CocoCharlie to invite you to a discussion of his code, I think you’re probably after some hints about your code.

These lines here need some work:

        if item.value/hero.distanceTo(item) < bestValue:
            bestItem = item
            bestValue = valueOverDistance(item)

Firstly have a think about the top line. If you had an item that had a value of 10 and was 1 metre away; and an item that had a value of 5 and was 5 metres away, then what would each of their ‘item.value/hero.distanceTo(item)’ be? Which one would be better? Which one would get set to the bestItem?

Once you’ve worked this one out, then the bottom of these three lines needs changing. Your valueOverDistance function doesn’t work. You can either fix it, or you can just put in the expression that you already have to find the item’s value divided by the hero’s distance to the item (which is…?).

Post again if you need more help.

Jenny

1 Like