Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am creating a maze solver program in Python. I am able to get the maze to print before the solution but I am unable to get it to print showing the solution to the maze. I am not receiving any errors so far.

What I have tried:

#read MAZE
import sys

mazeFile = open("maze01.txt", 'r')

tokens = mazeFile.readline().split()
rows = int(tokens[0])
cols = int(tokens[1])
enterRow = int(tokens[2])
enterCol = int(tokens[3])
exitRow = int(tokens[4])
exitCol = int(tokens[5])


def readMaze(maze, filename):               #read maze file
    mazeFile = open(filename, "r")
    columns = mazeFile.readlines()
    for j in columns:                   #make list of lists
        j = j.strip()
        i = [n for n in j]
        maze.append(i)

def solve(maze, start, r, c):
    if c > len(maze) or r > len(maze[c]):
        return False

    if maze[c][r] == 'E':
        return True

    if maze[c][r] != ' ':
        return False

    bc = 'o'

    maze[c,r] = bc

    if maze.solveMaze(r + 1, c) == True:
        bc = '>'
    elif maze.solveMaze(r, c + 1) == True:
        bc = 'v'
    elif maze.solveMaze(r - 1, c) == True:
        bc = '<'
    elif maze.solveMaze(r, c - 1) == True:
        bc = '^'
    else:

        bc = ' '

    maze[c,r] = bc
    return (bc != ' ')


maze = [ ]
readMaze(maze, "maze01.txt")

maze.pop(0)

maze[enterRow][enterCol] = 'S'
maze[exitRow][exitCol] = 'E'

for r in range(rows):
        for c in range(cols):
            if maze[r][c] == 'S':
                start = maze[enterRow][enterCol]
            if maze[r][c] == 'E':
                end = maze[exitRow][exitCol]


solve(maze, start, r, c)
start = maze[enterRow][enterCol]
end = maze[exitRow][exitCol]

print("Here's the maze: \n")
for sublist in maze:                #print maze line by line
    print(''.join(sublist))




#E###
# # #
# # #
#   #
#S###
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900