-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maze.py
62 lines (53 loc) · 1.82 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
59
60
61
62
import Cell
def connect(c1, c2, direction):
oposite = {
'west': 'east',
'east': 'west',
'north': 'south',
'south': 'north'
}
c1.connect(direction)
c2.connect(oposite[direction])
class Maze:
def __init__(self, cells, start_position, width, height):
self.cells = cells
self.start_position = cells[start_position[0]][start_position[1]]
self.end_position = None
self.width, self.height = width, height
self.dimension = (width, height)
self.totalSteps = -1
self.path = list()
def reset_visited(self):
for line in self.cells:
for c in line:
c.visited = False
def get_neighbours(self, cell: Cell):
(x, y) = cell.coords
neigh = dict()
if y + 1 < self.height:
neigh['north'] = self.cells[x][y + 1]
if y - 1 >= 0:
neigh['south'] = self.cells[x][y - 1]
if x - 1 >= 0:
neigh['west'] = self.cells[x - 1][y]
if x + 1 < self.width:
neigh['east'] = self.cells[x + 1][y]
return neigh
def get_non_visited_neighbours(self, cell: Cell):
neigh = self.get_neighbours(cell)
return {k: v for (k, v) in neigh.items() if not v.visited}
def get_valid_non_visited_neighbours(self, cell: Cell):
neigh = self.get_non_visited_neighbours(cell)
a = [k for k, v in cell.walls.items() if not v]
res = [(k, v) for k, v in neigh.items() if k in a]
return dict(res)
def set_end_position(self, cell: Cell):
self.totalSteps = cell.step
self.end_position = cell
path = list()
c = cell
while c.parent is not None:
path.append(c.y + 8 * c.x)
c = c.parent
path.append(c.y + 8 * c.x)
self.path = path