Dimond Dozen - please, assist

Can’t find out what’s wrong.

# 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()
    items = hero.findItems()
    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.moveXY(coin.pos.x, coin.pos.y)

Without finding what the actual problem was. Your code is very repetitive.

In the findBestItem method.

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

Honestly it would be better if you created a variable to hold this value since you then call the method to assign a value to bestValue. Knock out two birds with one stone and put the value in a holding variable before the if statement. You could then have

Holding_Variable = valueOverDistance(item)

if Holding_Variable > bestValue:
    bestValue = Holding_Variable

You also pass in an array called items but then you create a new variable called items wich holds the same exact value as the passed in variable. Right above the while statement.

You’re setting items = hero.findItems() in the function findBestItem, but why do you need to do that? You’re already sending the items variable to the function as a parameter.

If that doesn’t fix it, please provide some more details as to what is happening when you run the code. The code looks good to me, except findBestItem could use some work in a couple places. For instance, you’re using item.value / hero.distanceTo(item), but that’s exactly what your valueOverDistance function returns. This part shouldn’t make a big difference, though.

Hi there.

Thanks for the advice, but the problem, that I should’ve mentioned - remains.

Goal - to collect optimal amount of coins - fails.

Oh, I don’t think that’s a problem with your code. That’s a problem with the speed of your character. Buy better gear or just go back to it later.

It is, already, as fast as possible…)
P.S.
Before this level is completed - there is no proceeding further.

You’re using a basic warrior herio, speed ring, and +2 speed boots?

I’ll go test your code, then.

Your exact code works for me. Can you please send me a screenshot with the gear that you have on?

It even works for me without speed ring, so I’m going to bet you’re using 1.5 boots instead of 2 speed boots, or your glasses are too poor.

I’ve just clicked on “Submit” button and it worked too.
I’ve tried the “Run” button before.
Looks like some bug or something…

Thank you for the fast feedback, anyway!

Sure, glad it worked for you.

Everytime you press the submit button it generates a new set of monsters. Its to keep everything random so you cant perfect the level when you keep pressing run. If you submit it again you may or may not fail.

2 Likes

[Please, don’t post solutions here]

+2.5 speed boots

Try not to revive dead topics as they will unnecessarily push them back up to the top of the topic list. :slight_smile:

1 Like