So my hero gets carried away by collecting coins, and not summoning any troops. Is there any way to counter this. I know there’s at least one.
This is my code.
hero.summonSoldiers = function() {
var friends = hero.findFriends();
if (hero.gold === 20) {
hero.summon("soldier");
}
};
// Define the function: commandSoldiers
this.commandSoldiers = function() {
var soldiers = hero.findByType("soldier");
if (soldiers){
for (var index = 0; index < soldiers.length; index++){
var soldier = soldiers[index];
var enemy = soldier.findNearestEnemy();
if (enemy){
hero.command(soldier, 'attack', enemy);
}
}
}
};
// Define the function: pickUpNearestCoin
function pickUpNearestCoin() {
var item = hero.findNearestItem();
if (item) {
hero.move(item.pos);
}
}
var peasant = hero.findByType("peasant")[0];
while(true) {
hero.summonSoldiers();
hero.commandSoldiers();
pickUpNearestCoin();
}