I'm stuck on Fragile Maze

Hey everyone,
I’m stuck on fragile maze and I can’t work out how to fix my code:

# Escape from the maze!
# Some doors are blocked, some open when you are near them.

distanceBetweenRooms = 16
startRoom = {"x": 18, "y": 19}
hero.moveXY(hero.pos.x, hero.pos.y + 16)

while True:
    if hero.isPathClear(hero.pos, {"x": hero.pos.x, "y": hero.pos.y + 16}):
        hero.moveXY(hero.pos.x, hero.pos.y + 16)
    if hero.isPathClear(hero.pos, {"x": hero.pos.x + 16, "y": hero.pos.y}) and not hero.isPathClear(hero.pos, {"x": hero.pos.x, "y": hero.pos.y + 16}):
        hero.moveXY(hero.pos.x + 16, hero.pos.y)
    elif hero.isPathClear(hero.pos, {"x": hero.pos.x, "y": hero.pos.y - 16}) and not hero.isPathClear(hero.pos, {"x": hero.pos.x, "y": hero.pos.y + 16}) and not hero.isPathClear(hero.pos, {"x": hero.pos.x + 16, "y": hero.pos.y}):
        hero.moveXY(hero.pos.x, hero.pos.y - 16)
    else:
        if hero.isPathClear(hero.pos, {"x": hero.pos.x - 16, "y": hero.pos.y}) and not hero.isPathClear(hero.pos, {"x": hero.pos.x + 16, "y": hero.pos.y}) and not hero.isPathClear(hero.pos, {"x": hero.pos.x, "y": hero.pos.y + 16}) and not hero.isPathClear(hero.pos, {"x": hero.pos.x, "y": hero.pos.y - 16}):
            hero.moveXY(hero.pos.x - 16, hero.pos.y)

It always stops sometimes and goes up and down and up and down and up and down…etc.
I know I should make an array of past coordinates but I can’t work out how to do at all.
Thanks in advance,
Deadpool198 :lion::lion::lion:

1 Like

make an array for it

Say visited = []

Then when your hero moves, then do visited.append(hero.pos), and then when you are going to move to a new position, then do the if Path Clear if loop first, then call on a for loop inside it to iterate through the elements of visited and see if the new position matches one of the visited positions.

If so, break out of the if loop

if not, move to the new position.

I’ve tried this:

# Escape from the maze!
# Some doors are blocked, some open when you are near them.

distanceBetweenRooms = 16
startRoom = {"x": 18, "y": 19}
hero.moveXY(hero.pos.x, hero.pos.y + 16)
visited = []
while True:
    if hero.isPathClear(hero.pos, {"x": hero.pos.x, "y": hero.pos.y + 16}):
        pos = {"x": hero.pos.x, "y": hero.pos.y + 16}
        for room in visited:
            if room == visited:
                break
        hero.moveXY(hero.pos.x, hero.pos.y + 16)
        visited.append(hero.pos)
        
    if hero.isPathClear(hero.pos, {"x": hero.pos.x + 16, "y": hero.pos.y}):
        pos = {"x": hero.pos.x + 16, "y": hero.pos.y}
        for room in visited:
            if room == visited:
                break
        hero.moveXY(hero.pos.x + 16, hero.pos.y)
        visited.append(hero.pos)
    elif hero.isPathClear(hero.pos, {"x": hero.pos.x, "y": hero.pos.y - 16}):
        pos = {"x": hero.pos.x, "y": hero.pos.y - 16}
        for room in visited:
            if room == visited:
                break
        hero.moveXY(hero.pos.x, hero.pos.y - 16)
        visited.append(hero.pos)
        
    elif hero.isPathClear(hero.pos, {"x": hero.pos.x - 16, "y": hero.pos.y}):
        pos = {"x": hero.pos.x - 16, "y": hero.pos.y}
        for room in visited:
            if room == visited:
                break
        hero.moveXY(hero.pos.x - 16, hero.pos.y)
        visited.append(hero.pos)

but I think it isn’t working because of the break only leaving the for -loop and still triggering the move, but I’m not sure and I have tried this kind’ve thing but I’m not sure where I’m going wrong.
Thanks for replying, Deadpool198 :lion::lion::lion: (that’s my sign of from now on)

Hello Deadpool.
I am going to give you some tips on this problem

First you will need to learn about recursive programming aka Recursivity

What is Recursivity?

Recursive programming is saying to the computer to repeat a function a given number of time.
Remember when you responded to a topic about division or multiplication?

you said a a multiplication is a serie of addition.
Even though this kind of function is not optimal you have to know that it exists:

Let’s make an addition function like this:

def add(number, times):
    result = 0
    if times > 0:
        result = result + number
        times = times - 1
        return result + add(number, times)
    else:
        return result

hero.say(add(5, 5))

You will see that the hero will say: 25. So he repeated the same function 5 times.

Even though this problem is quite simple, recursivity can be used in very complex problems.

Now how recursivity can be used in Fragile Maze?
I will respond to this later

but here some tips:
tip #1: hero can go in 4 directions in this problem.
tip #2: You don’t want the hero to choose between two or more paths, you want the hero to take all paths at once.
tip 3# There is a part in recursive programming called recursive tree.

Alright have fun with this

3 Likes

Thank you very much, but could I also complete the level with what I’m trying to do above?
:lion::lion::lion:

1 Like

visited elements is not the best way of handling this problem because it doesn’t mean you don’t have to go back to that element later to get the right path.

if your hero as 2 choices (going up or going down) we could say he visited the last cell. But then he won’t go back taking the right path because he visited the cell

This problem look more like a Tree of if and else

if north path is clear:
   go that path and check all path again, move those path, or go back
if right path is clear:
   go that path and check all path again, move those path, or go back
if left path is clear:
   go that path and check all path again, move those path, or go back
if south path is clear:
   go that path and check all path again, move those path, or go back 
   (if some path are clear again, go that path, check all path etc, move if clear, go back...)

go back

Obviously I simplified the problem to a large extent.
There is a bit more to do to make everything work

I am not saying it is impossible with arrays though, because everything done with recursion can also be done with iterations.

1 Like

This doesn’t make sense.

“if room == visited:” will never return true because room is an element in the visited array, so there’s no way it’ll be equal to the array.

Ok:

# Some doors are blocked, some open when you are near them.

distanceBetweenRooms = 16
startRoom = {"x": 18, "y": 19}
hero.moveXY(hero.pos.x, hero.pos.y + 16)
visited = []
while True:
    if hero.isPathClear(hero.pos, {"x": hero.pos.x, "y": hero.pos.y + 16}):
        pos = {"x": hero.pos.x, "y": hero.pos.y + 16}
        for room in visited:
            if room == pos:
                break
        hero.moveXY(hero.pos.x, hero.pos.y + 16)
        visited.append(hero.pos)
        
    if hero.isPathClear(hero.pos, {"x": hero.pos.x + 16, "y": hero.pos.y}):
        pos = {"x": hero.pos.x + 16, "y": hero.pos.y}
        for room in visited:
            if room == pos:
                break
        hero.moveXY(hero.pos.x + 16, hero.pos.y)
        visited.append(hero.pos)
    elif hero.isPathClear(hero.pos, {"x": hero.pos.x, "y": hero.pos.y - 16}):
        pos = {"x": hero.pos.x, "y": hero.pos.y - 16}
        for room in visited:
            if room == pos:
                break
        hero.moveXY(hero.pos.x, hero.pos.y - 16)
        visited.append(hero.pos)
        
    elif hero.isPathClear(hero.pos, {"x": hero.pos.x - 16, "y": hero.pos.y}):
        pos = {"x": hero.pos.x - 16, "y": hero.pos.y}
        for room in visited:
            if room == pos:
                break
        hero.moveXY(hero.pos.x - 16, hero.pos.y)
        visited.append(hero.pos)

Is this because when it’s breaking from the for-loop it’s still moving?
Thanks, :lion:

I’ll take a look at it later.

I have the same problem!

# Escape from the maze!
# Some doors are blocked, some open when you are near them.

num = 16
startRoom = {"x": 18, "y": 19}
hero.move(startRoom)
while True:
    # up
    if hero.isPathClear(hero.pos, {'x': hero.pos.x, 'y': hero.pos.y + num}):
        hero.moveXY(hero.pos.x, hero.pos.y + num)
    elif hero.isPathClear(hero.pos, {'x': hero.pos.x + num, 'y': hero.pos.y}):
        hero.moveXY(hero.pos.x + num, hero.pos.y)
        # right
    elif hero.isPathClear(hero.pos, {'x': hero.pos.x, 'y': hero.pos.y - num}):
        hero.moveXY(hero.pos.x, hero.pos.y - num)
        # down
    elif hero.isPathClear(hero.pos, {'x': hero.pos.x - num, 'y': hero.pos.y}):
        hero.moveXY(hero.pos.x - num, hero.pos.y)
        # left