-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBensPF.py
More file actions
150 lines (119 loc) · 4.68 KB
/
BensPF.py
File metadata and controls
150 lines (119 loc) · 4.68 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import queue
# Off campus housing map
def createOrigMaze():
maze = []
# 0 1 2 3 4 5 6 7 8
maze.append(["#", "#", "#", "#", "#", "O", "#", "#", "#"]) # 0
maze.append(["#", " ", " ", " ", " ", " ", " ", " ", "#"]) # 1
maze.append(["#", " ", "#", "#", " ", " ", "#", " ", "#"]) # 2
maze.append(["#", " ", "#", " ", " ", " ", "#", " ", "#"]) # 3
maze.append(["#", " ", "#", " ", "#", " ", "#", " ", "#"]) # 4
maze.append(["#", " ", "#", " ", "#", " ", "#", " ", "#"]) # 5
maze.append(["#", " ", "#", " ", "#", " ", "#", "#", "#"]) # 6
maze.append(["#", " ", " ", " ", " ", " ", " ", " ", "#"]) # 7
maze.append(["#", "#", "X", "#", "#", "#", "#", "#", "#"]) # 8
return maze
def createEmptyMaze():
maze = []
maze.append(["#", "#", "#", "#", "#", "O", "#", "#", "#"])
maze.append(["#", " ", " ", " ", " ", " ", " ", " ", "#"])
maze.append(["#", " ", "#", "#", " ", " ", "#", " ", "#"])
maze.append(["#", " ", "#", " ", " ", " ", "#", " ", "#"])
maze.append(["#", " ", "#", " ", "#", " ", "#", " ", "#"])
maze.append(["#", " ", "#", " ", "#", " ", "#", " ", "#"])
maze.append(["#", " ", "#", " ", "#", " ", "#", "#", "#"])
maze.append(["#", " ", " ", " ", " ", " ", " ", " ", "#"])
maze.append(["#", "#", "X", "#", "#", "#", "#", "#", "#"])
return maze
# Pass the start location to this function
def fillFirstValue(coordinate):
mazePaths[coordinate[0]][coordinate[1]] = 0
# This function should only be passed valid, empty coordinates
def fillMazeSpot(origCoordinate, emptyCoordinate):
mazePaths[emptyCoordinate[0]][emptyCoordinate[1]] = mazePaths[origCoordinate[0]][origCoordinate[1]] + 1
def isEnd(coordinate, neighborCoordinate):
x = coordinate[0]
y = coordinate[1]
neighborX = neighborCoordinate[0]
neighborY = neighborCoordinate[1]
end = 0
# Check for end
if mazeOrig[x + neighborX][y + neighborY] == "X":
# Access solutionLocation as a global variable (You get an error if
# you don't for some reason)
global solutionLocation
solutionLocation += [x + neighborX]
solutionLocation += [y + neighborY]
end = 1
return end
def isValid(coordinate, neighborCoordinate):
x = coordinate[0]
y = coordinate[1]
neighborX = neighborCoordinate[0]
neighborY = neighborCoordinate[1]
valid = 0
# Check for out of bounds
if (x + neighborX < 0) or (y + neighborY < 0) or (x + neighborX > colCount) or (y + neighborY > rowCount):
# Keep valid at 0, it's out of bounds
pass
# Check for wall
elif mazeOrig[x+neighborX][y+neighborY] == "#":
# Keep valid at 0, it's a wall
pass
# Check if the path maze has already been filled in at this coordinate
elif mazePaths[x+neighborX][y+neighborY] != " ":
# Keep valid at 0, it's already been filled in
pass
# Anything else should be a blank space that needs filled in
else:
valid = 1
return valid
def findNeighbors(coordinate):
neighborList = []
x = coordinate[0]
y = coordinate[1]
for neighborRelativeVal in neighborRelativeVals:
if isEnd(coordinate, neighborRelativeVal):
solutionLocation = [[x+neighborRelativeVal[0], y+neighborRelativeVal[1]]]
if isValid(coordinate, neighborRelativeVal):
neighborList += [[x+neighborRelativeVal[0], y+neighborRelativeVal[1]]]
return neighborList
# Declare variables
rowCount = 0
colCount = 0
mazeOrig = createOrigMaze()
mazePaths = createEmptyMaze()
startLocation = []
solutionLocation = []
neighborsList = []
# U D R L
neighborRelativeVals = [[-1, 0], [1, 0], [0, 1], [0, -1]]
# Find Start Location
for i, row in enumerate(mazeOrig):
for j, column in enumerate(mazeOrig[i]):
if mazeOrig[i][j] == "O":
startLocation += [i]
startLocation += [j]
# Get amount of rows and cols
for i, row in enumerate(mazeOrig):
for j, column in enumerate(mazeOrig[i]):
if i == 0:
colCount += 1
rowCount += 1
# Check if the user did not provide a start location
if startLocation == []:
print("The start location was not found. Please put a 'O' somewhere in the maze")
exit()
neighborsQ = queue.Queue()
neighborsQ.put(startLocation) # enqueue
fillFirstValue(startLocation)
while solutionLocation == []:
tempCoordinate = neighborsQ.get() # dequeue
neighborsList = findNeighbors(tempCoordinate)
for neighbor in neighborsList:
if solutionLocation != []:
break
else:
fillMazeSpot(tempCoordinate, neighbor)
neighborsQ.put(neighbor)
# print(solutionLocation)