[solved ] Diamond Dozen JS

Hello I have many troubles with this lvl…
CodeCombat - Coding games to learn Python and JavaScript?
first, hero jump doesn’t work, my hero jump at is actual position then goin for the coin, he doesn’t jump to the coin correctly…
I end with less than 20 gold if i use jumpTo.
then the valueOverDistance looks broken (i didn’t tuched it) : If I use the function, sometimes hero is going for a copper coin that is farther than a close gold coin or diamond…
if I don’t use this function I can finish with 55+ gold, but if I use it it’s much less…
I tryied without jumpTo and withtout the function valueOverDistance, but can’t have more than 62/64 gold…
Next question is how many gold do I hava to gather ? The lvl objectif is to gather “the good quantity” not clear to me…

// 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;
    var itemslist = hero.findItems();
    // Loop over the items array.
    // Find the item with the highest valueOverDistance()
    while (itemsIndex < itemslist.length) {
        var item = itemslist[itemsIndex];
        if (valueOverDistance(item) > bestValue) {
            bestValue = item.value;
            bestItem = item;
        }
        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) {
            if (hero.isReady("jump")) {
                hero.jumpTo(coin.pos);
            } else {
                hero.move(coin.pos);
            }
        }
    }
}

I needed :
bestValue = valueOverDistance(item);
in the loop.

:woozy_face:

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.