Help treasured in ice plsss

# 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()[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)

Copying code from GitHub is not going to help you.

First of all, this is a standard BFS search problem. Start by tuning in a queue and visited list. If CoCo does not allow that, you can implement on your own (replace queue with list although it is slower). Here is an idea to get you started with.

queue = []
visited = []
sx = [-16, 16, 0, 0]
sy = [0, 0, -16, 16]

# Note that CoCo defults position in a Dictionary, you need to store them in tuples
# by calling .x and .y in the hero.pos dictionary
queue.append((hero.pos.x, hero.pos.y), [])
visited.append((hero.pos.x, hero.pos.y)) 

while len(queue) > 0:
    (x, y), path = queue.pop(0)
    # initalize your goal first, right now it is not defined
    if (x, y) == goal:
        return path

    visited.append((x, y))

    for dx, dy in zip(sx, sy):
        nx, ny = x + dx, y + dy
            if (nx, ny) not in visited:  # Check valid move
# from this point, there are a lot of conditions unchecked, and you will have to
# do that on your own

# You may implement hero.moveXY here as well, and check with isPathClear()
# You may also store the position as an object and call with isPathClear() too.
                queue.append(((nx, ny), path + [(nx, ny)])) # add the valid to path

    return [] # if no path is found, return empty list

You should implement the conditions, the whole grid of positions own your own, and your logic with moving the hero to the path.

# Sample
for x, y in path:
    hero.moveXY(x, y)
queue = []
visited = set()  
sx = [-16, 16, 0, 0]  
sy = [0, 0, -16, 16]  


chest = hero.findNearestItem()
if not chest:
    hero.say("No chest found!")
    return

goal = (chest.pos.x, chest.pos.y)


queue.append(((hero.pos.x, hero.pos.y), []))
visited.add((hero.pos.x, hero.pos.y))

while len(queue) > 0:
    (x, y), path = queue.pop(0)

    
    if (x, y) == goal:
        for px, py in path:
            hero.moveXY(px, py)
        return 

    
    visited.add((x, y))

    
    for dx, dy in zip(sx, sy):
        nx, ny = x + dx, y + dy
        if (nx, ny) not in visited and hero.isPathClear(x, y, nx, ny):
            queue.append(((nx, ny), path + [(nx, ny)])) 

i tried this but my hero does not move, can you pls help???

i would appreciate a fast response because this is a school thing thats due in a week so any help is appreciated :slight_smile:

hey @AnSeDra can you help me with this pls

Hello @Accelerator23, AnSeDra has not logged into these forums for 5 years.

oh ok thanks for letting me know

could u help me with this lvl btw

This level is too advanced for me, sorry.

oh then its ok, can anyone else help me? because i think andrea was the only one who knew how to do this level

@Anna can u help me with this

Try using something like this but for each direction not just east, btw I finished this level

def east():
    return {'x': coorConvert(hero.col - 2),
        'y': coorConvert(hero.row),
        'row': hero.row, 'col': hero.col - 2}

hey! my teacher also tried to help me search this up, but he also gave up and sent me the code. Thanks all for helping me!!!