# \[SOLVED\] Treasure cave please help

**URL:** <https://discourse.codecombat.com/t/solved-treasure-cave-please-help/22422>\
**Category:** Level Help\
**Created:** [June 21, 2020, 5:29pm UTC](https://discourse.codecombat.com/t/solved-treasure-cave-please-help/22422 "2020-06-21T17:29:27Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![ActualllOLLIEpop](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/actualllolliepop/32/12660_2.png) [@ActualllOLLIEpop](https://discourse.codecombat.com/u/ActualllOLLIEpop)\
**Post date:** [June 21, 2020, 5:29pm UTC](https://discourse.codecombat.com/t/solved-treasure-cave-please-help/22422/1 "2020-06-21T17:29:27Z")

</div>

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

```python
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})

```

---

<div class="post-metadata">

**Author:** ![AnSeDra](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ansedra/32/13123_2.png) [@AnSeDra](https://discourse.codecombat.com/u/AnSeDra)\
**Post date:** [June 21, 2020, 5:37pm UTC](https://discourse.codecombat.com/t/solved-treasure-cave-please-help/22422/2 "2020-06-21T17:37:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [June 21, 2020, 5:46pm UTC](https://discourse.codecombat.com/t/solved-treasure-cave-please-help/22422/3 "2020-06-21T17:46:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ActualllOLLIEpop](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/actualllolliepop/32/12660_2.png) [@ActualllOLLIEpop](https://discourse.codecombat.com/u/ActualllOLLIEpop)\
**Post date:** [June 21, 2020, 6:00pm UTC](https://discourse.codecombat.com/t/solved-treasure-cave-please-help/22422/4 "2020-06-21T18:00:55Z")

</div>

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:

```python
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})

```

---

<div class="post-metadata">

**Author:** ![jka2706](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jka2706/32/13636_2.png) [@jka2706](https://discourse.codecombat.com/u/jka2706)\
**Post date:** [June 21, 2020, 6:12pm UTC](https://discourse.codecombat.com/t/solved-treasure-cave-please-help/22422/5 "2020-06-21T18:12:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ActualllOLLIEpop](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/actualllolliepop/32/12660_2.png) [@ActualllOLLIEpop](https://discourse.codecombat.com/u/ActualllOLLIEpop)\
**Post date:** [June 21, 2020, 6:16pm UTC](https://discourse.codecombat.com/t/solved-treasure-cave-please-help/22422/6 "2020-06-21T18:16:13Z")

</div>

thanks! 🥳 (20 chars)

---

<div class="post-metadata">

**Author:** ![ActualllOLLIEpop](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/actualllolliepop/32/12660_2.png) [@ActualllOLLIEpop](https://discourse.codecombat.com/u/ActualllOLLIEpop)\
**Post date:** [June 21, 2020, 6:21pm UTC](https://discourse.codecombat.com/t/solved-treasure-cave-please-help/22422/7 "2020-06-21T18:21:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [June 21, 2020, 6:21pm UTC](https://discourse.codecombat.com/t/solved-treasure-cave-please-help/22422/8 "2020-06-21T18:21:39Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/8/8db334abd450188f0f4e0f52fb9897b28a8067c0.jpeg)

---

<div class="post-metadata">

**Author:** ![ActualllOLLIEpop](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/actualllolliepop/32/12660_2.png) [@ActualllOLLIEpop](https://discourse.codecombat.com/u/ActualllOLLIEpop)\
**Post date:** [June 21, 2020, 6:23pm UTC](https://discourse.codecombat.com/t/solved-treasure-cave-please-help/22422/9 "2020-06-21T18:23:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dedreous](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/dedreous/32/13849_2.png) [@dedreous](https://discourse.codecombat.com/u/dedreous)\
**Post date:** [June 21, 2020, 6:26pm UTC](https://discourse.codecombat.com/t/solved-treasure-cave-please-help/22422/10 "2020-06-21T18:26:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ActualllOLLIEpop](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/actualllolliepop/32/12660_2.png) [@ActualllOLLIEpop](https://discourse.codecombat.com/u/ActualllOLLIEpop)\
**Post date:** [June 21, 2020, 6:33pm UTC](https://discourse.codecombat.com/t/solved-treasure-cave-please-help/22422/11 "2020-06-21T18:33:59Z")

</div>

i managed. yay! 🥳

---

<div class="post-metadata">

**Author:** ![Cedar](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/cedar/32/29302_2.png) [@Cedar](https://discourse.codecombat.com/u/Cedar)\
**Post date:** [June 9, 2022, 5:24pm UTC](https://discourse.codecombat.com/t/solved-treasure-cave-please-help/22422/12 "2022-06-09T17:24:37Z")

</div>

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

```python
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)

```

---

<div class="post-metadata">

**Author:** ![Cedar](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/cedar/32/29302_2.png) [@Cedar](https://discourse.codecombat.com/u/Cedar)\
**Post date:** [June 12, 2022, 3:08pm UTC](https://discourse.codecombat.com/t/solved-treasure-cave-please-help/22422/13 "2022-06-12T15:08:59Z")

</div>

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:

```python
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)

```

---

<div class="post-metadata">

**Author:** ![Cedar](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/cedar/32/29302_2.png) [@Cedar](https://discourse.codecombat.com/u/Cedar)\
**Post date:** [June 12, 2022, 3:20pm UTC](https://discourse.codecombat.com/t/solved-treasure-cave-please-help/22422/15 "2022-06-12T15:20:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![milescombat](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/milescombat/32/34822_2.png) [@milescombat](https://discourse.codecombat.com/u/milescombat)\
**Post date:** [March 14, 2023, 7:23pm UTC](https://discourse.codecombat.com/t/solved-treasure-cave-please-help/22422/16 "2023-03-14T19:23:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![moonwatcher348](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/moonwatcher348/32/24126_2.png) [@moonwatcher348](https://discourse.codecombat.com/u/moonwatcher348)\
**Post date:** [March 16, 2023, 2:58am UTC](https://discourse.codecombat.com/t/solved-treasure-cave-please-help/22422/17 "2023-03-16T02:58:49Z")

</div>

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)
