Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Help! How can I go through Fragile Maze?

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

DBR = 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 + DBR}):
        hero.moveXY(hero.pos.x, hero.pos.y + DBR)
    elif hero.isPathClear(hero.pos, {'x': hero.pos.x + DBR, 'y': hero.pos.y}):
        hero.moveXY(hero.pos.x + DBR, hero.pos.y)
        # right
    elif hero.isPathClear(hero.pos, {'x': hero.pos.x, 'y': hero.pos.y - DBR}):
        hero.moveXY(hero.pos.x, hero.pos.y - DBR)
        # down
    elif hero.isPathClear(hero.pos, {'x': hero.pos.x - DBR, 'y': hero.pos.y}):
        hero.moveXY(hero.pos.x - DBR, hero.pos.y)
        # left
    

Here is my code. What is wrong?

The basic concept of your code is a good start, but you need to consider how to prevent your hero from back tracking. My hero got stuck with your code going up and down when backtracking sent him back up first. Also, instead of making a priority of if’s, make sure you check for all possibilities, then pick one that isn’t going backwards, unless you hit a dead end.