# Treasured in ice help

``````x=hero.pos.x
y=hero.pos.y
while True:
downClear = hero.isPathClear(hero.pos,{'x':hero.pos.x,'y':hero.pos.y - 16})
rightClear = hero.isPathClear(hero.pos,{'x':hero.pos.x- 16, 'y':hero.pos.y})
leftClear = hero.isPathClear(hero.pos,{'x':hero.pos.x + 16, 'y':hero.pos.y})
upClear= hero.isPathClear(hero.pos,{'x':hero.pos.x,'y':hero.pos.y + 16})
if downClear:
y-=16
hero.moveXY(x,y)
if rightClear:
x-=16
hero.moveXY(x,y)
if leftClear:
x+=16
hero.moveXY(x,y)
if upClear:
y+=16
hero.moveXY(x, y)
``````
1 Like

P.S. Good Job with formatting your code!   -@Luke10

Hi there! I haven’t used Python yet, but i (might) see the potential problem, in case the path to the right is clear twice in a row, or if 2 paths are clear at the same time. (like moving back and forth, or to the side and then up/down against a wall or similar)

in case this is the actual problem (I’m not at that level yet myself)
try checking only one “directionClear” at a time, instead of all four one after another within the same check.
You might also want to store your previous direction as a variable and avoid turning back, unless there’s no other path clear besides the one to turn back to.

hope my thoughts might be of use 3 Likes

@Shurutsue that is exactly my problem.

Perhaps you could use `elif` instead of `if`.

1 Like

Actually, ifs are their own boolean system, so there’s not really that much difference

Though, if I’m not wrong, you check (start position) if there’s a clear path to the sides, then you check each individually, so you check if down is free, if it is, you move there, then check if from the starting position to the right was free, if it is, then it also moves right… and so on until it reaches the end of the check for up. so if all directions were free, it would move down, right, left, up (return to it’s starting position) before actually updating if from the new position after the movement there’s a free path. so I too would guess having the three other if’s be elif’s
That, or to use `continue` after the movement command, which would let the while loop repeat, without moving the other steps. (That is, if I am correct).

Edit:
Also, if I’m not mistaken, your “check” if the right side is free, aswell as the movement to the right actually checks and moves to the left (not that it would necessarily cause an error in your case or similar, but might be worth noting that the coordinate x:0 should actually be at the very left and for example x:100 at the right side. (or is it different for python?)

1 Like

You’re missing a critical piece in your code. You have to check if you have already went to that coordinate or not. You’ll go to the same place over and over again if you don’t check if you went there or not.

Help

``````
# https://codecombat.com/play/level/treasured-in-ice
# 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()
walls = [ * 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)

``````

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

Andrei

1 Like

Uh… I can’t, because each time I try there are no four spaces

You should format the code as it is described here.

Andrei

1 Like
``````# Find the treasure inside the maze.
# When you get the treasure, move to the exit.
# 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()
walls = [ * 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)

``````

Please help me in any way possible. Also, in line 44, it always says there is an error.

Can you show me the line 44?

Andrei

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

1 Like

That’s line 44, just if your curious or confused

Ok. I will have a look at it later, when I will have my PC free.

Andrei

1 Like