Play Home Contribute GitHub Join Us Discourse Staff Members GitHub Contribution Guides Team

Stuck on diamond desert [HELP]


#1
# 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 valueOverDistance > 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)


#2

Please explain the problem.


#3

he doesn’t collect anything


#4

Sorry, but I don’t have that much time because I’m tired. Maybe @Chaboi_3000 or @MunkeyShynes could help you.


#5

oh
thanks
by the way


#6

Hi,
all you need to do is put an argument (something which goes in between the brackets of a function), in this case: item, here:

Otherwise the function valueOverDistance doesn’t have any “item” to compare value with distance.
Hope this helps!
:lion: :lion: :lion:


#7

it still says incomplete

# 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):
        items = items[itemsIndex]
        if valueOverDistance(item) > bestValue:
            bestItem = item
            bestValue = valueOverDistance()
        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)


#8

Why have you changed this line:

In your first post this line was right, now it’s wrong for the same reason that I said before.
If you change it back to what it was before it will work.
:lion: :lion: :lion:


#9

this?

bestValue = valueOverDistance(item)

#10

Yeah, in both places. :grin:
:lion: :lion: :lion:
(that should work)


#11
if valueOverDistance(item) > bestValue:
            bestItem = item
            bestValue = valueOverDistance(item)
        itemsIndex += 1

#12

i tried this it doesn’t work


#13

Ok, sorry there are two more things I missed the first time.

  1. this:

Look at it carefully, I think you can see it.

  1. and this:

This isn’t what was taught in the previous levels, well not quite. (> ----- <)

I’ve tried it now and it works.
:lion: :lion: :lion:


#14

im still kinda confused


#15

Confused about what?


#16

Ok,
I’ve tolded you quite clearly what to do.
Play around with it and it’ll work.
:lion: