Diamond Dozen: The Tricky Dozen

So I don’t even understand what to do on Diamond Dozen, The commented instructions aren’t very clear and the hints aren’t very useful! My Code:

// Claim the coins while defeating the marauding ogres.
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 < coins.length) {
    hero.moveXY(items.pos.x, items.pos.y);
    itemsIndex++;
}
return bestItem;}while(true) {
var enemies = hero.findEnemies();
var enemy = findMostHealth(enemies);
if(enemy && enemy.health > 15) {
    while(enemy.health > 0) {
        hero.attack(enemy);
    }
} else {
    var coins = hero.findItems();
    var coin = null;
    coin = findBestItem(coins);
    if(coin) {
        hero.moveXY(coin.pos.x, coin.pos.y);
    }
}}
1 Like

You must finish

function findBestItem(items) {
    var bestItem = null;
    var bestValue = 0;
    var itemsIndex = 0;
    
    // Loop over the items array.
    // Find the item with the highest valueOverDistance()
    
    return bestItem;
}

using

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

find valueOverDistance() for given item compare to bestValue( Over distance )
Use as a sample for other parts

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;
}

format code propertly

1 Like

Hmm… I tried that but it just wont work

1 Like

Provide your current code

1 Like
// Claim the coins while defeating the marauding ogres.

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 < items.length) {

    return bestItem;
    }
}

while(true) {
    var enemies = hero.findEnemies();
    var enemy = findMostHealth(enemies);
    if(enemy && enemy.health > 15) {
        while(enemy.health > 0) {
            hero.attack(enemy);
        }
    } else {
        var coins = hero.findItems();
        var coin = null;
        coin = findBestItem(coins);
        if(coin) {
            hero.moveXY(coin.pos.x, coin.pos.y);
        }
    }
}

What is Wrong with my code? I don’t understand what to do?

You didn’t do that part
do something like:

for (a in b){
    if (a > greatest){
        greatest = a
    }
}
return a

argh i dont do java script
pls dont chastise me on clean code

It still doesn’t work, Tharin won’t move

// Claim the coins while defeating the marauding ogres.

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 < items.length){
    var item = items[itemsIndex];
    if (item.value / hero.distanceTo(item) < valueOverDistance(item)){
        bestItem = item;
        bestValue = item.value;
    }
    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);
        }
    } else {
        var coins = hero.findItems();
        var coin = null;
        coin = findBestItem(coins);
        if(coin) {
            hero.moveXY(coin.pos.x, coin.pos.y);
        }
    }
}

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 < items.length){
    var item = items[itemsIndex];
    if (item.value / hero.distanceTo(item) < valueOverDistance(item)){
        bestItem = item;
        bestValue = item.value;
    }
    itemsIndex += 1;
    }
    return bestItem;
}

I see some problems here

First one

if (item.value / hero.distanceTo(item) < valueOverDistance(item))

This line of symbols

valueOverDistance(item)

redirect to this function

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

As you can see this this expression

item.value / hero.distanceTo(item) 

and function valueOverDistance(item) do the same.

They get Item value for example 3 and divide it on the distance from the hero to an item for example 10 meters
3g / 10m = 0.3
This index shows you how much gold you will get per 1 meter of the way
3g / 15m = 0.2 gold per meter of the way.
3g / 5m = 0.6 gold per meter of the way.

So the closer item to you and the higher the value of the item the more appealing it is for you.

With numbers your code looks like this:
If ( 0.3 < 0.3 )
{

}

Second one

var bestValue = 0;

It is really confusing but
In fact it is bestValueOverDistance
Instead of the current best value over distance for a given item ( use valueOverDistance(item) function for this )
you assign item.value:

bestValue = item.value;

I tryed this now. I didn’t work. It will collect coins, but not the right coins.

// Claim the coins while defeating the marauding ogres.

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 < items.length){
    var item = items[itemsIndex];
    if (bestValue < valueOverDistance(item)){
        bestItem = item;
        bestValue = item.value;
    }
    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);
        }
    } else {
        var coins = hero.findItems();
        var coin = null;
        coin = findBestItem(coins);
        if(coin) {
            hero.moveXY(coin.pos.x, coin.pos.y);
        }
    }
}

You did great with the issue number one

BUT

It seems like you completely overlooked the second one

Second one

bestValue 

It is really confusing but
In fact it is bestValueOverDistance
Instead of the current best value over distance for a given item ( use valueOverDistance(item) function for this )
you assign item.value:

bestValue = item.value;

Lets check what is going on in this block

if (bestValue < valueOverDistance(item)){
        bestItem = item;
        bestValue = item.value;
    }

again bestValue is the highest valueOverDistance what you encountered up until the current moment
it is 0 at the start

var bestValue = 0;

As the items appear you start to check them
you check first item - it is gem with value 5 and within 10 meters of you --> 5 / 10 --> 0.5
0 < 0.5
so for now
bestItem becomes that gem
bestValue becomes 0.5

then you check second item
It is gold coin - value 3 distance 2 --> 3 / 2 --> 1.5
0.5 < 1.5
So for now
bestItem becomes that gold coin
bestValue becomes 1.5

and so on until you check all items

I tried this, but it still doesn’t work

// Claim the coins while defeating the marauding ogres.

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 < items.length){
    var item = items[itemsIndex];
    if (bestValue < valueOverDistance(item)){
        bestItem = item;
        bestValue = valueOverDistance;
    }
    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);
        }
    } else {
        var coins = hero.findItems();
        var coin = null;
        coin = findBestItem(coins);
        if(coin) {
            hero.moveXY(coin.pos.x, coin.pos.y);
        }
    }
}

valueOverDistance is a function so () required in this case.
An item in the parentheses is the thing you pass to function to work with

Thank you. My code is now working successfully