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

[SOLVED] Please Help Me on Misty Island Mine

Could someone please help me on this level? I’m finding the logic behind it a bit confusing. Thanks a lot! :slight_smile: . 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 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
        
        // "I have no idea what to put here"

    return bestItem;
}

// This function checks if you have enough gold for a decoy.
function enoughGoldForDecoy() {
    return hero.gold >= 25;
}

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) && (enoughGoldForDecoy())) {
                // Command a peasant to build a "decoy":
                hero.command(peasant, "buildXY", "decoy");
                //" Add a continue so the peasant doesn't collect coins when building."
                continue;
            }
        }
        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", {'x': item.pos.x, 'y': item.pos.y});
        }
    }
}

Ok. After a few modifications, i can finally get the peasants to start collection coins. But I cannot seem to command them to build decoys. My code is below and the error message below that…

// 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 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 value = item.value / peasant.distanceTo(item);
        if (value >= bestItemValue) {
            bestItemValue = value;
            bestItem = item;
        }
    }
    return bestItem;
}

// This function checks if you have enough gold for a decoy.
function enoughGoldForDecoy() {
    return hero.gold >= 25;
}

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) && (enoughGoldForDecoy())) {
                // Command a peasant to build a "decoy": 
                var x = peasant.pos.x - 2;
                var pPos = peasant.pos;
                var y = pPos.y;
                hero.command(peasant, "buildXY", "decoy", (x, y));
                //" Add a continue so the peasant doesn't collect coins when building."
                continue;
            }
        }
        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", {'x': item.pos.x, 'y': item.pos.y});
        }
    }
}

Error message:

Line 48:
Argument Error:
buildXY’s argument “y” should have type “number”, but got “null”.
Build the “# {toBuild}” at an (x, y) coordinate.

In the end don’t worry I’ve figured it out.