[SOLVED] Greed Traps Map - Help


#1

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)
    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.
    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:


#2

Oh, and yes, I have switched

item = hero.findItems() 

with

item = hero.findNearestItem()

and it still doesn’t work!


#3

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.


#4

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()

#5

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


#6

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.


#7

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


#8

Apparently, I type too slow. :stuck_out_tongue:


#9

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


#10

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:


#11

That’s what we’re here for.


#12

Now I need help! Here is my code:

# 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)
    item = hero.findNearestItem()
    if item:
        if item.type == "coin":
            hero.buildXY("fire-trap", x, y)
    # Search a coin and  if you see it, build a "fire-trap"
    
    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.
    maybeBuildTrap(12, 12)


#13

Can you please explain what goes wrong when running the code?


#14

For some reason the character just goes on walking in a rectangle shape but ignores coins, therefore forgetting to build traps! Some coins do not even appear!


#15

Solved it! It was failing with Okar, but it worked perfectly with Anya!