-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrid.py
63 lines (47 loc) · 1.96 KB
/
Grid.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
from Tile import Tile
class Grid:
def __init__(self, height, width, tileSize):
self.height = height
self.width = width
self.tiles = {(x, y): Tile(x, y, tileSize) for x in range(width) for y in range(height)}
self.visitedTiles = {(x, y): 0 for x in range(width) for y in range(height)}
for tile in self.tiles.values():
self.findNeighbours(tile)
def findNeighbours(self, tile):
x, y = tile.gridPosition
if x > 0 and y < self.height - 1:
tile.neighbours.append(self.tiles[(x - 1, y + 1)])
if y < self.height - 1:
tile.neighbours.append(self.tiles[(x, y + 1)])
if x > 0:
tile.neighbours.append(self.tiles[(x - 1, y)])
if x < self.width - 1:
tile.neighbours.append(self.tiles[(x + 1, y)])
if y > 0:
tile.neighbours.append(self.tiles[(x, y - 1)])
if x < self.width - 1 and y > 0:
tile.neighbours.append(self.tiles[(x + 1, y - 1)])
def topRow(self):
return [self.tiles[(x, 0)] for x in range(self.width)]
def bottomRow(self):
return [self.tiles[(x, self.height - 1)] for x in range(self.width)]
def leftColumn(self):
return [self.tiles[(0, y)] for y in range(self.height)]
def rightColumn(self):
return [self.tiles[(self.width - 1, y)] for y in range(self.height)]
def findPath(self, fromTile, toTileList, playerColour, visited=None):
if visited is None:
visited = []
if fromTile.colour != playerColour:
return None
if fromTile in visited:
return None
if fromTile in toTileList:
return [fromTile]
visited.append(fromTile)
for neighbour in fromTile.neighbours:
path = self.findPath(neighbour, toTileList, playerColour, visited)
if path is not None:
path.append(fromTile)
return path
return None