Misty Island Mine Ran Out of Time


#1
I have recently tried the Misty Island Mine level. I have all of the code. The code works like a charm, but it says i run out of time before the peasants have 300 gold. I am even giving one of them haste every now and again. Nothing

#2

That something wrong with your code. I’ve completed this level. Reply to me on a PM I’m going to send you and there I can help you.


#3

I am having problems with misty island mine. My code is working fine, but there seems to be logic problem in the code. Can you pm me what I should fix? My code is below.

// Collect gold efficiently by commanding peasants wisely!
// Peasants should collect coins and build decoys.
// The function should return the best item per target
// Use an array of ids to ensure no two peasants target the same item.
function findBestItem(friend, excludedItems) {
var items = friend.findItems();
var bestItemValue = 0;
var currentItemValue = 0;
var bestItem = items[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
currentItemValue = item.value / hero.distanceTo(item);
bestItemValue = bestItem.value / hero.distanceTo(bestItem);
if (currentItemValue > bestItemValue) {
bestItem = item;
}
}
return bestItem;
}
while (true) {
var peasants = hero.findByType(“peasant”);
// Create a new array every loop.
var claimedItems = [];
for (var i = 0; i < peasants.length; i++) {
var peasant = peasants[i];
var enemy = peasant.findNearestEnemy();
if (enemy) {
// If the peasant is the target of the enemy
// AND the hero has enough gold for a decoy
if (enemy.target == peasant && hero.gold >=peasant.costOf(“decoy”)){
hero.say(hero.gold);
hero.say(hero.costOf(“decoy”));
hero.command(peasant, “buildXY”, “decoy”, peasant.pos.x - 5, peasant.pos.y);
continue;
} // Command a peasant to build a “decoy”:
// Add a continue so the peasant doesn’t collect coins when building.
}
var item = findBestItem(peasant, claimedItems);
if (item) {
// After an item is claimed, stick it in the claimedItems array.
claimedItems.push(item);
// Command the peasant to collect the coin:
hero.command(peasant, “move”, item.pos);
}
}
}


#4

Please format your code correctly. Then we can help.
Use triple backticks before and after your code.