Stuck on Diamond Dozen in Desert


Turns out arrays are a lot more complicated than I have been giving them credit for. :frowning:

I’m currently stuck on Diamond Dozen. Was working on “The Trials” which is still incomplete, but I’ve gotten progress there, and figured I’d find what I was looking for (how to not target the enemy across the map since there is a wall in my way) and tried quite a few things to no avail. Getting off topic so I’ll digress.

Ended up struggling through quite a few of levels that I still have to go back and study on due to a lack of confidence in understanding them, but I can’t for the life of me figure out this one now.

# 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 > targetHealth:
            target = enemy
            targetHealth =
        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 = valueOverDistance(item)
        itemsIndex += 1
        return bestItem

while True:
    enemies = hero.findEnemies()
    enemy = findMostHealth(enemies)
    if enemy and > 15:
        while > 0:
        coins = hero.findItems()
        coin = None
        coin = findBestItem(coins)
        if coin:
            hero.moveXY(coin.pos.x, coin.pos.y)

He is supposed to go after the coin with the best distance and value from what I understand, but I can’t seem to figure out what I am doing wrong. I can get him to go after coins, but it seems that he just goes for whatever is closest if he goes at all. Seriously lost :confused:

while itemsIndex < len(items):
        item = items[itemsIndex]
        if item.value/ hero.distanceTo(item) > bestValue:
            bestItem = item
            bestValue = valueOverDistance(item)
        itemsIndex += 1
return bestItem


If you look at your findBestItem function, you have a return statement inside the while-loop.

Return exits out of functions as soon as it’s called, so on the first item it will check if it is the best item. Since the only item checked is the first item, the first item is probably the best item and it returns that and quits the function.

You probably want the return statement outside of the while loop, but still inside the function.


bestValue must be renamed into bestValueOverDistance
bestValue is confusing

function findMostHealth(enemies) {
    var target = null;
    var targetHealth = 0;
    var enemyIndex = 0;
    while(enemyIndex < enemies.length) {
        var enemy = enemies[enemyIndex];
        if( > targetHealth) {
            target = enemy;
            targetHealth =;
        enemyIndex += 1;
    return target;

function valueOverDistance(item) {
    return item.value / hero.distanceTo(item);

// Return the item with the highest valueOverDistance(item)
function findBestItem(items) {
    var bestItem = null;
    var bestValue = 0;
    var itemsIndex = 0;
    // Loop over the items array.
    // Find the item with the highest valueOverDistance()
    while(itemsIndex < item) {
        var item = items[itemsIndex];
        if(item.value > bestItem) {
            bestItem = item;
            bestValue = valueOverDistance(item);
         itemsIndex +=1;
    return bestItem;

while(true) {
    var enemies = hero.findEnemies();
    var enemy = findMostHealth(enemies);
    if(enemy && > 15) {
        while( > 0) {
    var coins = hero.findNearestItem();
    var coin = findBestItem(coins);
    if(coin) {
            hero.moveXY(coin.pos.x, coin.pos.y);
I cant get my code to work the avatar defeats the enemies 
but wont move at all when it comes 
to picking up coins any help would be appreciated


You’ve done the findMostHealth(enemies) { right it’s the coin bit as you said, which isn’t right, it’s exactly the same as the findMostHealth(enemies) one but you seem to have done it differently:

the item one should be the same as the enemy one.
This next bit is quite confusing, the value that you want to refer to the bestValue in the findBestItem(items)
isn’t the actual value of the coin it’s the valueOverDistance(), so once you’ve found the item

you need to put the item into the valueOverDistance() and make a variable out of it and use that value to refer to the bestValue and to make the new bestValue as you have done here:

Apart from that your code looks fine. Hope this helps! :grin: