-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maze.py
58 lines (50 loc) · 1.5 KB
/
Maze.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
import json
class Maze(object):
def __init__(self, w, h):
self.m_W = w
self.m_H = h
self.m_Maze = {}
def SetWay(self, f, t):
self.SetOrderedWay(f, t)
self.SetOrderedWay(t, f)
def SetOrderedWay(self, f, t):
f = str(f)
if(not f in self.m_Maze):
self.m_Maze[f] = []
self.m_Maze[f].append(t)
def HasPath(self, f, t):
f = str(f)
if(not f in self.m_Maze):
return False
return t in self.m_Maze[f]
def GetVertex(self, x, y):
return self.m_W * y + x
def Print(self):
str = ''
for x in range(self.m_W):
str += ' __'
print(str)
for y in range(self.m_H):
str = ''
for x in range(self.m_W):
if(x != 0 and self.HasPath(self.GetVertex(x, y), self.GetVertex(x - 1, y))):
str += ' '
else:
str += '|'
if(self.HasPath(self.GetVertex(x, y), self.GetVertex(x, y + 1))):
str += ' '
else:
str += '__'
print(str + '|')
print('')
def ToJSON(self):
return json.dumps({
'Width': self.m_W,
'Height': self.m_H,
'Graph': self.m_Maze
})
def FromJSON(self, jsonString):
data = json.loads(jsonString)
self.m_W = data['Width']
self.m_H = data['Height']
self.m_Maze = data['Graph']