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

[SOLVED] Diamond Dozen, help

Hy guy, i need help for this level, this is my code:

def findMostHealth(enemies):            # Claim the coins while defeating the marauding ogres.
    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 item.value / hero.distanceTo(item)

def findBestItem(items):            # Return the item with the highest valueOverDistance(item)
    bestItem = None
    bestValue = 0
    itemsIndex = 0
    
    while itemsIndex < len(items):
        item = items[itemsIndex]
        if item.value / hero.distanceTo(item) > bestValue:
            bestValue = item
            bestitem = 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.moveXY(coin.pos.x, coin.pos.y)

hero don’t move to coin… why?

you didnt use any of the functions that u defined.

Perhaps calling some of them in your while true loop will help

bestValue = item
bestitem = valueOverDistance(item)

Shouldn’t these be switched around? (bestValue to the valueOverDistance and bestItem to the corresponding item)
Additionally i believe it should be bestItem (capital i), unless python doesn’t care about case sensitivity.
That’s all i see currently, other than that it seems to be fine, so i believe it’s just a slip of the pen. :smile:

1 Like

I define 3 function and i calling all…

Yes, thanks Shurutsue, i switchtced value, and correct Capital i … and now work… thans so much