-
Notifications
You must be signed in to change notification settings - Fork 1
/
grid_generator.py
109 lines (94 loc) · 3.11 KB
/
grid_generator.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
import numpy
import random
import pickle
import os.path
import state
LENGTH = 101
num_grids = 50
grid_list = []
#this sets isPath and parent to False for the whole grid
def reset(grid):
for i in grid:
for j in i:
j.isPath = False
j.parent = None
return grid
def generate_neighbors(cell):
neighbors = []
if(cell[0] - 1 >= 0):
neighbors.append([cell[0] - 1, cell[1]])
if(cell[0] + 1 < LENGTH):
neighbors.append([cell[0] + 1, cell[1]])
if(cell[1] - 1 >= 0):
neighbors.append([cell[0], cell[1] - 1])
if(cell[1] + 1 < LENGTH):
neighbors.append([cell[0], cell[1] + 1])
return neighbors
def generate_grid():
global grid_list
grid = [[state.State(i, j) for i in range(LENGTH)] for j in range(LENGTH)]
visited = []
stack = []
current_cell = [random.randint(0, LENGTH - 1), random.randint(0, LENGTH - 1)]
while(len(visited) != (LENGTH ** 2)):
#mark current cell as visited
if(current_cell not in visited):
visited.append(current_cell)
#generate neighbors of current cell
neighbors = generate_neighbors(current_cell)
neighbors = [n for n in neighbors if n not in visited]
# if multiple neighbors choose randomly, else choose 1st one, else backtrack, else go to a random cell
if(len(neighbors) > 1):
next_cell = neighbors[random.randint(0, len(neighbors) - 1)]
if(random.randint(0, 9) > 2):
stack.append(current_cell)
else:
grid[current_cell[0]][current_cell[1]].setBlocked()
elif(len(neighbors) == 1):
next_cell = neighbors[0]
if(random.randint(0, 9) > 2):
stack.append(current_cell)
else:
grid[current_cell[0]][current_cell[1]].setBlocked()
elif(len(stack) != 0):
next_cell = stack.pop()
else:
next_cell = [random.randint(0, LENGTH - 1), random.randint(0, LENGTH - 1)]
if(next_cell in visited and len(visited) != (LENGTH ** 2)):
while(next_cell in visited):
next_cell = [random.randint(0, LENGTH - 1), random.randint(0, LENGTH - 1)]
if(random.randint(0, 9) > 2):
stack.append(current_cell)
else:
grid[current_cell[0]][current_cell[1]].setBlocked()
current_cell = [next_cell[0], next_cell[1]]
if(len(visited) % 1000 == 0):
print('STEP # ' + str(len(visited)))
return grid
def grid_in_list(grid, grid_list):
return next((True for elem in grid_list if numpy.array_equal(elem, grid)), False)
def generate_grid_list():
global grid_list
global num_grids
while(len(grid_list) < num_grids):
print('GRID # ' + str(len(grid_list)))
grid = generate_grid()
if(not grid_in_list(grid, grid_list)):
grid_list.append(grid)
def save_grid_list():
global grid_list
output_file = open('grids', 'wb')
pickle.dump(grid_list, output_file)
output_file.close()
def load_grid_list():
global grid_list
if(os.path.isfile('grids')):
gridfile = open('grids', 'rb')
grid_list = pickle.load(gridfile)
gridfile.close()
def get_grid_list():
global grid_list
return grid_list
def get_random_grid():
global grid_list
return grid_list[random.randint(0, len(grid_list) - 1)]