I have made the following code in Python which opens the following text file. The first line is the size of the maze file, the second line is the starting point though on the maze it is actually at row 9, column 1 and the third line is the ending point though on the maze it is actually at row 1, column 19.
5 10
1 1
5 10
---------------------
| | | |
|-+-+-+ +-+-+ + + +-|
| | | | | |
| +-+-+ + +-+-+-+ + |
| | | | | |
|-+-+ + + + +-+ +-+-|
| | |
|-+ +-+-+-+-+-+ +-+ |
| | | |
---------------------
Most of the functions work just fine and do what they are supposed to do. What I am having trouble with is solving the maze. The way my maze_solution function is set up right now fills up the whole maze with asterisks, I would like only the path to be drawn with asterisks and I am not sure how to do so.
class Maze:
def __init__(self, file):
self.file = file
self.maze = []
data = self.file.readlines()
data.pop(0)
data.pop(0)
data.pop(0)
for line in data:
if line[-1] == '\n':
self.maze.append(list(line[:-1]))
else:
self.maze.append(list(line))
def print_maze(self):
for i in range(len(self.maze)):
for j in range(len(self.maze[0])):
print(self.maze[i][j], end='')
print()
def maze_points(self):
for item in self.maze:
rows = len(self.maze)
columns = len(item)
self.start = (rows - 2, 1)
self.end = (1, columns - 2)
def maze_solution(self, row, col):
if row == self.end[0] and col == self.end[1]:
self.maze[row][col] = '*'
return True
elif self.maze[row][col] == ' ':
self.maze[row][col] = '*'
if self.maze_solution(row, col + 1) or \
self.maze_solution(row + 1, col) or \
self.maze_solution(row, col - 1) or \
self.maze_solution(row - 1, col):
self.maze[row][col] = ' '
return False
return True
elif self.maze[row][col] in '-|+':
return False
def maze_file():
file = open('maze.txt', 'r')
maze = Maze(file)
maze.maze_points()
row = maze.start[0]
col = maze.start[1]
maze.maze_solution(row, col)
maze.print_maze()