Diamond dozen (python)


#1

my code:


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()
    findMostValueOverDistance
    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)


#2

I strongly suggest that you use the search feature and read previous threads about this level. I was stuck on it for a couple of days myself. Your code needs some work and it’s best for you to do some research to learn what needs to be done.


#3

gg on mod, @MunkeyShynes


#4

@SuperSmacker
Thanks. It seems I spend half my life here anyway. LOL.


#5

@Atticus_Black

I found this and thought it might help.


#6

Those are good, though we’re replaced ‘self’ with ‘hero’.


#7

i tried those could you check my code and see what I would put for the missing segment

# 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()
    
    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)


#8
i = 0
greatestValue = 0
for item in item:
    value = valueOverDistance(item)
    # each time you go through the loop, compare the current item's value to the max value.
    if value > greatestValue:
        value = greatestValue
        index = i
        # store the index of the item as well, so you can return the index and use it in the main loop to find and collect the item.
        # index is i in this case.
        i += 1
    else:
        i += 1
return index

#9

thank you for the help