forked from mhahsler/CS7320-AI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze_helper.py
85 lines (59 loc) · 2.16 KB
/
maze_helper.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# +
# Code by Nicholas Crothers modified and expanded by M. Hahsler
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import colors
# -
def parse_maze(maze_str):
"""Convert a maze as a string into a 2d numpy array"""
maze = maze_str.split('\n')
maze = np.array([[tile for tile in row] for row in maze if len(row) > 0])
return maze
# This is modified code I found on StackOverflow, at this link
# https://stackoverflow.com/questions/43971138/python-plotting-colored-grid-based-on-values
def show_maze(maze):
"""display a maze (numpy array)"""
cmap = colors.ListedColormap(['white', 'black', 'blue', 'green', 'red', 'gray', 'orange'])
bounds = [0, 1, 2, 3, 4, 5, 6, 7]
# make a deep copy first so the original maze is not changed
maze = np.copy(maze)
# Converts all tile types to integers
maze[maze == ' '] = 0
maze[maze == 'X'] = 1 # wall
maze[maze == 'S'] = 2 # start
maze[maze == 'G'] = 3 # goal
maze[maze == 'P'] = 4 # position/final path
maze[maze == '.'] = 5 # visited squares
maze[maze == 'F'] = 6 # frontier
# Converts all string values to integers
maze = maze.astype(np.int)
norm = colors.BoundaryNorm(bounds, cmap.N)
fig, ax = plt.subplots()
ax.imshow(maze, cmap=cmap, norm=norm)
plt.show()
# +
# Example: Display all mazes
def run_example():
for type in ['small', 'medium', 'large', 'open']:
f = open(f"{type}_maze.txt", "r")
maze_str = f.read()
print(maze_str)
maze = parse_maze(maze_str)
print(maze)
show_maze(maze)
#run_example()
# -
def find_pos(maze, what = "S"):
"""
Find start/goal in a maze. Caution: there is no error checking
Parameters:
maze: a array with characters
what: the letter to be found ('S' for start and 'G' for goal)
Returns:
array: [x, y] for the found position.
"""
pos = np.where(maze == what)
return([pos[0][0], pos[1][0]])
def look(maze, pos):
"""look at the label of a square with the position as an array of the form [x, y]."""
return(maze[pos[0], pos[1]])