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

Hunters and prey Help me please

Hello everybody,

I don’t understand why my code don’t work !

Could you help me please :souriant:

// Ogres are trying to take out your reindeer!
// Keep your archers back while summoning soldiers to attack.

hero.pickUpCoin = function() {
    // Collect coins.
    var item = hero.findItems();
    var nearest = hero.findNearest(item);
    if (nearest) {
        hero.move(nearest.pos);
    }
};

hero.summonTroops = function() {
    // Summon soldiers if you have the gold.
    if(hero.gold > hero.costOf("soldier")){
       hero.summon("soldier");   
    }
};

// This function has an argument named soldier.
// Arguments are like variables.
// The value of an argument is determined when the function is called.
hero.commandSoldier = function(soldier) {
    // Soldiers should attack enemies.
    var soldiers = hero.findFriends();
    var soldierIndex =0;
    for(var i =0; i< soldiers.length; i++){
        soldier = soldiers[i];
        var enemy = soldier.findNearestEnemy();
        if(enemy){
       hero.command(soldier, 'attack', enemy);
        }
    }
};

// Write a commandArcher function to tell your archers what to do!
// It should take one argument that will represent the archer passed to the function when it's called
hero.commandArcher = function(archer){
var archers = hero.findFriends();
    var archerIndex =0;
    for(var i =0; i< archers.length; i++){
        archer = archers[i];
        var enemy = archer.findNearestEnemy();
        if(enemy && archer < 25){
       hero.command(archer, 'move', archer);
        }
    }
};
// Archers should only attack enemies who are closer than 25 meters, otherwise, stay still.


while(true) {
    hero.pickUpCoin();
    hero.summonTroops();
    hero.commandArcher();
    var friends = hero.findFriends();
    for(var i=0; i < friends.length; i++) {
        var friend = friends[i];
        if(friend.type == "soldier") {
            // This friend will be assigned to the variable soldier in commandSoldier
            hero.commandSoldier(friend);
        } else if(friend.type == "archer") {
            // Be sure to command your archers.
            hero.commandArcher(friend);
        }
    }
}

The pickupCoin and summonTroops look good, but the commandSoldier and commandArcher need some cleaning up.

One main thing I noticed is that you are looking for all your friends again in the command functions. The idea with this level is to go through each friend in the main code and as you determine if a friend is a soldier or archer command that one friend at that time. So your command functions should simply include; find an enemy for that one friend and if an enemy attack, with one extra check for the archers if the distance to the enemy is less than 25.

It might be easier to remove all the code for the command functions and start fresh or it might be confusing trying to clean it all up.

Thanks you i’m going to try again.

Hello, just checking in again. Were you able to complete the level? We can go through it in more detail if you are still struggling with this level. :nerd_face: