# Treasured in Ice Help Please

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

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

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.

now my character is going in circles

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.

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

it says here missing quotes