0

I am attempting to replicate this very solution: Maze solving with python

My only goal is to use carrots to display the direction of the solution path within the maze. I have already solved the maze but the path consists of one character(2). I would like to replace it with the appropriate carrots ('>','v','<','^')

My issue is with the:

if move(x + 1, y) == True:

Because I am calling the recursive function as part of the IF condition, it keeps looping through without acknowledging the "== True" condition - so it's not returning the "True" statement and ignoring my condition to replace with the appropriate carrot.

How can I force it to process the entire IF condition, including the "== True".

Example output: NOTE: It's skipping the print statements (e.g: "EAST IS TRUE) in the IF condition. BUT - it is recognizing the else statement("EAST NOT TRUE).

LOOP START:  X: 54 Y: 25
LOOP START:  X: 55 Y: 25
LOOP START:  X: 56 Y: 25
LOOP START:  X: 57 Y: 25
LOOP START:  X: 58 Y: 25
LOOP START:  X: 59 Y: 25
LOOP START:  X: 60 Y: 25
That is wall X: 60 Y: 25
EAST NOT TRUE X: 59 Y: 25
LOOP START:  X: 59 Y: 26
LOOP START:  X: 60 Y: 26
That is wall X: 60 Y: 26
...etc

My code:

def move(x, y):
    global FoundWayOut
    if FoundWayOut:
        return

    print('LOOP START: ' + ' X: ' + str(x) + ' Y: ' + str(y))


    if (x < 0 or x > width - 1 or y < 0 or y > height - 1):
        print('Out of bounds' + ' X: ' + str(x) + ' Y: ' + str(y)); return
    if (myMaze[y][x] == 7):
        FoundWayOut = True
        print('Got it!' + ' X: ' + str(x) + ' Y: ' + str(y)); return
    if (myMaze[y][x] == 1):
        print('That is wall' + ' X: ' + str(x) + ' Y: ' + str(y)); return
    if (myMaze[y][x] == 2):
        print('Was here before' + ' X: ' + str(x) + ' Y: ' + str(y)); return
    myMaze[y][x] = 2

    if move(x + 1, y):
        myMaze[y][x] = ">"
        print("EAST IS TRUE")
    else:
        print("EAST NOT TRUE" + " X: " + str(x) + " Y: " + str(y))

    if move(x, y + 1):
        myMaze[y][x] = "v"
        print("SOUTH IS TRUE")
    else:
        print("SOUTH NOT TRUE" + " X: " + str(x) + " Y: " + str(y))

    if move(x - 1, y):
        myMaze[y][x] = "<"
        print("WEST IS TRUE")
    else:
        print("WEST NOT TRUE" + " X: " + str(x) + " Y: " + str(y))

    if move(x, y - 1):
        myMaze[y][x] = "^"
        print("NORTH IS TRUE")
    else:
        print("NORTH NOT TRUE" + " X: " + str(x) + " Y: " + str(y))

The result I would expect would be for the character to be replaced IF the condition is true.

JRob921
  • 1
  • 1
  • I don't see a `return False` anywhere. Also, I think you just need `if move(x + 1, y):` – Sheldore Feb 01 '19 at 23:51
  • When I added a return False is exited the loop entirely and failed to solve the maze. I removed the return True and False all together. Still have the same issue...updated my main code above. – JRob921 Feb 04 '19 at 16:44
  • if move(x + 1, y): myMaze[y][x] = ">" print("EAST IS TRUE") else: print("EAST NOT TRUE" + " X: " + str(x) + " Y: " + str(y)) – JRob921 Feb 04 '19 at 16:47
  • @Bazingaa - sorry forgot to tag you in the edit above. Tried the change to no avail. Thank you for any insight into why this condition isn't processing. Much appreciated. – JRob921 Feb 04 '19 at 17:07

0 Answers0