-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze_generator.py
134 lines (109 loc) · 4.58 KB
/
maze_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
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
import random
import math
class Generator(object):
def __init__(self, startx, starty, sizex=11, sizey=11):
self.Grid = [[0 for i in range(sizex)] for j in range(sizey)]
self.Grid[starty][startx] = 1
self.Frontier = []
self.sizex = sizex
self.sizey = sizey
# print(startx, starty)
self.CalculateFrontier(startx, starty)
# print(self.Frontier)
# print(self.Frontier)
while (len(self.Frontier) > 0):
self.Expand()
# self.AddLoops(math.floor(math.sqrt(sizex*sizey)-1/2))
# print(self.Grid)
# self.PrintGrid()
def PrintGrid(self):
for row in self.Grid:
for cell in row:
if (cell == 0):
print(" ", end=" ")
else:
print("\u25A0", end=" ")
print("\n", end="")
def CalculateFrontier(self, x, y):
if (x >= 0 and x <= self.sizex - 1 and y >= 0 and y <= self.sizey - 1):
if (x - 2 >= 0 and x - 2 <= self.sizex - 1 and self.Grid[y][x - 2] != 1):
if (not [x - 2, y] in self.Frontier):
self.Frontier.append([x - 2, y])
if (x + 2 >= 0 and x + 2 <= self.sizex - 1 and self.Grid[y][x + 2] != 1):
if (not [x + 2, y] in self.Frontier):
self.Frontier.append([x + 2, y])
if (y - 2 >= 0 and y - 2 <= self.sizey - 1 and self.Grid[y - 2][x] != 1):
if (not [x, y - 2] in self.Frontier):
self.Frontier.append([x, y - 2])
if (y + 2 >= 0 and y + 2 <= self.sizey - 1 and self.Grid[y + 2][x] != 1):
if (not [x, y + 2] in self.Frontier):
self.Frontier.append([x, y + 2])
def GetNeighbors(self, x, y):
Neighbors = []
if (x >= 0 and x <= self.sizex - 1 and y >= 0 and y <= self.sizey - 1):
if (x - 2 >= 0):
if (self.Grid[y][x - 2] == 1):
Neighbors.append([x - 2, y])
if (x + 2 <= self.sizex - 1):
if (self.Grid[y][x + 2] == 1):
Neighbors.append([x + 2, y])
if (y - 2 >= 0):
if (self.Grid[y - 2][x] == 1):
Neighbors.append([x, y - 2])
if (y + 2 <= self.sizey - 1):
if (self.Grid[y + 2][x] == 1):
Neighbors.append([x, y + 2])
return Neighbors
def Mid(self, p1, p2):
x = y = 0
# X is equal
if (p1[0] == p2[0]):
x = p1[0]
# Calc y
if (p1[1] > p2[1]):
y = p2[1] + 1
else:
y = p1[1] + 1
# Y is equal
elif (p1[1] == p2[1]):
y = p1[1]
# Calc x
if (p1[0] > p2[0]):
x = p2[0] + 1
else:
x = p1[0] + 1
return [x, y]
def Expand(self):
# Choose a random frontier vertex
FrontierIndex = random.randint(0, len(self.Frontier) - 1)
FE = self.Frontier[FrontierIndex]
Neighbors = self.GetNeighbors(FE[0], FE[1])
NeighborIndex = random.randint(0, len(Neighbors) - 1)
# Calculate the midpoint between the frontier vertex and existing pathway
Mid = self.Mid(FE, Neighbors[NeighborIndex])
x = Mid[0]
y = Mid[1]
# Set the frontier vertex and midpoint to a pathway
self.Grid[FE[1]][FE[0]] = 1
self.Grid[y][x] = 1
# Remove the frontier vertex from the frontier list
self.Frontier.remove(FE)
# Calculate the new frontier vertices
self.CalculateFrontier(FE[0], FE[1])
def AddLoops(self, NumLoops):
for i in range(NumLoops):
# Get a random vertex and one of it's neighbors
p1 = [round(random.randint(0, self.sizex - 1) / 2) * 2, round(random.randint(0, self.sizey - 1) / 2) * 2]
p2 = random.choice(self.GetNeighbors(p1[0], p1[1]))
# Get new vertices if the current ones are already linked
while (self.Grid[self.Mid(p1, p2)[1]][self.Mid(p1, p2)[0]] == 1):
p1 = [round(random.randint(0, self.sizex - 1) / 2) * 2,
round(random.randint(0, self.sizey - 1) / 2) * 2]
p2 = random.choice(self.GetNeighbors(p1[0], p1[1]))
self.Grid[self.Mid(p1, p2)[1]][self.Mid(p1, p2)[0]] = 1
if __name__ == "__main__":
GridSizex = 32
GridSizey = 32
x = math.floor(random.randint(0, GridSizex) / 2) * 2
y = math.floor(random.randint(0, GridSizey) / 2) * 2
Generator(x, y, GridSizex, GridSizey)