-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathboard.py
48 lines (35 loc) · 1.36 KB
/
board.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
from output import Output
class Board:
def __init__(self, letters):
self.size = len(letters)
self.neighbours = {}
for row in letters:
if len(row) != self.size:
raise ValueError("Bad list size %s, expected %s" % (len(row), self.size))
for x in range(self.size):
self.neighbours[x] = []
for y in range (self.size):
self.neighbours[x].append(self._calc_neighbours(x, y))
self.letters = letters
def get_letter(self, col, row):
if col >= self.size or row >= self.size:
raise ValueError("Bad coordinates")
return self.letters[row][col]
def _calc_neighbours(self, col, row):
neighbours = []
for c in range(col-1, col+2):
for r in range(row-1, row+2):
neighbours.append((c, r))
neighbours.remove((col,row))
return filter(lambda t: t[0] in range(0, self.size) and t[1] in range(0, self.size), neighbours)
def get_neighbours(self, col, row):
return self.neighbours[col][row]
def __str__(self):
output = Output()
output.thick_bar(13)
output.add('| |')
for row in self.letters:
output.add('| ' + ' '.join(row) + ' |')
output.add('| |')
output.thick_bar(13)
return str(output)