Greed Traps Help

I’ve tried this code many times, and I think it’s right, but the first enemy that comes my character doesn’t build a fire-trap for. All of the other ones it does. Could I have some help please?
Thanks in advance!

// Patrol around the clearing and place traps ONLY if you see a coin.
// Move from one mark to another moving clockwise and avoiding the coins.

// Write this function.
function maybeBuildTrap(x, y) {
    // Move to the x,y postion and build a 'fire-trap' if a coin is near.
    var item = hero.findNearestItem();
    
    hero.moveXY(x, y);
    if (item) {
        hero.buildXY("fire-trap", x, y);
        
    }
}

while (true) {
    // Call 'maybeBuildTrap' function for the top left passage.
    maybeBuildTrap(12, 56);
    // Now for the top right passage.
    maybeBuildTrap(68, 56);
    // Now for the bottom right passage.
    maybeBuildTrap(68, 12);
    // Now for the bottom left passage.
    maybeBuildTrap(12, 12);
}

Try to move first, then search an item.

hi this is my code but i’m still not winning pls help
**# Patrol and place traps ONLY if you see a coin.

Write this function.

def maybeBuildTrap(x, y):
# Move to the given x,y postion
hero.moveXY(x, y)
# Search a coin and if you see it, build a “fire-trap”
item = hero.findNearestItem()
if item.type == “coin”:
hero.buildXY(“fire-trap”, x, y)
pass

while True:
# Call maybeBuildTrap for the top left passage.
maybeBuildTrap(12, 56)
# Now for the top right passage.
maybeBuildTrap(68, 56)
# Now for the bottom right passage.
maybeBuildTrap(68, 12)
# Now for the bottom left passage.
mayeBuildTrap(12, 12)
**

Two small errors:
You misspelt maybe here:

And you didn’t check if the coin existed (if coin) here:

Remember you can use and to link two conditions in an if statement. Like:

if enemy and enemy.type == "ogre":
    hero.attack(enemy)

Danny

2 Likes

yes that is also a problem thank you