-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.py
More file actions
227 lines (198 loc) · 6.88 KB
/
loader.py
File metadata and controls
227 lines (198 loc) · 6.88 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import csv
class Cell:
'Class for our cell should contain a value and a bool'
def __init__ (self, value):
self.value = value
self.visited = False
class Labyrinth:
'Class for our labyrinth data structure'
def __init__(self, seed, width, height, entrance, exit, maze):
self.seed = seed
self.width = width
self.height = height
self.entrance = entrance
self.exit = exit
self.maze = maze
def showLab(self):
print (f"Lab has seed {self.seed}")
print(f"The height is {self.height} the width is {self.width}")
print (f"The lab entrance is located at {self.entrance}")
print (f"The lab exit is located at {self.exit}")
for i, sublist in enumerate(self.maze):
for j, item in enumerate(sublist):
print(f"{item.value}")
#print(f"The bool located at [{i}][{j}] is {item.visited}")
def __getitem__(self, index):
return self.maze[index]
def load_prop(fileName):
with open(fileName) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=" ")
line_count = 0
maze = []
for row in csv_reader:
if line_count == 0:
print ("First line of the file is the seed")
print (row)
line_count += 1
seed = int(row[0])
print (seed)
elif line_count == 1:
print(row)
print(f"The height is {row[0]} the width is {row[1]}")
height = int(row[0])
width = int(row[1])
line_count += 1
elif line_count == 2:
print (row)
entrance = int(row[0])
exit = int(row[1])
line_count += 1
else:
#print ("Time to load the cells")
rowList = []
#print (row)
values = row[0].split(',')
for value in values:
newCell = Cell(value)
#print (value)
rowList.append(newCell)
maze.append(rowList)
return seed, height, width, entrance, exit, maze
def number_to_walls(number):
mask = 15
extract = number&mask
return format(int(bin(extract)[2:],2),'04b')
def dfs(maze,startrow,startcol):
global solutionRute
if startcol == (maze.exit-1) and startrow == (len(maze.maze)-1):
print(f"Exit found at [{(len(maze.maze)-1)}][{(maze.exit-1)}]")
maze[startrow][startcol].visited = True
#solutionRute = generateSolution(maze)
print(solutionRute)
return True #we found the exit
if (maze[startrow])[startcol].visited:
print('Weve been here, stopping')
return False #We already been here
maze[startrow][startcol].visited = True
solutionRute.append((startrow,startcol))
print(f"The value of [{startrow}][{startcol}] is {(maze[startrow][startcol].value)} and the boolean is {maze[startrow][startcol].visited}")
walls = number_to_walls(int(maze.maze[startrow][startcol].value))
if int(walls[0]) == 0 and maze.maze[startrow][startcol-1].visited == False:
print(startrow,startcol)
print("Going West")
if dfs(maze, startrow, startcol-1):
return True
if int(walls[1]) != 1 and maze.maze[startrow][startcol+1].visited == False:
print(startrow,startcol)
print("Going East")
if dfs(maze, startrow, startcol+1):
return True
if int(walls[2]) != 1 and maze.maze[startrow+1][startcol].visited == False:
print(startrow,startcol)
print("Going South")
if dfs(maze, startrow+1, startcol):
return True
if int(walls[3]) != 1 and maze.maze[startrow-1][startcol].visited == False:
print(startrow,startcol)
print("Going North")
if dfs(maze, startrow-1, startcol):
return True
'''
found = dfs(maze, startrow+1, startcol) or \
dfs(maze, startrow, startcol-1) or \
dfs(maze, startrow, startcol+1) or \
dfs(maze, startrow-1, startcol)
return found
'''
return False
def pruneMaze(maze):
count = 0
print("initiating pruning")
for rowCounter,row in enumerate(maze.maze):
for colCounter, cell in enumerate(row):
if int(cell.value) == 7:
cell.value = 15
maze.maze[rowCounter][colCounter-1].value = int(maze.maze[rowCounter][colCounter-1].value)+4
count = count + 1
if int(cell.value) == 11:
cell.value = 15
maze.maze[rowCounter][colCounter+1].value = int(maze.maze[rowCounter][colCounter+1].value)+8
count = count + 1
if int(cell.value) == 13:
cell.value = 15
maze.maze[rowCounter+1][colCounter].value = int(maze.maze[rowCounter+1][colCounter].value)+1
count = count + 1
if int(cell.value) == 14:
cell.value = 15
maze.maze[rowCounter-1][colCounter].value = int(maze.maze[rowCounter-1][colCounter].value)+2
count = count + 1
while count:
print(f"Pruning complete. # of nodes pruned {count}")
count = pruneMaze(maze)
return count
def iterativ_dfs(maze, row, col):
solutions = []
stack = []
startrow = 0
startcol = maze.entrance-1
goalrow = maze.height-1
goalcol = maze.exit-1
stack.append((startrow,startcol,'start'))
solutions.append((startrow,startcol,'start'))
print(f"initiating iterative dfs with entrance [0][{startcol}] and exit [{goalrow}][{goalcol}]")
#modify startnode to wall north off
maze.maze[startrow][startcol].value = int(maze.maze[startrow][startcol].value)+1
while stack:
row,col, direction = stack.pop()
print(f"Entering row {row} col {col}")
cell = maze.maze[row][col]
if cell.visited != True:
cell.visited = True
#solutions.append((row,col,direction))
if row == goalrow and col == goalcol:
print("Congratulations you found the exit")
solutions.append((row,col,'exit'))
return solutions
walls = number_to_walls(int(maze.maze[row][col].value))
print(walls)
if int(walls[0]) == 0:
solutions.append((row,col,direction))
stack.append((row,col-1,'west'))
if int(walls[1]) == 0:
solutions.append((row,col,direction))
stack.append((row,col+1,'east'))
if int(walls[2]) == 0:
solutions.append((row,col,direction))
stack.append((row+1,col,'south'))
if int(walls[3]) == 0:
solutions.append((row,col,direction))
stack.append((row-1,col,'north'))
solutions.remove((row,col,direction))
def generateSolution(maze):
solution = []
for row in maze.maze:
rowList = []
for cell in row:
if cell.visited:
rowList.append(1)
else:
rowList.append(0)
solution.append(rowList)
return solution
#def printSolution()
solutionRute = []
myMaze = Labyrinth(*load_prop("5x5_32736.dat"))
#myMaze.showLab()
#dfs(myMaze,0, (myMaze.entrance-1))
pruneMaze(myMaze)
solution = iterativ_dfs(myMaze, 0,myMaze.entrance-1)
print(solution)
#print(list(dict.fromkeys(solution)))
#print(solution)
#dfs(myMaze,0, (myMaze.entrance-1))
#number_to_walls(9)
#mySolution = generateSolution(myMaze)
#for row in solutionRute:
# print (row)
#base case
#end condition