# Stuck on Diamond Dozen in Desert

#1

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

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 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 = 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)
``````

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

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

#3

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.

#4

@serg
Imho
bestValue must be renamed into bestValueOverDistance
bestValue is confusing

#5
``````function findMostHealth(enemies) {
var target = null;
var targetHealth = 0;
var enemyIndex = 0;
while(enemyIndex < enemies.length) {
var enemy = enemies[enemyIndex];
if(enemy.health > targetHealth) {
target = enemy;
targetHealth = enemy.health;
}
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 && enemy.health > 15) {
while(enemy.health > 0) {
hero.attack(enemy);
}
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

``````

#6

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!