# Treasured in Ice Help Please

**URL:** <https://discourse.codecombat.com/t/treasured-in-ice-help-please/18495>\
**Category:** Level Help\
**Created:** [May 23, 2019, 10:27pm UTC](https://discourse.codecombat.com/t/treasured-in-ice-help-please/18495 "2019-05-23T22:27:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ENd0](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/end0/32/10271_2.png) [@ENd0](https://discourse.codecombat.com/u/ENd0)\
**Post date:** [May 23, 2019, 10:27pm UTC](https://discourse.codecombat.com/t/treasured-in-ice-help-please/18495/1 "2019-05-23T22:27:53Z")

</div>

I’m trying to do this level but my character just gets into a loop. Can someone help?

```python
#lpx and lpy == lastPositionX and lastPositionY
x = hero.pos.x
y = hero.pos.y
while True:
    downClear = hero.isPathClear(hero.pos,{'x':hero.pos.x,'y':hero.pos.y - 16})
    if downClear and y - 16 != lpy:
        lpx = x
        lpy = y
        y-=16
        hero.moveXY(x,y)
    leftClear = hero.isPathClear(hero.pos,{'x':hero.pos.x + 16, 'y':hero.pos.y})
    if leftClear and x - 16 != lpx:
        lpx = x
        lpy = y
        x+=16
        hero.moveXY(x,y)
    rightClear = hero.isPathClear(hero.pos,{'x':hero.pos.x- 16, 'y':hero.pos.y})
    if rightClear and x + 16 != lpx:
        lpx = x
        lpy = y
        x-=16
        hero.moveXY(x,y)
    upClear= hero.isPathClear(hero.pos,{'x':hero.pos.x,'y':hero.pos.y + 16})    
    if upClear and y + 16 != lpy:
        lpx = x
        lpy = y
        y+=16
        hero.moveXY(x, y)

```

---

<div class="post-metadata">

**Author:** ![Shurutsue](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/shurutsue/32/10314_2.png) [@Shurutsue](https://discourse.codecombat.com/u/Shurutsue)\
**Post date:** [May 23, 2019, 10:42pm UTC](https://discourse.codecombat.com/t/treasured-in-ice-help-please/18495/2 "2019-05-23T22:42:40Z")

</div>

Hey there!  
Before anything else, you’ve got a mistake on left and right checks I believe!  
on the _isPathClear function_ you pass hero’s position **+16** and in the if-block after that you check if you’ve been to hero’s position **-16** and the other way around for right.

---

<div class="post-metadata">

**Author:** ![ENd0](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/end0/32/10271_2.png) [@ENd0](https://discourse.codecombat.com/u/ENd0)\
**Post date:** [May 23, 2019, 10:47pm UTC](https://discourse.codecombat.com/t/treasured-in-ice-help-please/18495/3 "2019-05-23T22:47:58Z")

</div>

☹ now my character is going in circles

---

<div class="post-metadata">

**Author:** ![Shurutsue](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/shurutsue/32/10314_2.png) [@Shurutsue](https://discourse.codecombat.com/u/Shurutsue)\
**Post date:** [May 23, 2019, 10:50pm UTC](https://discourse.codecombat.com/t/treasured-in-ice-help-please/18495/4 "2019-05-23T22:50:31Z")

</div>

What you could do now would be to define an array of “positions you’ve been to before” and a function that would check the position passed by iterating through the array and checking if one of these match and if not would return true. That way you’re sure to **never** go to a position you’ve been to before. Just an idea 😄  
Because right now, you just check the last position you’ve been to.

---

<div class="post-metadata">

**Author:** ![ineedhelpwithcoding](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ineedhelpwithcoding/32/30168_2.png) [@ineedhelpwithcoding](https://discourse.codecombat.com/u/ineedhelpwithcoding)\
**Post date:** [September 14, 2020, 7:20am UTC](https://discourse.codecombat.com/t/treasured-in-ice-help-please/18495/5 "2020-09-14T07:20:36Z")

</div>

someone please help me this is my code

```python
# Find the treasure inside the maze.
# When you get the treasure, move to the exit.
# The exit is marked by the red cross. The level ends when you step on the mark.
# Some doors are blocked, some open when you are near them.

exitPosition = {"x": 150, "y": 120}
hero.wait(1)
chest = hero.findItems()[0]
walls = [[0] * 4 for i in range(100)]
steps = []

def direction(n):
    x = 0
    y = 0
    if n == 0:
        y += 16
    elif n == 1:
        x += 16
    elif n == 2:
        y -= 16
    else:
        x -= 16
    return {"x": hero.pos.x + x, "y": hero.pos.y + y}

while True:
    jMod = 0
    doNext = True
    lastStep = (steps[-1] + 2) % 4
    yDif = hero.pos.y - chest.pos.y
    xDif = hero.pos.x - chest.pos.x
    if hero.distanceTo(chest) < 5:
        break
    if yDif > 1:
        if xDif > 1:
            jMod = 2
        else:
            jMod = 1
    else:
        if xDif > 1:
            jMod = 3
    # hero.say(jMod)
    for j in range(4):
        i = (j + jMod) % 4
        checkPoint = direction(i)
        if hero.isPathClear(hero.pos, checkPoint) and walls[len(steps)][i] == 0 and i != lastStep:
            hero.moveXY(checkPoint.x, checkPoint.y)
            steps.append(i)
            doNext = False
            break
    if doNext:
        checkPoint = direction(lastStep)
        walls[len(steps) - 1][steps[-1]] = 1
        steps.pop()
        hero.moveXY(checkPoint.x, checkPoint.y)

while hero.distanceTo(exitPosition) > 10:
    checkPoint = direction((steps[-1] + 2) % 4)
    steps.pop()
    hero.moveXY(checkPoint.x, checkPoint.y)

hero.moveXY(exitPosition.x, exitPosition.y)

```

---

<div class="post-metadata">

**Author:** ![ineedhelpwithcoding](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/ineedhelpwithcoding/32/30168_2.png) [@ineedhelpwithcoding](https://discourse.codecombat.com/u/ineedhelpwithcoding)\
**Post date:** [September 14, 2020, 7:21am UTC](https://discourse.codecombat.com/t/treasured-in-ice-help-please/18495/6 "2020-09-14T07:21:05Z")

</div>

> [@ineedhelpwithcoding](#):
>
> `if hero.isPathClear(hero.pos, checkPoint) and walls[len(steps)][i] == 0 and i != lastStep:`

it says here missing quotes
