[SOLVED] Treasure cave please help

i’m having problems with this level. i don’t know what they are. here’s the code:

while True:
    if hero.pos.y < 43 and hero.time < 5:
        hero.move({"x":73, "y":45})
        hero.move({"x":64, "y":44})
    else:
        fireTrap = hero.findHazards()
        hero.buildXY("fire-trap", 45, 46)
        pos1 = {"x":64, "y":44}
        pos2 = {"x":72, "y":9}
        pos3 = {"x":41, "y":13}
        while fireTrap:
            if hero.distanceTo(pos1) > 1:
                hero.move(pos1)
            elif hero.distanceTo(pos2) > 1:
                hero.move(pos2)
            elif hero.distanceTo(pos3) > 1:
                hero.move(pos3)
        coin = hero.findNearestItem()
        while coin:
            if coin:
                hero.move(coin.pos)
        hero.move({"x":41, "y":13})
        hero.move({"x":72, "y":9})

Hi. Could you, @dedreous, help him because I have to go to bed. Bye!

Heh…I’m already looking at it. @ActualllOLLIEpop, your hero is getting caught in a never ending (rather than endless) loop, where he is cycling between pos1 and pos2…the code never progresses beyond the move(pos2) line.

In my solution, I used a linear method, rather than loops. Not saying we can’t get your version to work, just that it might be a bit more complex than necessary. In my method, I build a trap on the X, move back behind the trees, then on to the treasure. Using the only loop, I pick up all items, then high tail it back to the starting point. This took only 7 lines of code.

If you would like to give this a try, go for it! Otherwise, we can work on your code as it is.

1 Like

hi @dedreous. i’ve tried the linear method, and my hero starts by going to the place where he need to be and builds a trap, but then he goes straight to the coins here’s my code:

hero.move({"x":73, "y":45})
hero.move({"x":64, "y":44})
hero.buildXY("fire-trap", 45, 46)
hero.move({"x":64, "y":44})
hero.move({"x":72, "y":9})
hero.move({"x":41, "y":13})
coin = hero.findNearestItem()
while coin:
    if coin:
        hero.move(coin.pos)
hero.move({"x":41, "y":13})
hero.move({"x":72, "y":9})

Try using moveXY rather than move. With moveXY the code waits until the hero has completed the move before running the next line of code.

1 Like

thanks! :partying_face: (20 chars)

@jka2706, i tried it, but for some reason the trap won’t activate

If it helps, here’s my ‘dance plan’…4 moveXY statements (counting the build trap as the 1st move):

but the trap doesn’t activate does it @dedreous?

It does…it blows, just about the time I’m rounding the bottom trees, providing a distraction just long enough for me to reach the coins. The yetti does attack me, but I’m able to finish collecting all of the coins and the run back to the start…he does chase me, but it’s already too late by then, the game is over.

i managed. yay! :partying_face:

2 Likes

My code doesn’t work. the trap doesn’t trigger.
My code:

hero.moveXY(73, 45)
hero.moveXY(64, 44)
hero.buildXY("fire-trap", 45, 46)
hero.moveXY(64, 44)
hero.moveXY(72, 9)
hero.moveXY(41, 13)
coin = hero.findNearestItem()
while coin:
    if coin:
        hero.move(coin.pos)
hero.moveXY(41, 13)
hero.moveXY(72, 9)

The trap triggers now, but the yeti doesn’t investigate it… it just moves there and back with no pause to allow me to get the coins.
my code:

hero.moveXY(73, 45)
hero.moveXY(64, 44)
hero.buildXY("fire-trap", 45, 46)
hero.moveXY(64, 44)
hero.moveXY(72, 9)
hero.moveXY(41, 13)
coin = hero.findNearestItem()
while coin:
    if coin:
        hero.move(coin.pos)
hero.moveXY(41, 13)
hero.moveXY(72, 9)

I fixed it. I was placing the trap at the end of the top road, not the X.

2 Likes

I need javascript answer. I tried looking on youtube but it doesn’t have it

Hi, we don’t really give you the solution here but rather try to help :) so would you like to post your code and someone can have a look at it? (also, (@)dedreous hasn’t been on for a long time so replying to his posts is useless)

(also, side note, I know a website with every solution on codecombat, so if code is copied from somewhere I will know, and if I don’t, someone else will)