-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.py
45 lines (33 loc) · 1.12 KB
/
graph.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
class Graph:
def __init__(self, width, height):
self.width = width
self.height = height
self.walls = []
self.solid_ground = []
self.rocky_ground = []
self.sandy_ground = []
self.marsh_ground = []
self.rewards = []
self.weights = {}
def in_bounds(self, id):
(x, y) = id
return 0 <= x < self.width and 0 <= y < self.height
def passable(self, id):
return id not in self.walls or id in self.rewards
def neighbors(self, id):
(x, y) = id
results = [(x+1, y), (x, y-1), (x-1, y), (x, y+1)]
if (x + y) % 2 == 0:
results.reverse() # aesthetics
results = filter(self.in_bounds, results)
results = filter(self.passable, results)
return results
def cost(self, from_node, to_node):
return self.weights.get(to_node)
def validate_state(self, state):
(x, y) = state
if state in self.walls:
return False, False
elif x <= 0 or x >= self.width or y <= 0 or y >= self.height:
return False, True
return True, False