Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

[Solved] Mountain-mercenaries _in javascript,

#1

why !!!

why the blue orges are so powerful!

why my soldiers all died !!!

is there a problem with my code ?

···’‘’

loop {

var coin=this.findNearest(this.findItems());
if (coin) {   
    this.move(coin.pos);       }

if (this.gold > this.costOf("soldier")) {
   this.summon("soldier");
}

var enemy = this.findNearest(this.findEnemies());
if (enemy) {

    var soldiers = this.findFriends();
    var soldierIndex = 0;
    while (soldierIndex<soldiers.length) {
         var soldier = soldiers[soldierIndex];
        this.command(soldier, "attack", enemy);
         soldierIndex++;
    }
}

}

···‘’‘

Merged Doublepost

i added "this.attack(enemy) "in the while-loop,

though i passed the level;

but i don’t think i made it right…

1 Like
#3

You might want to make the soldiers attack their nearest enemy, i.e.

var enemy = soldier.findNearestEnemy()
if (enemy) this.command(soldier, 'attack', enemy)

(Also, this.gold >= this.costOf('soldier') also works (note the >=)

#4

Can’t figure this level out, my soldiers won’t move at all. Can someone please help?

#5

Please post your code( formatted properly ) onto your post and explain what you’ve already tried what’s happening in more detail and any other things that happened when you ran the program e.g. errors or freezing so that people can help you.
Thank you

1 Like
#6

I already figured it out, but Thanks! :slight_smile: @Deadpool198

#7

My code doesn’t seem to work. It has only one soldier attacking enemies at any one time.

while(true) {
    
    var coins = hero.findItems();
    var coin = hero.findNearest(coins);
    
    hero.move(coin.pos);
    
    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];
       
        while (soldierIndex < soldiers.length){
            
            hero.command(soldier, "attack", enemy);
             soldierIndex++;
            
        }
        
    }
}
#8

Look carefully the code in the first post. The solution of your problem is there. You don’t need multiple topics with the same question - delete the other post. ( hero = this )

#9

Thank you, sorry about that; my first time here. I tried deleting the other thread but I don’t have permission.