-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMazeGenerator.py
75 lines (63 loc) · 2.39 KB
/
MazeGenerator.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
import random
from Maze import Maze
class MazeGenerator(object):
def __init__(self, w, h):
self.m_W = max(1, w)
self.m_H = max(1, h)
self.Generate()
def InitSet(self):
self.m_LastSetNumber = 0
self.m_Set = []
for i in range(self.m_W):
self.m_LastSetNumber += 1
self.m_Set.append(self.m_LastSetNumber)
def DestroyLeftWalls(self, y):
for x in range(1, self.m_W):
if(self.m_Set[x] != self.m_Set[x - 1] and random.choice([True, False])):
self.SetWay(x - 1, y, x, y)
def DestroyDownLines(self, y):
hasDownWay = False
for x in range(self.m_W):
if(random.choice([True, False])):
self.SetWay(x, y, x, y + 1)
hasDownWay = True
if(x == self.m_W - 1 or self.m_Set[x] != self.m_Set[x + 1]):
if(hasDownWay):
hasDownWay = False
else:
self.SetWay(x, y, x, y + 1)
def UpdateSet(self, y):
for x in range(self.m_W):
if(not self.HasPath(x, y, x, y + 1)):
self.m_LastSetNumber += 1
self.m_Set[x] = self.m_LastSetNumber
def ProcessLastLine(self):
y = self.m_H - 1
for x in range(1, self.m_W):
if(self.m_Set[x] != self.m_Set[x-1]):
self.SetWay(x, y, x - 1, y)
def Generate(self):
self.m_Maze = Maze(self.m_W, self.m_H)
self.InitSet()
for y in range(self.m_H - 1):
self.DestroyLeftWalls(y)
self.DestroyDownLines(y)
self.UpdateSet(y)
self.ProcessLastLine()
return self.m_Maze
def MergeSet(self, x1, x2):
setNumToReplace = max(self.m_Set[x1], self.m_Set[x2])
newSetNum = min(self.m_Set[x1], self.m_Set[x2])
for x in range(self.m_W):
if(self.m_Set[x] == setNumToReplace):
self.m_Set[x] = newSetNum
def SetWay(self, x1, y1, x2, y2):
self.m_Maze.SetWay(self.m_Maze.GetVertex(x1, y1), self.m_Maze.GetVertex(x2, y2))
self.MergeSet(x1, x2)
def HasPath(self, x1, y1, x2, y2):
return self.m_Maze.HasPath(self.m_Maze.GetVertex(x1, y1), self.m_Maze.GetVertex(x2, y2))
def GetMaze(self):
return self.m_Maze
if __name__ == "__main__":
mazeGenerator = MazeGenerator(10, 10)
mazeGenerator.GetMaze().Print()