forked from AlexandruValeanu/Mazify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw_maze.py
50 lines (37 loc) · 1.26 KB
/
draw_maze.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from matplotlib import pyplot
from matplotlib import colors
import constants
def draw_maze(maze, path=None, figsize=(20, 10), file_path=None):
if path is None:
path = []
pyplot.figure(figsize=figsize)
color_path(maze, path)
# make a color map of fixed colors
cmap = colors.ListedColormap(['black', 'white', 'red', 'blue'])
bounds = [0, 1, 2, 3, 4]
norm = colors.BoundaryNorm(bounds, cmap.N)
# tell imshow about color map so that only set colors are used
pyplot.imshow(maze.board, interpolation='nearest',
cmap=cmap, norm=norm)
pyplot.xticks([]), pyplot.yticks([])
if file_path is not None:
pyplot.savefig(file_path + ".png")
maze.write_to_file(file_path + '.txt')
else:
pyplot.show()
def ascii_representation(maze):
rep = ''
for i in range(maze.nrows):
for j in range(maze.ncolumns):
if maze.is_wall(i, j):
rep += 'X'
else:
rep += ' '
rep += '\n'
return rep
def color_path(maze, path):
for (x, y) in path:
maze.board[x][y] = constants.RED
if len(path):
maze.board[path[0][0], path[0][1]] = constants.BLUE
maze.board[path[-1][0], path[-1][1]] = constants.BLUE