Misty island mines help

# 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.
def findBestItem(friend, excludedItems):
    
    items = friend.findItems()
    bestItem = None
    bestItemValue = 0
    for item in items:
        # Use in to check if an element is in the excludedItems array.
        # In that case, skip over that item as another peasant is targeting it.
        if item in excludedItems:
            continue
        # Finish the function!
        # Remember bestItemValue should be the highest item.value / distanceTo
        value = item.value / hero.distanceTo(item)
        if value > bestItemValue:
            bestItem = item
            bestItemValue = value
    return bestItem

# This function checks if you have enough gold for a decoy.
def enoughGoldForDecoy():
    return hero.gold >= 25

while True:
    peasants = hero.findByType("peasant")
    # Create a new array every loop.
    claimedItems = []
    for peasant in peasants:
        enemy = peasant.findNearestEnemy()
        if enemy and enemy.target == peasant and enoughGoldForDecoy() and peasant.distanceTo(enemy):
            hero.command(peasant, "buildXY", "decoy",peasant.pos.x + 2, peasant.pos.y)
        item = findBestItem(peasant, claimedItems)
        if item:
            # After an item is claimed, stick it in the claimedItems array.
            claimedItems.append(item)
            # Command the peasant to collect the coin:
            hero.command(peasant, "move", item.pos)
        
 

it gives me an code never finishes bug

1 Like

What does the bug and/or error say?

It doesn’t give me the infinite loop error when i submit it, but the peasants aren’t summoning decoys because the distanceTo condition doesn’t have a value.

Also the second if statement in the while loop needs to be an else if.

new code peasents still not making decoys

 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.
def findBestItem(friend, excludedItems):
    
    items = friend.findItems()
    bestItem = None
    bestItemValue = 0
    for item in items:
        # Use in to check if an element is in the excludedItems array.
        # In that case, skip over that item as another peasant is targeting it.
        if item in excludedItems:
            continue
        # Finish the function!
        # Remember bestItemValue should be the highest item.value / distanceTo
        value = item.value / hero.distanceTo(item)
        if value > bestItemValue:
            bestItem = item
            bestItemValue = value
    return bestItem

# This function checks if you have enough gold for a decoy.
def enoughGoldForDecoy():
    return hero.gold >= 25

while True:
    peasants = hero.findByType("peasant")
    # Create a new array every loop.
    claimedItems = []
    for peasant in peasants:
        enemy = peasant.findNearestEnemy()
        if enemy and enemy.target == peasant and enoughGoldForDecoy() and peasant.distanceTo(enemy) < 10:
            hero.command(peasant, "buildXY", "decoy",peasant.pos.x + 2, peasant.pos.y)
        item = findBestItem(peasant, claimedItems)
        if item:
            # After an item is claimed, stick it in the claimedItems array.
            claimedItems.append(item)
            # Command the peasant to collect the coin:
            hero.command(peasant, "move", item.pos)
        
 

Did you change the if statement to an else if? (also move the findBestItem next to the findNearestEnemy)

# 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.
def findBestItem(friend, excludedItems):
    
    items = friend.findItems()
    bestItem = None
    bestItemValue = 0
    for item in items:
        # Use in to check if an element is in the excludedItems array.
        # In that case, skip over that item as another peasant is targeting it.
        if item in excludedItems:
            continue
        # Finish the function!
        # Remember bestItemValue should be the highest item.value / distanceTo
        value = item.value / hero.distanceTo(item)
        if value > bestItemValue:
            bestItem = item
            bestItemValue = value
    return bestItem

# This function checks if you have enough gold for a decoy.
def enoughGoldForDecoy():
    return hero.gold >= 25

while True:
    peasants = hero.findByType("peasant")
    # Create a new array every loop.
    claimedItems = []
    for peasant in peasants:
        enemy = peasant.findNearestEnemy()
        item = findBestItem(peasant, claimedItems)
        if enemy and enemy.target == peasant and enoughGoldForDecoy() and peasant.distanceTo(enemy) < 10:
            hero.command(peasant, "buildXY", "decoy",peasant.pos.x + 2, peasant.pos.y)
        elif item:
            # After an item is claimed, stick it in the claimedItems array.
            claimedItems.append(item)
            # Command the peasant to collect the coin:
            hero.command(peasant, "move", item.pos)
        
 

it gives me the error "code never finished. it’s eaither realty slow or has an infinity loop

That’s weird. It works for me but is pretty laggy. Maybe change out the function enoughGoldForDecoy with hero.gold >= 25. It definitely made it lag less. Also in your findBestItem it should use the friend’s distance to the item not the hero’s.

ok i will try it (20 chars )

i beat it !!! (20 chars)

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.