This repository has been archived by the owner on Jan 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.py
262 lines (212 loc) · 9.84 KB
/
grid.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import numpy as np
import random
#Method to create a grid using Randomized Prim's Algorithm
def createGrid(rows,cols):
#Initialize with all 1's denoting all walls
grid = np.ones((rows,cols),dtype=(int))
visited_dict = {}
print(grid[0][0])
unvisited = []
walls = []
#Start with a random cell
x_random = int(random.random()*rows)
y_random = int(random.random()*cols)
#Make the values of all cells 3 -> Denoting all cells are unvisited in the beginning
for i in range(rows):
for j in range(cols):
grid[i][j] = 3
#Make sure we don't start at the edge/boundary of the grid
if x_random == 0:
x_random += 1
if x_random == rows-1:
x_random -= 1
if y_random == 0:
y_random += 1
if y_random == cols-1:
y_random -= 1
#Make this (x_random,y_random) as an open path
grid[x_random][y_random] = 0
#Add the surrounding cells to the walls list
walls.append((x_random, y_random - 1))
walls.append((x_random,y_random + 1))
walls.append((x_random - 1, y_random))
walls.append((x_random + 1, y_random))
grid[x_random][y_random - 1] = 1
grid[x_random][y_random + 1] = 1
grid[x_random - 1][y_random] = 1
grid[x_random + 1][y_random - 1] = 1
while(walls):
#Pick a randoim wall
r_wall = walls[int(random.random()*len(walls))-1]
# Check if it is a left wall
if (r_wall[1] != 0):
if (grid[r_wall[0]][r_wall[1]-1] == 3 and grid[r_wall[0]][r_wall[1]+1] == 0):
# Find number of surrounding cells
s_cells = surroundingCells(grid,r_wall)
if (s_cells < 2):
# Denote the new path
grid[r_wall[0]][r_wall[1]] = 0
# Mark the new walls
# Upper cell
if (r_wall[0] != 0):
if (grid[r_wall[0]-1][r_wall[1]] != 0):
grid[r_wall[0]-1][r_wall[1]] = 1
if ((r_wall[0]-1, r_wall[1]) not in walls):
walls.append((r_wall[0]-1, r_wall[1]))
# Bottom cell
if (r_wall[0] != rows-1):
if (grid[r_wall[0]+1][r_wall[1]] != 0):
grid[r_wall[0]+1][r_wall[1]] = 1
if ((r_wall[0]+1, r_wall[1]) not in walls):
walls.append((r_wall[0]+1, r_wall[1]))
# Leftmost cell
if (r_wall[1] != 0):
if (grid[r_wall[0]][r_wall[1]-1] != 0):
grid[r_wall[0]][r_wall[1]-1] = 1
if ((r_wall[0], r_wall[1]-1) not in walls):
walls.append((r_wall[0], r_wall[1]-1))
# Delete wall
for wall in walls:
if (wall[0] == r_wall[0] and wall[1] == r_wall[1]):
walls.remove(wall)
continue
# Check if it is an upper wall
if (r_wall[0] != 0):
if (grid[r_wall[0]-1][r_wall[1]] == 3 and grid[r_wall[0]+1][r_wall[1]] == 0):
#Find number of surrounding cells
s_cells = surroundingCells(grid,r_wall)
if (s_cells < 2):
# Denote the new path
grid[r_wall[0]][r_wall[1]] = 0
#Mark new walls
# Upper cell
if (r_wall[0] != 0):
if (grid[r_wall[0]-1][r_wall[1]] != 0):
grid[r_wall[0]-1][r_wall[1]] = 1
if ((r_wall[0]-1, r_wall[1]) not in walls):
walls.append((r_wall[0]-1, r_wall[1]))
# Leftmost cell
if (r_wall[1] != 0):
if (grid[r_wall[0]][r_wall[1]-1] != 0):
grid[r_wall[0]][r_wall[1]-1] = 1
if ((r_wall[0], r_wall[1]-1) not in walls):
walls.append((r_wall[0], r_wall[1]-1))
# Rightmost cell
if (r_wall[1] != cols-1):
if (grid[r_wall[0]][r_wall[1]+1] != 0):
grid[r_wall[0]][r_wall[1]+1] = 1
if ((r_wall[0], r_wall[1]+1) not in walls):
walls.append((r_wall[0], r_wall[1]+1))
# Delete wall
for wall in walls:
if (wall[0] == r_wall[0] and wall[1] == r_wall[1]):
walls.remove(wall)
continue
# Check the bottom wall
if (r_wall[0] != rows-1):
if (grid[r_wall[0]+1][r_wall[1]] == 3 and grid[r_wall[0]-1][r_wall[1]] == 0):
s_cells = surroundingCells(grid,r_wall)
if (s_cells < 2):
# Denote the new path
grid[r_wall[0]][r_wall[1]] = 0
# Mark the new walls
if (r_wall[0] != cols-1):
if (grid[r_wall[0]+1][r_wall[1]] != 0):
grid[r_wall[0]+1][r_wall[1]] = 1
if ((r_wall[0]+1, r_wall[1]) not in walls):
walls.append((r_wall[0]+1, r_wall[1]))
if (r_wall[1] != 0):
if (grid[r_wall[0]][r_wall[1]-1] != 0):
grid[r_wall[0]][r_wall[1]-1] = 1
if ((r_wall[0], r_wall[1]-1) not in walls):
walls.append((r_wall[0], r_wall[1]-1))
if (r_wall[1] != cols-1):
if (grid[r_wall[0]][r_wall[1]+1] != 0):
grid[r_wall[0]][r_wall[1]+1] = 1
if ((r_wall[0], r_wall[1]+1) not in walls):
walls.append((r_wall[0], r_wall[1]+1))
# Delete wall
for wall in walls:
if (wall[0] == r_wall[0] and wall[1] == r_wall[1]):
walls.remove(wall)
continue
# Check the right wall
if (r_wall[1] != cols-1):
if (grid[r_wall[0]][r_wall[1]+1] == 3 and grid[r_wall[0]][r_wall[1]-1] == 0):
s_cells = surroundingCells(grid,r_wall)
if (s_cells < 2):
# Denote the new path
grid[r_wall[0]][r_wall[1]] = 0
# Mark the new walls
if (r_wall[1] != cols-1):
if (grid[r_wall[0]][r_wall[1]+1] != 0):
grid[r_wall[0]][r_wall[1]+1] = 1
if ((r_wall[0], r_wall[1]+1) not in walls):
walls.append((r_wall[0], r_wall[1]+1))
if (r_wall[0] != rows-1):
if (grid[r_wall[0]+1][r_wall[1]] != 0):
grid[r_wall[0]+1][r_wall[1]] = 1
if ((r_wall[0]+1, r_wall[1]) not in walls):
walls.append((r_wall[0]+1, r_wall[1]))
if (r_wall[0] != 0):
if (grid[r_wall[0]-1][r_wall[1]] != 0):
grid[r_wall[0]-1][r_wall[1]] = 1
if ((r_wall[0]-1, r_wall[1]) not in walls):
walls.append((r_wall[0]-1, r_wall[1]))
# Delete wall
for wall in walls:
if (wall[0] == r_wall[0] and wall[1] == r_wall[1]):
walls.remove(wall)
continue
# Delete the wall from the list
for wall in walls:
if (wall[0] == r_wall[0] and wall[1] == r_wall[1]):
walls.remove(wall)
# Mark the unvisited cells as walls
for i in range(0, rows):
for j in range(0, cols):
if(grid[i][j] == 3):
grid[i][j] = 1
# Set start and end
for i in range(0, cols):
if (grid[1][i] == 0):
grid[0][i] = 0
break
for i in range(cols - 1, 0, -1):
if(grid[rows - 2][i] == 0):
grid[rows-1][i] = 0
break
return grid
#Find the number of surrounding cells
def surroundingCells(grid,r_wall):
count = 0
#If Up Cell is free
if (grid[r_wall[0]-1][r_wall[1]] == 0):
count += 1
#If down cell is free
if(grid[r_wall[0]+1][r_wall[1]] == 0):
count +=1
#If left cell is free
if (grid[r_wall[0]][r_wall[1]-1] == 0):
count +=1
#If right cell is free
if (grid[r_wall[0]][r_wall[1]+1] == 0):
count += 1
return count
#Create the text file of grid from Grid Matrix
def create_file_from_grid(grid):
with open('/Users/zeeshanahsan/Code/CS520Final_exam/ZeeGrid.txt', 'w') as f:
for i in range(len(grid)):
for j in range(len(grid[0])):
#If wall then write 'X'
if(grid[i][j] == 1):
f.write('X')
#If free cell then write '_'
elif(grid[i][j] == 0):
f.write('_')
#Go to new line
f.write('\n')
#Driver code
grid = createGrid(19,19)
print(grid)
create_file_from_grid(grid)