Diamond Dozen Help (python)

# Claim the coins while defeating the marauding ogres.

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 item.value / 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 > bestValue:
            bestItem = item
            bestValue = item.value
        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)

So for some reason, first time I run it, it works, and the rest don’t.
CodeCombat - Coding games to learn Python and JavaScript?

The problem is here. Try checking if bestValue is less than the value over the distance function with item as the argument. Other than that, your code is perfect :slightly_smiling_face:.

1 Like
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 > (item.value / hero.distanceTo(item)):
            bestItem = item
            bestValue = item.value
        itemsIndex += 1
    return bestItem

Do you mean this?

or you can use the function they already gave you: valueOverDistance()

2 Likes
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 > valueOverDistance(item):
            bestItem = item
            bestValue = item.value
        itemsIndex += 1
    return bestItem

Still doesn’t work

1 Like

Hi @Lydia_Song. Couple of things to change here:

This time, try checking if bestValue is less than value over distance with item as an argument. Next:

Set bestValue equal to the value over distance function with item as an argument.

Hope this helps.

2 Likes
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 bestValue > valueOverDistance(item):
            bestItem = item
            bestValue = item.value/ hero.distanceTo(item)
        itemsIndex += 1
    return bestItem

Now my hero doesn’t collect any items

1 Like

your mistake is here, you check if bestValue is bigger, while you should be doing the opposite, you should be checking if its smaller. :slight_smile:
P.S. you should use a for loop

for item in items:
    value = valueOverDistance(item)
    if value > bestValue:
            bestItem = item
            bestValue = value
# this is the same code except a bit better-formatted
2 Likes

No, don’t use a for-loop. Why? It is not covered in the desert, in the mountain though. I suggest that @Lydia_Song sticks with while loops until she reaches the mountain and so that no one gets confused.

This should do the trick now!

1 Like

@Lydia_Song could you post your latest code, or have you solved the level?

1 Like

I used a for-loop in Kithgard…

Hmm, it may not be the case but maybe you already had experience with loops before starting CoCo, or maybe you were doing it all over again. @Lydia_Song may be doing this level for the first time and may have not covered for-loops, so I think that she should stick to what she knows before we go off on a tangent :grimacing:.