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);
}
}
}
