-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtilesHandler.py
More file actions
126 lines (95 loc) · 3.51 KB
/
tilesHandler.py
File metadata and controls
126 lines (95 loc) · 3.51 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from tile import Tile
class Display():
def __init__(self,canvas,SIZE,TSIZE,maze):
self.size = SIZE
self.tsize = TSIZE
self.maze = maze
self.canvas = canvas
self.tiles= {}
self.listTiles = []
self.start = None
self.end = None
self.fillTiles()
self.fillNeighbours()
self.displayMaze()
def fillTiles(self):
for r in range(self.size):
for c in range(self.size):
t = Tile(r,c,self.tsize,self.maze)
if self.maze[r][c] == 'S':
self.start = t
if self.maze[r][c] == 'E':
self.end = t
self.tiles[r,c] = t
self.listTiles.append(t)
def fillNeighbours(self):
for coord , tile in self.tiles.items():
x,y = coord
# Top left corner :
if x == 0 and y == 0:
self.add_neighbours(tile, self.tiles[x+1, y]) # Under
self.add_neighbours(tile, self.tiles[x, y+1]) # Right
elif x == 0 and y == self.size - 1 :
self.add_neighbours(tile, self.tiles[x, y-1]) # Left
self.add_neighbours(tile, self.tiles[x+1, y]) # Under
elif x == self.size-1 and y == 0:
self.add_neighbours(tile, self.tiles[x - 1, y]) # Over
self.add_neighbours(tile, self.tiles[x, y + 1]) # Right
elif x == self.size-1 and y == self.size-1:
self.add_neighbours(tile, self.tiles[x - 1, y]) # Over
self.add_neighbours(tile, self.tiles[x, y - 1]) # Left
elif x == 0:
self.add_neighbours(tile, self.tiles[x + 1, y]) # Under
self.add_neighbours(tile, self.tiles[x, y + 1]) # Right
self.add_neighbours(tile, self.tiles[x, y - 1]) # Left
elif y == self.size-1:
self.add_neighbours(tile, self.tiles[x + 1, y]) # Under
self.add_neighbours(tile, self.tiles[x - 1, y]) # Over
self.add_neighbours(tile, self.tiles[x, y - 1]) # Left
elif x == self.size-1:
self.add_neighbours(tile, self.tiles[x - 1, y]) # Over
self.add_neighbours(tile, self.tiles[x, y - 1]) # Left
self.add_neighbours(tile, self.tiles[x, y + 1]) # Right
# Left border
elif y == 0:
self.add_neighbours(tile, self.tiles[x - 1, y]) # Over
self.add_neighbours(tile, self.tiles[x + 1, y]) # Under
self.add_neighbours(tile, self.tiles[x, y + 1]) # Right
# Elsewhere in the maze.
else:
self.add_neighbours(tile, self.tiles[x - 1, y]) # Over
self.add_neighbours(tile, self.tiles[x + 1, y]) # Under
self.add_neighbours(tile, self.tiles[x, y + 1]) # Right
self.add_neighbours(tile, self.tiles[x, y - 1]) # Left
def add_neighbours(self, tile1,tile2):
if tile2.val != '#':
if tile2 not in tile1.neighbours:
tile1.neighbours.append(tile2)
if tile1.val != '#':
if tile1 not in tile2.neighbours:
tile2.neighbours.append(tile1)
def displayMaze(self):
for r in range(self.size):
for c in range(self.size):
t = self.tiles[r,c]
self.displayTile(t,t.color,"#38B8BB")
def displayTile(self,tile,color,outline):
self.canvas.create_rectangle(tile.x1, tile.y1, tile.x2, tile.y2, fill = color , outline=outline)
self.canvas.update()
def redrawTile(self,tile,color,outline):
if tile != self.start and tile != self.end :
self.canvas.create_rectangle(tile.x1, tile.y1, tile.x2,tile.y2,fill=color,outline=outline)
self.canvas.update()
def constructPath(self,cameFrom,start,end):
cur = end
path = [cur]
while cur != start :
cur = cameFrom[cur]
path.append(cur)
path.append(start)
# path.reverse()
return path
def distance(tile1,til2):
x1,y1 = (tile1.x,tile1.y)
x2,y2 = (tile2.x,tile.y)
return abs(x1-x2) + abs(y1-y2)