hello everyone,
I’m stuck in the grim determination level. I wrote a really elaborate code with a lot of functions, so I cut out what I think that has the problem. the rest of the code works fine, it’s just that the peasant don’t collect enough coins. they keep trying to go for the same coin. I tried to change that but it still doesn’t work. what is the problem?
here’s my code (regarding this):
function findBestItem(friend, excludedItems) {
var items = friend.findItems();
var bestItem = null;
var bestItemValue = 0;
for(var i = 0; i < items.length; i++) {
var item = items[i];
// indexOf searches and array for a certain element:
var idx = excludedItems.indexOf(item);
// If the array doesn't contain it, it returns -1
// In that case, skip over that item as another peasant is targeting it.
if(idx != -1) {
continue;
}
// Finish the function!
// Remember bestItemValue should be the highest item.value / distanceTo
var distance = friend.distanceTo(item);
if ((item.value/hero.distanceTo(item)) > bestItemValue) {
excludedItems.push(item);
bestItem = item;
bestItemValue = bestItem.value;
}
}
return bestItem;
}
function commandPeasants(){
var peasants = hero.findByType("peasant");
for (var peasantIndex = 0; peasantIndex<peasants.length;peasantIndex++){
var peasant = peasants[peasantIndex];
var claimedItems = [];
var coin = findBestItem(peasant,claimedItems);
if (coin) {
hero.command(peasant, "move", coin.pos);
}
}
}