[SOLVED] Mountain mercenaries help please (JavaScript)

I have completed the code and my soldiers aren’t enough, should I just get better and faster shoes? or is there something wrong with my code? JavaScript.

// Gather coins to summon soldiers and have them attack the enemy.
function findGold(number) {
    var items = hero.findItems();
    var near = 9999;
    var nearIndex = 0;
    for (var i = 0; i < items.length; ++i) {
        if (items[i].value == number) {
            if (near > hero.distanceTo(items[i])) {
                near = hero.distanceTo(items[i]);
                nearIndex = i;
            }
        }
    }
    return items[nearIndex];
}
function getBetterGold() {
    var gold3 = findGold(3);
    var gold2 = findGold(2);
    var gold1 = findGold(1);
    var cost3 = calculateCost(gold3);
    var cost2 = calculateCost(gold2);
    var cost1 = calculateCost(gold1);
    if (cost3 <= cost2 && cost3 <= cost1)
        return gold3;
    if (cost2 <= cost3 && cost2 <= cost1)
        return gold2;
    if (cost1 <= cost3 && cost1 <= cost2)
        return gold1;
}
function calculateCost(coin) {
    var cost = 9999;
    if (coin)
        cost = hero.distanceTo(coin) / coin.value;
    return cost;
}
while (true) {
    // If you have funds for a soldier, summon one.
    if (hero.gold > hero.costOf("soldier")) {
        //hero.say("I should summon something here!");
        hero.summon("soldier");
    }
    var enemy = hero.findNearest(hero.findEnemies());
    if (enemy) {
        var soldiers = hero.findFriends();
        var soldierIndex = 0;
        var soldier = soldiers[soldierIndex];
        var enemies = hero.findEnemies();
        var ien = 0; 
        // Loop over all your soldiers and order them to attack.
        for (var is = 0; is < soldiers.length; ++is) {
            var enemy = enemies[ien];
            // Use the 'attack' command to make your soldiers attack.
            hero.command(soldiers[is], "attack", enemy);
            if (ien < enemies.length - 1)
                ien++;
            else
                ien = 0;
        }
    }
    // Move to the nearest coin.
    // Use move instead of moveXY so you can command constantly.
    var gold = getBetterGold();
    if (gold) {
        hero.move(gold.pos);
    }
}

1 Like

should I put an attack function in there that makes it so when there is <1 soldier my hero attacks here is pretty decked out, I will send a screen shot

1 Like

1 Like

I cant put and attack function lol

1 Like

Can you tell me what error do you get and post a link to this level?

Andrei

1 Like

I fixed it I just had to submit it to change the order the ogres come in, i’m sorry for not responding right away, I was away.

2 Likes

Then congratulations for completing the level! :partying_face:

Andrei

1 Like

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