/
path.py
70 lines (60 loc) · 2.63 KB
/
path.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
'''
An empty path segment
'''
from constants import *
from element import GameElement
'''
I think there may be a better way to check neighbors than having each Path
object store values for each of its neighbors. For now though, I have ported
your code with identical functionality. -- odd
'''
class Path(GameElement):
def __init__(self, ps, *args, **kwargs):
super(Path, self).__init__(*args, **kwargs)
self.group = ELEMENT_PATH
self.neighbors = {'n': None, 's': None, 'e': None, 'w': None}
self.contents = [] # tile's "inventory"
self.light = 0 # how brightly lit the tile is (0 is unlit)
self.passable = True # can the player be in this square?
# May reimplement functionality of the below variables... -- odd
self.map_checked = False # has this path been map checked?
self.parent_solver = ps
def show_items(self):
for item in self.contents:
pass # PORT: game.doc.addChild(contents[i]);
def find_neighbors(self):
# West
if self.x > 0:
if self.parent_solver.grid[self.x-1][self.y].group == ELEMENT_WALL:
self.neighbors['w'] = None
else:
self.neighbors['w'] = self.parent_solver.grid[self.x-1][self.y]
# North
if self.y > 0:
if self.parent_solver.grid[self.x][self.y-1].group == ELEMENT_WALL:
self.neighbors['n'] = None
else:
self.neighbors['n'] = self.parent_solver.grid[self.x][self.y-1]
# East
if (self.x + 1) < len(self.parent_solver.grid):
if self.parent_solver.grid[self.x+1][self.y].group == ELEMENT_WALL:
self.neighbors['e'] = None
else:
self.neighbors['e'] = self.parent_solver.grid[self.x+1][self.y]
# South
if (self.y + 1) < len(self.parent_solver.grid[self.x]):
if self.parent_solver.grid[self.x][self.y+1].group == ELEMENT_WALL:
self.neighbors['s'] = None
else:
self.neighbors['s'] = self.parent_solver.grid[self.x][self.y+1]
def grab_neighbors(self, path, step=0):
self.map_checked = True
step += 1
print 'grab_neighbors() step print:', step # DEBUG
if self.contents[0].group == ELEMENT_GOAL:
print 'Found goal at step', step
self.parent_solver.path_lengths.append(step)
for d in DIRECTIONS:
if self.neighbors[d] != None and self.neighbors[d] != path \
and self.neighbors[d].map_checked != False:
self.neighbors[d].grab_neighbors(self, step)