[SOLVED]Mountain Mercenaries - Cloudrip Mountain(JS)

The code ducky says I have an infinite loop. Help:

// Gather coins to summon soldiers and have them attack the enemy.

while(true) {
    var coin = hero.findNearestItem();
    // Move to the nearest coin.
    // Use move instead of moveXY so you can command constantly.
    if (coin) {
        hero.move(coin.pos);
    }
    
    // If you have funds for a soldier, summon one.
    if (hero.gold > hero.costOf("soldier")) {
        hero.summon("soldier");
    }
    var enemy = hero.findNearest(hero.findEnemies());
    if (enemy) {
        
        var soldiers = hero.findFriends();
        var soldierIndex = 0;
        var soldier = soldiers[soldierIndex];
        // Loop over all your soldiers and order them to attack.
        while (soldierIndex < soldiers.length) {
            // Use the 'attack' command to make your soldiers attack.
            hero.command(soldier, "attack", enemy);
        }
    }
}

Hi Monsty,

You need to increase soldierIndex each time the while loop runs, otherwise it will go on forever!

Jenny

2 Likes

Thanks @jka2706! It worked!

Seems like clumsy me forgot to increment soldierIndex :sweat_smile:

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