[SOLVED] Greed Traps Map - Help


Map link - https://codecombat.com/play/level/greed-traps?

I am on the Greed Traps map and for some reason the map isn’t working properly.

This is the initial instructions filled out with exactly what the developer asked the user to input:

# 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.findItems()
    if item == "coin":
        hero.buildTypes("fire-trap", x, y)

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.
    maybeBuildTrap(12, 12)

Now, I run the script and the hero does move to every X without difficulty. However, he does not detect items nor does he lay down fire-traps. I have tried to modify my code to make this map work properly but I am a novice programmer at best. Maybe one of you more experienced users can figure this out or assist me in figuring out a solution to this problem. Thanks for the assist :slight_smile:


Oh, and yes, I have switched

item = hero.findItems() 


item = hero.findNearestItem()

and it still doesn’t work!


I think you need to check if there’s a coin because if you don’t what are checking the type of?
And you also have to change:

to the function which actually builds the traps, which is shown on the hammer.


Thanks for the response Deadpool198. I actually just solved it. Appreciate you taking the time to respond. I understand what I did wrong. First, I labeled the build type wrong

hero.buildTypes("fire-trap", x, y)

needed to be

hero.buildType("fire-trap", x y)

also, the

item = hero.findItems()

doesn’t work in this scenario better than:

item = hero.findNearestItem()


Moderator, please mark this as Solved!. Thank you :slight_smile:


You were correct to switch from hero.findItems to findNearestItem - findItems is used to find arrays, not individual items.

The problem is in your function. You define item and then check to see if item is a coin, but never look to see if item exists. Since the only items you’ll find in this level are coins, there’s really no need to check for the item type. Try just using, if item: instead.


That’s fine, I’m glad you solved it.


Apparently, I type too slow. :stuck_out_tongue:


Well, you’re response was more clear so it would take longer to type.
Mine was a bit confuzling. LOOL


lol, no worries. I really appreciate the responses. This is my first attempt at coding so I will probably be posting help requests in the forum several times a week. Look forward to picking your brain more :stuck_out_tongue: :slight_smile:


That’s what we’re here for.